Home Project Folders Resources How Do I? MWForum
Site Map Search Our Team
How do I set up a timer in a game?
Use resett and timer.

Resett stands for reset timer. It resets the built-in timer to 0.

Timer reports elapsed time in tenths of seconds. So to announce the time in seconds, we divide timer by 10.

to startup
reset
end

to reset
make "score 0
setseconds 0
everyone [st]
end

to go
resett
forever [time]
everyone [st forever [fd 1 / 3]]
when [:score = 20] [cancel [time] announce (se [You clicked all 20 balls in] int timer / 10 "seconds!) stopall]
end

to time
setseconds int timer / 10
wait 10
end

to touch
ht
make "score :score + 1
end
Click go to set the balls in motion. How fast can you click all 20 turtles to make them vanish? The instruction for each turtle is touch.

Notes:  

Int outputs the integer portion of the elapsed time (to avoid rapidly-changing decimals when we divide). You may display the time in a textbox or just keep track "behind the scenes" and make an announcement about the time when the game ends. Or you may do both, as in the game above. If you display the time in a textbox, you may round to the nearest second (as demonstrated above), or omit the primitive int if you wish to display tenths of seconds.

If the game runs for quite a long time, as in the 100-ball project below, you might wish to display minutes instead of seconds and display your running score as well. In this project, the final announcement displays elapsed minutes and seconds. Name your textboxes score and minutes and modify your procedures:

to reset
setscore 0
setminutes 0
everyone [st]
end

to go
reset
resett
forever [time]
everyone [st forever [fd 1 / 3]]
when [score = 100] [cancel [time] announce (se [You clicked 100 balls in] int timer / 600 [minutes] "and int timer / 10 - 60 * int timer / 600 "seconds!) stopall]
end

to time
setminutes int timer / 600
end

to touch
setscore score + 1
fd 100
if score > 80 [ht]
end
We only see 20 balls at a time, but we must click 100 of them to complete the game. They don't begin disappearing until we have clicked more than 80. (In this way, the game exceeds one minute.)

Note:  These projects also demonstrate two methods for keeping score. See How do I keep score in a game? for more information.

« Previous Menu Next »


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