Timers, Countdowns, Stopwatches Oh My!
Time is an important element of most games. The Unruly Splats block language provides a few different ways of measuring time using the Stopwatch widget. Let’s take a closer look at the Stopwatch and how it is used in play.
Meet the Stopwatch
The Stopwatch appears in the widget area above the Virtual Splats and the Run button. Here it is:
The widget holds a number that represents the number of seconds that have elapsed since the Stopwatch was started; it’s accurate to about 10 milliseconds. Typically the Stopwatch counts upward but it can also be used as a countdown timer, as we’ll see in a minute.
The main blocks for starting and setting the Stopwatch can be found in the Controls menu:
The simplest way of using the Stopwatch is to start and stop it on different events, like two separate Splat presses. For example, this short program gives you the time it takes to run to Splat #2 after stepping on Splat #1:
One thing you’ll notice is that if you hit Splat #1 again after hitting Splat #2 it will start the timer from its last value. This could be useful for some types of gameplay, or you can initialize it to 0 each time with the Set Stopwatch block:
Note that the Stopwatch is reset to 0 each time you hit the Run button.
You can get the value of the stopwatch and use it in your program with the stopwatch block in the Sensing menu. For example, adding to the previous program:
Here we added an If/Else statement that checks the value of the stopwatch each time Splat #2 is pressed. If it took less than 2 seconds to run between Splats (stopwatch < 2) the player gets a Cheer. This same approach can be used for other types of time-based scoring in conjunction with the Scoring menu blocks.
Using the Stopwatch with the Run button
The “when program starts” block is triggered when the Run button is placed. You can think of this as the “main body” of your program, or your event loop (see “Event-based Programming” for more about this block). Try this program and hit the Run button for a simple example.
You’ll notice that the Stopwatch at the end will be a little bit more than 1.00; that’s because it take a little bit of time for the program to execute each block (in this case about 30 milliseconds).
You’ll also see that the program ends after 1 second and nothing will happen until you hit the Run button again. Something else you may notice is that the program can’t do anything else in that block while the delay block is waiting.
You can use the “when splat pressed” blocks in conjunction with the “when program starts” block. This simple game gives you a point each time Splat #1 is pressed until the Stopwatch reaches 10 seconds:
You can also use a While/Do loop in the “when program starts” block to create a time limit for a game. This program will light up all the Splats with random colors for 10 seconds, then end:
As soon as the Stopwatch reaches 10 seconds the condition for the While loop is false and the program continues past the loop to the next block that stops the Stopwatch.
Stopwatch as a countdown timer
The Stopwatch can be set to a negative number if you want, which could be used as a kind of “countup” timer before the start of the game. Or you can use the countdown block, which turns the Stopwatch into a proper reverse countdown timer. The countdown block takes a starting number (in seconds) and subtracts one from the Stopwatch each second.
Here’s another way of writing the previous program using the countdown timer:
Another use for the countdown is to trigger an event at a particular time. For example, counting down from 10 and flashing red for the last 5 seconds as a warning that time is almost up. Here’s one wrong way to do that:
It’s instructive to take a look at this program because it shows how tricky it can be sometimes when approaching event-based programming. If you trace this program, the “when program starts” block is executed when the Run button is touched and the Stopwatch starts counting down from 10. When it hits the While loop the value of the Stopwatch right around 10 so the condition (stopwatch < 5) is not true and the program ends. Instead, you should also check for a stopwatch value greater than (or equal to) 5:
Advanced Stopwatch Hacking
Well, it’s not that advanced, and more of a trick than a hack. Here’s one more example of using the Stopwatch to trigger an event every 5 seconds. In this case the event is a random animal noise; maybe the game involves running through a zoo:
This program uses the remainder block to check if the stopwatch value is an even multiple of 5, and triggers the sound when it is. The remainder block is handy for triggering things because it will be 0 for the values 5, 10, 15, 20, 25, etc.
There are many variations of these techniques you can use but these examples should give you a good foundation for using time in your game programming.