Home Project Folders Resources How Do I? MWForum
Site Map Search Our Team
How do I hatch turtles under program control?
In some game projects, you might wish to surprise the user by hatching an unknown number of turtles as the game is running. Use newturtle, word, setinstruction, (and more) as appropriate. See Ant Hill 6, a game similar to the guessing game below.

Fill the jar with bouncing beans and guess how many there are. How many guesses do you need?
to fill_the_jar
make "#guesses 0
everyone [remove who]
hatchturtles
end

to hatchturtles
make "howmany 15 + random 20
dotimes [i :howmany]
[newturtle word "t 1 + :i
setinstruction [bounce]
seth random 360
setsh pick [b1 b2 b3 b4 b5 b6]
setsize 20 + random 21
clickon st]
end

to bounce
forever [fd 3
if not colorunder = 71
[rt 160 + random 40 fd 5] ]
end

to guess
make "#guesses :#guesses + 1
question [Guess how many beans!]
carefully
[ if answer > :howmany [announce [too high!] ]
if answer < :howmany [announce [too low!] ]
if answer = :howmany [announce
(se [That's right! It took you] :#guesses "guesses.) stopall] ]
[ announce [First fill the jar.] ]
end

Notes:  

It can be easier to create a lot of new turtles all at once with this method than to hatch and modify turtles one by one, as you prepare a project. But remember that you can also hatch a batch of identical turtles quickly if you select, copy, and paste (many times) a programmed turtle or cluster of turtles.

Everyone [remove who] removes all existing turtles from the page.

The Ant Hill 6 project notes explain one way to hatch and activate multiple new turtles. The hatchturtles procedure above is similar but it uses dotimes as an alternate to repeat and uses make instead of a hidden textbox to hold the value of howmany.

MicroWorlds cannot launch an unlimited number of processes. It can run at most 34 simultaneous processes. So if you animate the new turtles, you must limit their number to a maximum of 34. If you do not activate turtle instructions, you may hatch many more turtles (up to 150, if there are no other objects on the page). See the variation below.

to fill_the_jar
make "#guesses 0
everyone [remove who]
hatchturtles
end

to hatchturtles
make "howmany 50 + random 99
dotimes [i :howmany]
[newturtle word "t 1 + :i
setx -90 + random 180
sety -122 + random 188
setsh pick [b1 b2 b3 b4 b5 b6]
setsize 20 + random 21
st]
end

(The guess procedure is the same as above. There is no need for a bounce procedure.)

These beans don't bounce around, so we can hatch a lot more. Guess how many?


But if you are not going to animate your turtles, do you really need to hatch so many of them? The variation below uses just one turtle to stamp as many beans as you wish. When creating such a project, just remember to freeze the background with freezebg before you begin stamping so that you can erase just the stamped objects with clean, between each round of a game.

to fill_the_jar
make "#guesses 0
make "howmany 50 + random 350
repeat :howmany
[setx -90 + random 180
sety -122 + random 188
setsh pick [b1 b2 b3 b4 b5 b6]
setsize 20 + random 21
stamp]
end
(The guess procedure is the same as above.)

This version uses just one turtle! (Don't hatch a lot of turtles unless you really need to!)


« Previous Menu Next »


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