Ivan
Ivan

Reputation: 11

How to stop a turtle netlogo

I want to know how to stop my turtle (patron) from moving forward if there is a black patch ahead of it. The problem with this is that the go button is a forever button. I have tried to make the turtle turn using coordinates but of course that will not stop the turtles from moving forward since its a forever button. This is in NetLogo, just to clarify.

to go  ;; customer procedure
  ask patrons [
    fd 1
    while [any? other patrons-here]
      [ fd 1 ]
    if pxcor = -15 and pycor = 14
      [rt 90]
      if pxcor = -14 and pycor = 14

  ]

  tick   
end

Upvotes: 1

Views: 2095

Answers (1)

JenB
JenB

Reputation: 17678

I am not entirely clear whether you have multiple patrons. If the problem is that you want to stop the model running, then you can use the stop primitive. If the problem is that you want to stop a particular patron so it can do something else, then you could use the ifelse primitive to check if it moves or does the other thing each tick

globals [numwant]
turtles-own [new?]

to setup
  clear-all
  set numwant 5
  create-turtles 1 [set color green
                    set new? TRUE]
  ask n-of 30 patches [set pcolor red]
  reset-ticks
end

to go
   ask turtles with [new?]
   [ ifelse [pcolor] of patch-ahead 1 != red
     [ fd 1]
     [ set color yellow
       set new? FALSE
       ifelse count turtles < numwant
       [spawn]
       [stop]
     ]
   ]
  tick
end

to spawn
  hatch 1 [set color green
           setxy random-xcor random-ycor
           set heading random-float 360
           set new? TRUE]
end

Edited to add the multiple turtles and sequential spawn in response to comment. However, you might want to think through your logic a bit, it is unusual to want only one turtle moving at a time.

Upvotes: 2

Related Questions