你怎么用鼠标点击Netlogo杀死一只乌龟?

时间:2015-01-02 03:26:33

标签: 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  

1 个答案:

答案 0 :(得分:1)

您的代码几乎是正确的。但是一只乌龟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这里是任意的;你可以调高或调低灵敏度。