查看“海龟车的程序解释”的源代码
←
海龟车的程序解释
跳转至:
导航
、
搜索
因为以下原因,你没有权限编辑本页:
您刚才请求的操作只对以下1个用户组开放:
用户
。
您可以查看并复制此页面的源代码:
原有的程序 globals [ sample-car ] turtles-own [ speed speed-limit speed-min ] to setup clear-all ask patches [ setup-road ] setup-cars watch sample-car reset-ticks end to setup-road ;; patch procedure if (pycor < 2) and (pycor > -2) [ set pcolor white ] end to setup-cars if number-of-cars > world-width [ user-message (word "There are too many cars for the amount of road. Please decrease the NUMBER-OF-CARS slider to below " (world-width + 1) " and press the SETUP button again. The setup has stopped.") stop ] set-default-shape turtles "car" create-turtles number-of-cars [ set color blue set xcor random-xcor set heading 90 ;; set initial speed to be in range 0.1 to 1.0 set speed 0.1 + random-float 0.9 set speed-limit 1 set speed-min 0 separate-cars ] set sample-car one-of turtles ask sample-car [ set color red ] end ; this procedure is needed so when we click "Setup" we ; don't end up with any two cars on the same patch to separate-cars ;; turtle procedure if any? other turtles-here [ fd 1 separate-cars ] end to go ;; if there is a car right ahead of you, match its speed then slow down ask turtles [ let car-ahead one-of turtles-on patch-ahead 1 ifelse car-ahead != nobody [ slow-down-car car-ahead ] [ speed-up-car ] ;; otherwise, speed up ;; don't slow down below speed minimum or speed up beyond speed limit if speed < speed-min [ set speed speed-min ] if speed > speed-limit [ set speed speed-limit ] fd speed ] tick end ;; turtle (car) procedure to slow-down-car [ car-ahead ] ;; slow down so you are driving more slowly than the car ahead of you set speed [ speed ] of car-ahead - deceleration end ;; turtle (car) procedure to speed-up-car set speed speed + acceleration end ; Copyright 1997 Uri Wilensky. ; See Info tab for full copyright and license. 定义全局变量 sample-car 定义属于海龟的变量 speed speed-lim speed-min 对于setup 将全局变量清0 要求patchs执行setup-road setup-cars 观察 sample-car 将时钟计数器重设为0 结束 对于setup-road 在y坐标位于(-2,2)内的瓦片,设置颜色为白色 结束 对于setup-cars 如果汽车数量大于世界宽度 则执行“ 弹出用户信息,文字:There are too many cars for the amount of road. Please decrease the NUMBER-OF-CARS slider to below "(world-width + 1) " and press the SETUP button again. The setup has stopped 结束” 设置海龟的默认初始图形为 车 设置海龟数量“设置颜色 蓝色 设置海龟随机位置 设置海龟面向东 设置速度 speed 为0.1+0~0.9之间的随机浮点数 设置速度上限speed lim 1,下限speed min 0 seperate-cars” 设置sample car为海龟集合的随机一个 请求sample car设置颜色为红色 结束 对于seperate-cars 如果这里有了其他海龟车,那么“前进一步,separate-cars” 结束 对于go 请求海龟“让 car-ahead 是随机海龟前方一格瓦片上的海龟 如果 car-ahead 得到的不是nobody,那么减速(slow-down-car car-ahead),否则加速(speed-up-car) 如果速度小于最小速度,那么让最小速度取代速度 如果速度大于最大速度,那么让最大速度取代速度 让海龟前进速度的步数” 时钟计数器前进1 结束 对于slow-down-car car-ahead 设置速度减少,对主体,返回car-ahead的速度值 对于speed-up-car 设置速度增加
返回
海龟车的程序解释
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
查看
阅读
查看源代码
查看历史
操作
搜索
导航
首页
实践教学
个性化3D设计与实现
人工智能实践教学
区块链技术及应用
虚拟现实技术与内容制作
超越学科界限的认知基础课程
电子工艺实习
Nand2Tetris Engine Curriculum
TULLL Creative Learning Group
Wiki上手说明
Wiki账户创建
最近更改
工具
链入页面
相关更改
特殊页面
页面信息