要求海龟在遇到其他海龟时改变颜色并停下来 Netlogo
Ask turtles to change colour and stop when they meet other turtles Netlogo
我有两只乌龟颜色'green'和颜色'blue'。我想当绿海龟遇到蓝海龟时,蓝海龟变白并停止移动,而绿海龟继续随机移动。这是我的代码,我做了但是它给出了 Expected a literal value 的错误并且没有返回预期的结果。非常感谢任何帮助。
ask turtles with [color = green]
[if any? other turtles-here with [color = blue]
[set turtles with [color = blue] [color = white]
]
]
你忘了ask
(指导)海龟。关键字 with
子集是正确的海龟,但您需要 set
一个变量,而不是 set
海龟集。此外,'=' 用于评估(比较),而不是用于分配值。我想你想要这个:
ask turtles with [color = green]
[ if any? other turtles-here with [color = blue]
[ ask turtles-here with [color = blue]
[ set color white
]
]
]
请注意,我必须重新写turtles-here
,否则所有的蓝龟都会变白。一种减少错误的更易读的方法是为相关海龟设置一个临时变量(使用let
)。
ask turtles with [color = green]
[ let changers other turtles-here with [color = blue]
if any? changers
[ ask changers
[ set color white
]
]
]
我有两只乌龟颜色'green'和颜色'blue'。我想当绿海龟遇到蓝海龟时,蓝海龟变白并停止移动,而绿海龟继续随机移动。这是我的代码,我做了但是它给出了 Expected a literal value 的错误并且没有返回预期的结果。非常感谢任何帮助。
ask turtles with [color = green]
[if any? other turtles-here with [color = blue]
[set turtles with [color = blue] [color = white]
]
]
你忘了ask
(指导)海龟。关键字 with
子集是正确的海龟,但您需要 set
一个变量,而不是 set
海龟集。此外,'=' 用于评估(比较),而不是用于分配值。我想你想要这个:
ask turtles with [color = green]
[ if any? other turtles-here with [color = blue]
[ ask turtles-here with [color = blue]
[ set color white
]
]
]
请注意,我必须重新写turtles-here
,否则所有的蓝龟都会变白。一种减少错误的更易读的方法是为相关海龟设置一个临时变量(使用let
)。
ask turtles with [color = green]
[ let changers other turtles-here with [color = blue]
if any? changers
[ ask changers
[ set color white
]
]
]