Reputation: 1
There are a certain number of turtles distributed on the lower part of the grid. Some of the turtles are “parents”, they hatch 1 offspring with each go procedure. Additionally, the turtles move around with each go procedure. Is there any way that I can make the turtles stop after crossing 5 patches and then change color, thus it is easy to see which turtles stopped moving?
breed [birds bird];my turtles
birds-own [
parents?
]
to setup
ca
ask patches [set pcolor green]
create-birds 100 [
set color blue
set size 2
move-to one-of patches with [pycor < 50 ] ;100 x 100, wrapped,origin in the left bottom corner
set heading random 360
set parents? false
]
ask n-of 25 birds [set parents? true]
reset-ticks
end
to go
give-birth
fly-around
stop-flying
tick
end
to give-birth
ask birds with [parents? = true][
hatch 1
]
end
to fly-around
ask birds [
fd 2 display
]
end
to stop-flying
ask birds-on patches with [pycor > 70][
;instead of pycor as a condition, I want them do stop and change color after crossing 5 patches.
set color red
stop
]
end
Maybe it can be done via ticks, but since there are new turtles hatching with each tick, the turtles do not all have the same number of ticks (some are younger than others).
Upvotes: 0
Views: 53
Reputation: 2926
Sure, the idea would be to let each bird use a counter to check how many times it changed patch. Everytime a bird moves, it will check if the patch it is on is different from the previous one; if that's true, the counter will be incremented by 1; when the counter reaches 5, the bird stops.
Given that your birds move by a distance greater than 1 (currently by 2, in your code), in order to use the counter as above I suggest to make them move one step at a time and check the patch after every step. You can do this by using repeat 2 [fd 1 ...]
instead of using fd 2
.
You can implement this as follows:
birds-own [
counter
last-patch
stopped?
]
to setup
...
create-birds 100 [
...
set last-patch [patch-here]
set stopped? FALSE
...
]
...
end
to fly-around
repeat 2 [
if (not stopped?) [
fd 1
if (patch-here != last-patch) [
set counter counter + 1
if (counter = 5) [
set stopped? TRUE
set color red
]
]
]
]
end
There is some room to make this code more efficient (for example: as of now each bird will check twice if (not stopped?)
, given that that's inside the repeat 2 [ ... ]
command block, even if it already has stopped? = TRUE
), but that's pretty much the logic.
A note:
set heading random 360
is redundant in setup
, as that's the default behaviour when turtles are created through create-turtles
. However you might want to use it in hatch
, as hatched turtles will inherit the parent's heading. That depends on your intentions.Upvotes: 3