如何让海龟/代理在某个补丁上走得更快或更慢?

How to make turtles / agents go faster or slower when being on a certain patch?

我必须让乌龟在绿色补丁上走得更快,而当它们在蓝色补丁上时,速度应该降低。我添加了我尝试过的部分代码,但它不起作用。有人可以帮我吗? 提前致谢!!

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

我认为 user2901351 的意思是您在示例代码中使用了 neighbors。如果您查看 dictionary entry for neighbors, you'll see that it references the 8 patches around the current patch. If you instead want the turtle to check the patch it's currently on, you can use the patch-here 原语,或者作为快捷方式,只需让 turtle 直接检查 patch-owned 变量。下面是一个玩具示例,展示了工作中的示例 - 评论中有更多详细信息。

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