Netlogo:[CODE]直接要求您背后的补丁更改颜色

时间:2019-03-27 12:58:40

标签: netlogo agent-based-modeling

我正在尝试创建游戏,我想做的就是在乌龟吃完水果后立即在乌龟后面打补丁以改变其颜色。因此,乌龟移动并吃水果。吃了一种水果后,其上一个水果的后面的贴片会改变颜色,但是这种颜色会随乌龟一起移动,从而产生增加乌龟长度/大小的效果

现在,我尝试使用该代码:

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

问题是随着乌龟继续移动,整个路径变成黄色,而不是当前位置后面的斑块。是否有任何代码可以解决此问题?另外,当它吃另一种水果时,我希望它后面的两个色块为黄色。那我该怎么编码呢?

1 个答案:

答案 0 :(得分:2)

那么,你在追赶Snake这样的比赛吗?您可以给补丁添加一个memory变量,以便它们可以跟踪自乌龟经过它们以来已经有多长时间了。如果此记忆值被蛇体的当前“大小”(基于它消耗了多少水果)修改,则可以使色块保持更长的颜色。下面是一个简单的版本(没有控件,蛇只是随机运动):

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