要求海龟在经过某个补丁时改变颜色 Netlogo

Ask turtles to change colour when they go through a certain patch Netlogo

当乌龟经过某个补丁时,它会改变颜色并继续随机移动新颜色。所以我问名为 'yong' 的海龟是黄色的,如果任何 'yong' 海龟穿过半径为 2 的补丁,它应该将颜色变为绿色。我的代码运行没有错误,但在输出中,所有 'yong' 黄色的海龟都将颜色变为绿色,这不是我想要的。

to interact-turtles
 ask yong [
 if any? yong-on patches in-radius 2 [ set color green  ]
  ]
end

你的问题是你让乌龟询问补丁 in-radius,这将把这些补丁的来源放在乌龟恰好所在的地方。相反,您需要距离原点 2 以内的补丁。

to interact-turtles
  let sink patches with [abs px-cor <= 2 and abs py-cor <= 2]
  ask yong-on sink [ set color green  ]
end

但是如果你想让这个汇聚区域成为环境的永久特征,你可以使用变量来存储它而不是重复创建它。作为全局变量:

globals [sink]

to setup
  ...
  set sink patches with [abs px-cor <= 2 and abs py-cor <= 2]
  ...
end

然后你就可以随时ask yong-on sink做事了。

或者您可以为每个补丁设置一个 true/false(布尔值)变量,说明它是否在接收器中。

patches-own [sink?]

to setup
  ...
  ask patches [ set sink? if abs px-cor <= 2 and abs py-cor <= 2]
  ...
end

然后你可以用ask yong-on patches with [sink?]这样的结构来做事。