你如何在 Netlogo 中点击鼠标杀死一只乌龟?
How do you mouseclick to kill a turtle in Netlogo?
我正在尝试为 netlogo 制作一款打地鼠游戏
任何帮助,将不胜感激
这是我的全长代码
globals [
game-over?
]
To setup
ca
set game-over? false
ask patches [set pcolor green]
end
To play
if game-over? [
ask turtles [die]
ask patch 0 4 [set plabel "GAME OVER"]
]
set-default-shape turtles "ant 2"
crt 1
ask turtle 0 [
set size 7
set color brown
set xcor random 33 - 16
set ycor random 33 - 16]
if mouse-down? [
ask turtles with [round xcor = round mouse-xcor and round ycor = round mouse-ycor] [
die]
]
end
您的代码几乎是正确的。但是乌龟的 xcor
几乎永远不会完全等于 round mouse-xcor
,除非乌龟恰好站在补丁中心。如果你的海龟没有死,这可能就是原因。
再添加一些舍入应该可以使它起作用:
if mouse-down? [
ask turtles with [round xcor = round mouse-xcor and round ycor = round mouse-ycor] [
die
]
]
但请注意,实际上利用补丁网格比使用 round
更容易。以下代码在没有显式舍入的情况下执行相同的操作:
if mouse-down? [
ask turtles-on patch mouse-xcor mouse-ycor [
die
]
]
根据您希望游戏如何运行,您也可以考虑忽略小块边界作为确定点击了哪个海龟的基础,而只计算海龟与点击点的实际距离:
if mouse-down? [
ask turtles with [distancexy mouse-xcor mouse-ycor < 0.5] [
die
]
]
这里的0.5
是任意的;你可以上下调整它来控制灵敏度。
我正在尝试为 netlogo 制作一款打地鼠游戏 任何帮助,将不胜感激 这是我的全长代码
globals [
game-over?
]
To setup
ca
set game-over? false
ask patches [set pcolor green]
end
To play
if game-over? [
ask turtles [die]
ask patch 0 4 [set plabel "GAME OVER"]
]
set-default-shape turtles "ant 2"
crt 1
ask turtle 0 [
set size 7
set color brown
set xcor random 33 - 16
set ycor random 33 - 16]
if mouse-down? [
ask turtles with [round xcor = round mouse-xcor and round ycor = round mouse-ycor] [
die]
]
end
您的代码几乎是正确的。但是乌龟的 xcor
几乎永远不会完全等于 round mouse-xcor
,除非乌龟恰好站在补丁中心。如果你的海龟没有死,这可能就是原因。
再添加一些舍入应该可以使它起作用:
if mouse-down? [
ask turtles with [round xcor = round mouse-xcor and round ycor = round mouse-ycor] [
die
]
]
但请注意,实际上利用补丁网格比使用 round
更容易。以下代码在没有显式舍入的情况下执行相同的操作:
if mouse-down? [
ask turtles-on patch mouse-xcor mouse-ycor [
die
]
]
根据您希望游戏如何运行,您也可以考虑忽略小块边界作为确定点击了哪个海龟的基础,而只计算海龟与点击点的实际距离:
if mouse-down? [
ask turtles with [distancexy mouse-xcor mouse-ycor < 0.5] [
die
]
]
这里的0.5
是任意的;你可以上下调整它来控制灵敏度。