(Created page with "定义全局变量 sample-car 定义属于海龟的变量 speed speed-lim speed-min 对于setup 将全局变量清0 要求patchs执行setup-road setup-cars 观察 sample-car...") |
小 (6个版本:all) |
||
(1位用户的5个中间修订版本未显示) | |||
第1行: | 第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 | 定义全局变量 sample-car | ||
− | 定义属于海龟的变量 speed speed- | + | |
+ | 定义属于海龟的变量 speed speed-limit speed-min | ||
对于setup | 对于setup | ||
+ | |||
将全局变量清0 | 将全局变量清0 | ||
+ | |||
要求patchs执行setup-road | 要求patchs执行setup-road | ||
+ | |||
setup-cars | setup-cars | ||
+ | |||
观察 sample-car | 观察 sample-car | ||
+ | |||
将时钟计数器重设为0 | 将时钟计数器重设为0 | ||
+ | |||
结束 | 结束 | ||
对于setup-road | 对于setup-road | ||
+ | |||
在y坐标位于(-2,2)内的瓦片,设置颜色为白色 | 在y坐标位于(-2,2)内的瓦片,设置颜色为白色 | ||
+ | |||
结束 | 结束 | ||
对于setup-cars | 对于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 | + | |
− | + | 则执行“ 弹出用户信息,文字: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 limit 1,下限speed min 0 | ||
+ | |||
+ | seperate-cars” | ||
+ | |||
设置sample car为海龟集合的随机一个 | 设置sample car为海龟集合的随机一个 | ||
+ | |||
请求sample car设置颜色为红色 | 请求sample car设置颜色为红色 | ||
+ | |||
结束 | 结束 | ||
对于seperate-cars | 对于seperate-cars | ||
+ | |||
如果这里有了其他海龟车,那么“前进一步,separate-cars” | 如果这里有了其他海龟车,那么“前进一步,separate-cars” | ||
+ | |||
结束 | 结束 | ||
对于go | 对于go | ||
+ | |||
请求海龟“让 car-ahead 是随机海龟前方一格瓦片上的海龟 | 请求海龟“让 car-ahead 是随机海龟前方一格瓦片上的海龟 | ||
+ | |||
如果 car-ahead 得到的不是nobody,那么减速(slow-down-car car-ahead),否则加速(speed-up-car) | 如果 car-ahead 得到的不是nobody,那么减速(slow-down-car car-ahead),否则加速(speed-up-car) | ||
+ | |||
如果速度小于最小速度,那么让最小速度取代速度 | 如果速度小于最小速度,那么让最小速度取代速度 | ||
+ | |||
如果速度大于最大速度,那么让最大速度取代速度 | 如果速度大于最大速度,那么让最大速度取代速度 | ||
+ | |||
让海龟前进速度的步数” | 让海龟前进速度的步数” | ||
+ | |||
时钟计数器前进1 | 时钟计数器前进1 | ||
+ | |||
结束 | 结束 | ||
对于slow-down-car car-ahead | 对于slow-down-car car-ahead | ||
− | + | ||
+ | 设置 速度speed 为 car-ahead的speed减去deceleration | ||
+ | |||
对于speed-up-car | 对于speed-up-car | ||
− | + | ||
+ | 设置速度speed 为 speed加上acceleration |
2016年4月6日 (三) 03:03的最后版本
原有的程序
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-limit 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 limit 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
设置 速度speed 为 car-ahead的speed减去deceleration
对于speed-up-car
设置速度speed 为 speed加上acceleration