改变颜色后如何设置海龟的默认颜色(海龟进入模型时的颜色)?
How to set a default color(turtle's color when its enter in the model) of turtle after it changes its color?
我写的一行代码是这样的
ask turtles [if count other turtles in-radius 1 > 5 [set color white]]
现在乌龟的颜色变成了白色,但在一定时间后 属性 对于同一只乌龟不成立,它应该将颜色更改为默认颜色吗?
我该如何解决?
我认为您正在寻找一个 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
我写的一行代码是这样的
ask turtles [if count other turtles in-radius 1 > 5 [set color white]]
现在乌龟的颜色变成了白色,但在一定时间后 属性 对于同一只乌龟不成立,它应该将颜色更改为默认颜色吗? 我该如何解决?
我认为您正在寻找一个 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