下面完成绘图、开关和监视器的创建,可以自定义海龟的数量(滑动条)。选择下图中的相应选项创建组件。
麦好的AI乐园博客所有内容是原创,如果转载请注明来源
http://blog.csdn.net/myhaspl/
同时,让海龟自动繁殖,并规定,在1000步后模型停止,并编写相应代码:
;code:myhaspl@myhaspl.com
;date:2014-01-24
turtles-own [energy]
patches-own [oxygen]
to setup
clear-all
reset-ticks
create-turtles turtlescount
setup-turtles
setup-patches
end
to go
if ticks >= 1000 [stop]
die-turtles
reproduce-turtles
generate-oxygen
move-turtles
breathe
tick
plots
end
to generate-oxygen
ask patches [
if oxygen <= 0 [
set pcolor 100
]
if oxygen < 10[
set oxygen (oxygen + 0.1)
if pcolor <= 109 [
set pcolor pcolor + 0.1
]
]
if oxygen > 9 [
set pcolor 109
set oxygen 10
]
]
end
to move-turtles
ask turtles [
if energy > 0[
right random 360
forward 1
set energy (energy - 1)
]
]
end
to breathe
ask turtles [
if oxygen > 0 [
set oxygen (oxygen - 2)
set energy (energy + 2)
if pcolor >= 100 [
set pcolor pcolor - 0.1
]
]
ifelse show_energy
[set label energy ]
[set label "" ]
]
end
to setup-patches
ask patches[
set pcolor 109
set oxygen 10
]
end
to setup-turtles
ask turtles[
setxy random-xcor random-ycor
set energy 10
]
end
to die-turtles
ask turtles[
if energy <= 0[
die
]
]
end
to reproduce-turtles
ask turtles[
if energy > 50[
set energy energy - 50
hatch 1[set energy 10]
]
]
end
to plots
set-current-plot "myplots"
set-current-plot-pen "turtles"
plot count turtles
set-current-plot-pen "oxygen"
plot count patches with [pcolor = 109]
end