Will
Will

Reputation: 61

How do you mouseclick to kill a turtle in Netlogo?

im trying to make a whack a mole game for netlogo any help would be appreciated here is my full length code

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  

Upvotes: 2

Views: 1092

Answers (1)

Seth Tisue
Seth Tisue

Reputation: 30453

Your code is almost correct. But a turtle's xcor will hardly ever exactly equal round mouse-xcor, unless the turtle happens to be standing on a patch center. If your turtles aren't dying, that's probably why.

Adding some more rounding should make it work:

if mouse-down? [
  ask turtles with [round xcor = round mouse-xcor and round ycor = round mouse-ycor] [
    die
  ]
]

But note that it's actually easier to take advantage of the patch grid than to use round. The following code does the same thing without explicit rounding:

if mouse-down? [
  ask turtles-on patch mouse-xcor mouse-ycor [
    die
  ]
]

Depending on how you want your game to work, you might also consider ignoring the patch boundaries as a basis for determining what turtle was clicked, and just compute the actual distance of the turtle from the click point:

if mouse-down? [
  ask turtles with [distancexy mouse-xcor mouse-ycor < 0.5] [
    die
  ]
]

The 0.5 here is arbitrary; you could adjust it up or down to control the sensitivity.

Upvotes: 2

Related Questions