|
|||||
|
| 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.
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.
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.
|
|||||||||||
| « Previous Menu Next » |