Home Project Folders Resources How Do I? MWForum
Site Map Search Our Team
How do I remove turtles with a command?
In some game projects, you might wish to hide or remove turtles one by one:

t1, ht   (to hide a turtle)

remove "t1   (to remove a turtle from the page)

everyone [remove who]   (to remove all turtles from the page… who is the current turtle)

Note:  When you remove turtles as a program is running, you need to have a way to restore the turtles if you wish to start the action all over again. See How do I hatch turtles under program control? The project below shows how to create a batch of turtles and remove them one by one as the game proceeds.

Click go to set the turtles in motion. Click attack! and the snake will try to catch a turtle. How many tries does it take to catch all the turtles?
to startup
reset
end

to reset
make "tries 0
make "turtlecount 10
everyone [remove who]
newturtle "snake
seth 90
setpos [-120 80]
setsh "snake1 st
make "howmany 10
dotimes [i :howmany]
[ newturtle word "t 1 + :i
setx -110 + random 220
sety -135
setc random 140
setinstruction [bye]
st]
end

to bye
forever [fd (1 / 7]
when [touching? "snake who] [make "turtlecount :turtlecount - 1 remove who]
end

to go
ask [t1 t2 t3 t4 t5 t6 t7 t8 t9 t10] [wait 10 clickon]
end

to attack!
make "tries :tries + 1
snake, launch [repeat 10 [wiggle] allgone? snake, setx -120]
end

to wiggle
setsh "snake2 fd 10 wait 1 setsh "snake1 fd 10 wait 1
end

to allgone?
if :turtlecount = 0 [announce (se [The snake ate all the turtles in] :tries "tries!]
end

Note:  Do you really need to remove turtles or can you just hide them? Here is another version of the project which simply hides the turtles with ht. Since they are never removed, they never need to be hatched again. But be careful! The turtles will keep moving even if they are hidden. Be sure to stop all the action when the game is over. (stopall has been added to the allgone? procedure.) The startup procedure runs automatically when the project is launched and ensures that all turtles are in position.

to reset
make "tries 0
make "turtlecount 10
snake, setpos [-120 80]
ask [t1 t2 t3 t4 t5 t6 t7 t8 t9 t10] [ setx -110 + random 220
sety -135
setc random 140 st ]
end

to bye
forever [fd (1 / 7]
when [touching? "snake who] [make "turtlecount :turtlecount - 1 ht]
end

to allgone?
if :turtlecount = 0 [announce (se [The snake ate all the turtles in] :tries "tries!) stopall]
end

The procedures go, attack!, and wiggle are the same as above.

This version hides turtles instead of removing them. The turtles each have the instruction, bye.


« Previous Menu Next »


© copyright 2001 - 2006  OpenWorld Learning (OWL).   All rights reserved.