Netlogo:[CODE] 要求直接在你身后的补丁改变颜色

Netlogo: [CODE] Asking patches directly behind you to change colour

我正在尝试创建一个游戏,我想做的是让乌龟身后的补丁在乌龟吃水果后立即改变颜色。所以乌龟移动并吃水果。吃一个水果后,后面的那个补丁会改变颜色,但是这个颜色会随着乌龟移动,从而产生乌龟length/size增加的效果

现在,我尝试使用代码:

ask turtles
[ if score = 10
  [ ask patch-ahead -1
    [ set pcolor yellow
    ]
  ]
]

这个问题是,随着海龟继续移动,整个路径变成黄色,而不是它当前位置后面的小块。有没有代码可以解决这个问题?另外,因为它吃另一种水果,我想要它后面的两个色块是黄色的。那么我该如何编码呢?​​

那么你喜欢贪吃蛇这样的游戏吗?你可以给补丁一个 memory 变量,这样他们就可以跟踪自乌龟经过它们以来已经过了多长时间。如果这个内存值被蛇体的当前 'size' 修改(基于它消耗了多少水果),你可以让补丁保持颜色更长时间。下面是一个简单的版本(没有控制,蛇只是随机移动):

patches-own [ mem ]
breed [ snakes snake ]
breed [ fruits fruit ]

snakes-own [ tail-len ]

to setup
  ca
  create-snakes 1 [
    set color white
    set tail-len 1
    face one-of neighbors4
    ask patch-here [
      set pcolor [pcolor] of myself
    ]
  ]
  create-fruits 30 [
    move-to one-of patches
    set shape "flower"
  ]
  reset-ticks
end

to go
  ask snakes [
    if random-float 1 < 0.05 [
      rt one-of [ 90 -90 ]
    ]
    if [ pcolor ] of patch-ahead 1 = white [
      stamp
      die
    ]
    move-to patch-ahead 1
    ask patch-ahead -1 [
      set pcolor [color] of myself
      set mem [tail-len] of myself + 1
    ]
    if any? fruits-here [
      ask fruits-here [ die ]
      set tail-len tail-len + 1
    ]
  ]
  if not any? snakes [
    print "The snake tried to eat itself."
    stop
  ]
  ask patches with [ mem > 0 ] [
    set mem mem - 1
    if mem = 0 [
      set pcolor black
    ]
  ]
  tick
end