我必須讓海龜在綠地時跑得更快,而當它們在藍地時,速度應該降低。我添加了我嘗試過的部分代碼,但它不起作用。有人可以幫我嗎?提前致謝!!
turtles-own [speed]
to go
ask turtles [
left 60
right random 60
forward 1
if any? neighbors with [pcolor = green]
[
set speed speed 1
]
if any? neighbors with [pcolor = blue]
[
set speed speed 0.1
]
]
reset-ticks
uj5u.com熱心網友回復:
我認為 user2901351 得到的是您neighbors在示例代碼中使用的。如果您查看鄰居的字典條目,您會看到它參考了當前補丁周圍的 8 個補丁。如果您希望海龜檢查它當前所在的補丁,您可以使用patch-here原語,或者作為快捷方式,直接要求turtle檢查補丁擁有的變數。下面是一個玩具示例,展示了一個在作業中的示例 - 評論中的更多詳細資訊。
turtles-own [speed]
to setup
ca
crt 5 [ set speed 1 ]
ask n-of 20 patches [
ifelse random-float 1 < 0.5
[ set pcolor green ]
[ set pcolor blue ]
]
reset-ticks
end
to go
ask turtles [
; Since 'pcolor' is a patch variable, if the turtle queries pcolor
; it will check the patch's variable directly.
if pcolor = green [ set speed speed 0.5 ]
if pcolor = blue [ set speed speed - 0.5 ]
; Make sure that the turtles are moving forward by their speed value,
; rather than the hard-coded value of "1" in the example code. Also,
; I've included a check here so the turtles don't either stop moving
; or start moving backwards.
if speed < 1 [ set speed 1 ]
rt random-float 60 - 30
fd speed
show speed
]
tick
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429851.html
