更改颜色后如何设置乌龟的默认颜色(当它进入模型时为乌龟的颜色)?

时间:2018-06-28 10:27:27

标签: netlogo agent-based-modeling

我写的是这行代码

ask turtles [if count other turtles in-radius 1 > 5 [set color white]]

现在当乌龟将颜色更改为白色时,但是在经过一定时间后,对于同一只乌龟来说,此属性不成立时,应该将其颜色更改为默认颜色吗? 我该如何解决?

1 个答案:

答案 0 :(得分:1)

我认为您在turtles-own计数器后面,只要条件不满足,计数器就会减少。使用此设置:

turtles-own [ default-color countdown ]

to setup
  ca
  crt 150 [ 
    setxy random-xcor random-ycor 
    set default-color blue
    set color default-color
  ]
  reset-ticks
end

现在,每当海龟改变颜色时,它们都可以四处游荡并更改其countdown变量。当不满足该条件时,他们可以减少计数器直到计数器归零为止,此时它们可以恢复为默认颜色。在评论中有更多详细信息:

to go
  ask turtles [
    rt random 60 - 30
    fd 0.25

    ; if there are more than 5 turtles in radius 3,
    ; turn white and set countdown to 5   
    ifelse count other turtles in-radius 3 > 5 [
      set color white 
      set countdown 5
    ] [
      ; If not, and counter is greater than 0,
      ; decrease the counter. 
      if countdown > 0 [
        set countdown countdown - 1

        ; If counter gets down to 0, 
        ; set color back to the default.
        if countdown = 0 [
          set color default-color
        ]
      ]
    ]

  ]
  tick
end