我建议在 NetLogo 模型库的生物学部分查看 Wolf Sheep Predation 模型。它的代码正是这样做的。
代码的相关部分是:
patches-own [countdown]
to setup
...
ask patches [
set countdown random grass-regrowth-time
set pcolor one-of [green brown]
]
..
end
to go
...
ask sheep [ eat-grass ]
ask patches [ grow-grass ]
...
end
to eat-grass ;; sheep procedure
if pcolor = green [
set pcolor brown
]
end
to grow-grass ;; patch procedure
;; countdown on brown patches: if reach 0, grow some grass
if pcolor = brown [
ifelse countdown <= 0
[ set pcolor green
set countdown grass-regrowth-time ]
[ set countdown countdown - 1 ]
]
end
请注意,这grass-regrowth-time
是一个滑块。