turtles-own [energy]
to setup
__clear-all-and-reset-ticks
reset-ticks
ask patches[
ifelse random-float 1 < 0.2
[
set pcolor green
]
[
set pcolor black
]
]
create-turtles 1[
setxy random-xcor random-ycor
set heading random 360
set energy 100
]
end
to go
addfood
ask turtles [
turtle_move
turtle_breed
turtle_die
]
do_plot
tick
end
to addfood
let i 0
while [i < 10]
[
let xx random-pxcor
let yy random-pycor
ask patches with [pxcor = xx and pycor = yy][
set pcolor green
]
set i i + 1
]
end
to turtle_move
if pcolor = green[
;eat food
set pcolor black
set energy energy + 10
]
forward 1
if random-float 1 < 0.1
[
set heading random 360
]
set energy (energy - 2)
end
to turtle_breed
if energy > 50
[
set energy (energy / 2)
hatch 1 [;born new pathces with 100 energy
fd 1
set energy 100
]
]
end
to turtle_die
if energy <= 0[
die
]
end
to do_plot
set-current-plot "Population"
set-current-plot-pen "grass"
plot count patches with [pcolor = green]
set-current-plot-pen "sheep"
plot count turtles
end
- turtles-own [energy]:为绘图agent新增属性energy
- let xxx random-pxcor/pycor:为xxx赋值为当前虚拟世界的x/y坐标随机值
- hatch 1 [...]:新增1个turtle agent并继承父对象的所有属性值包括位置,再使它执行[...]操作
- set-current-plot "XXX":设定当前绘图的画板,XXX是已经定义的plot控件的名字
- set-cuurent-plot-pen "XXX":更新当前画笔为"XXX"
- plot count patches with [pcolor = green]:使用当前画笔,绘制颜色为green的patches的数量
- plot count turtles:同6