我是Netlogo编程的新手,我需要调整代码,这样海龟们在编队飞行50个刻度后就会感到无聊,然后右转90度。我在查看其他一些示例时修改了代码,但我有一种非常强烈的预感,它并没有完成它应该做的事情。你能帮帮我或者给我点提示吗?下面是有问题的代码:
turtles-own [
flockmates ;; agentset of nearby turtles
nearest-neighbor ;; closest one of our flockmates
flock-tick
]
to setup
clear-all
create-turtles population
[ set color yellow - 2 + random 7 ;; random shades look nice
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set flockmates no-turtles
set flock-tick 0 ]
reset-ticks
end
to go
ask turtles [ flock ]
;; the following line is used to make the turtles
;; animate more smoothly.
repeat 5 [ ask turtles [ fd 1 ] display ]
;; for greater efficiency, at the expense of smooth
;; animation, substitute the following line instead:
;; ask turtles [ fd 1 ]
tick
end
to flock ;; turtle procedure
find-flockmates
if any? flockmates
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[ separate ]
[ align
cohere ] ]
end
to find-flockmates ;; turtle procedure
set flockmates other turtles in-cone vision 90
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
to flock-timer
if any? flockmates [
set flock-tick 50
while [ flock-tick > 0 ] [
set flock-tick ( flock-tick - 1 ) ]
if flock-tick = 0
[ set heading ( heading - 90 ) ] ]
end代码的其余部分仍然与库中的模型相同。我必须承认我真的不知道我在做什么,而且西北大学的网站对我也没有太大的帮助(可能是我自己的错)。
谢谢!
发布于 2017-03-13 00:23:17
如果我们在你的代码中添加注释,我们可以看到逻辑问题。
to flock-timer
if any? flockmates [
;the next line sets `flock-tick` to 50
set flock-tick 50
;the next line sets `flock-tick` to 0
while [flock-tick > 0] [set flock-tick (flock-tick - 1)]
;so the next condition will always be satisfied
if (flock-tick = 0) [
;and so the heading will always be changed
set heading (heading - 90)
]
]
end您可能会将事情分解,以便更容易遵循逻辑。
to flock-timer ;turtle proc
update-flock-tick
if (flock-tick = 0) [get-bored]
end
to update-flock-ticks ;turtle proc
;you can add additional logic here if you wish
if (flock-tick > 0) [
set flock-tick (flock-tick - 1)
]
end
to get-bored ;turtle proc
set heading (heading + one-of [-90, 90])
end这不是一个完整的解决方案,但应该可以让您开始使用它。
https://stackoverflow.com/questions/42748761
复制相似问题