我有 6 只海龟,它们应该会环游世界。它们从原点 (0,0) 开始,沿直线前进,直到到达世界的边缘,然后它们面向原点,然后返回。他们应该在到达原点后停下来,问题是他们没有停在原点。他们继续走到对面的墙上,然后从那里弹开。
代码如下:
to searching
if not can-move? 1 [
set return-to-base true
set label "RTB"
go-home]
forward 1
end
to go-home
ifelse pxcor = 0 and pycor = 0 [
set return-to-base false
set label ""
stop]
[ facexy 0 0 ]
forward 1
end
“RTB”标签永远不会被重置,这告诉我即使海龟穿过原点,它们实际上并没有看到 (0,0)?
想通了。下面的代码完成了我想要它做的事情。
to searching
if not can-move? 1 [
set return-to-base true
set label "RTB"]
if return-to-base [go-home]
forward 1
end
to go-home
ifelse pxcor <= 1 and pxcor >= -1 and pycor <= 1 and pycor >= -1 [
set label ""
set return-to-base false
stop]
[facexy 0 0]
forward 1
end