我用命令ask patches with [ pxcor < 0] [set pcolor blue] ask patches with [pxcor > 0] [set pcolor green]
创建了一个分为两部分的世界。其中一只海龟有100只,其中5只感染了病毒,另一只也感染了病毒。我的问题是强迫海龟只在它们所在的地方活动。所以我希望pcolor=蓝色的海龟在第二和第三象限(pxcor<0(随机移动,pcolor=绿色的海龟在第一和第四象限(pxcor>0(。我该怎么办?
这是代码:
turtles-own
[
sick?
sick-time]
to setup
ca
ask patches with [ pxcor < 0 ] [set pcolor blue] ; we want divide the world in two parts: the blue one in the north of Italy
ask patches with [pxcor > 0 ] [set pcolor green]; the white one is the south of Italy
ask patches with [pxcor = 0 ] [set pcolor white ] ; represent the border
create-turtles 200 ; we create a population made up for 200 people
[ set size 1
set shape "person"
set sick-time 0
get-healthy]
ask n-of 100 turtles ; 100 of this one live in north of Italy
[setxy 12 random-ycor ]
ask n-of 100 turtles ; another 100 in the south
[setxy -12 random-ycor
]
ask n-of 5 turtles with [pcolor = blue] ; we want infect 5 people for each world
[get-sick ]
ask n-of 5 turtles with [pcolor = green]
[get-sick ]
reset-ticks
end
to get-healthy
set sick? false
set color white
end
to get-sick
set sick? true
set color yellow
set shape "circle"
set sick-time sick-time + 1
end
to go
ask turtles
[
move ]
tick
end
to move
rt random-float 360
fd 1
end
您的移动过程看起来像:
to move
right random-float 360
forward 1
end
如果你想让他们呆在原地,如果移动会让他们进入错误的一半,那么你可以使用patch-ahead
来测试他们将要移动的补丁。我认为你想要的是他们不会去不同颜色的补丁。一种方法是:
to move
right random-float 360
if [pcolor] of patch-ahead 1 = pcolor [forward 1]
end
[pcolor] of patch-ahead 1
返回前方一个距离单位的补丁的颜色,即乌龟试图移动到的位置。pcolor
是乌龟当前站在的补丁的色彩。