Reaction Timer

In this exercise you will build a reaction timer and at the same time learn about random numbers. Randomness (otherwise known as 'chance' or 'luck') is an important part of many games, but the reaction timer itself is not a game. When you hit the 'n' key, the program waits for a random length of time between 3 and 7 seconds before showing the green light. This makes it more difficult to predict when the light will go green, and thus more difficult to cheat.

If you haven't tried the random number block before then you can get a feel for how it works by setting up a simple script like this (attach this script to any sprite, but not to the stage, since the stage can't 'talk').

You might find it hard to believe, but I spent about three hours working on the finished program. It took less than an hour to make a basic version but I kept working on it for two reasons:

  • I wanted to include 'cheat detection' - if you hit the space bar while the amber light is shining then the program detects this and stops you getting a good result that you don't deserve.
  • I was determined to make the project as 'elegant' as possible. By 'elegant' I mean short, simple and easy to understand. My finished program contains no sprites whatsoever and only 14 lines of code. I hope you get satisfaction from making your own programs elegant too for this would indicate that you might have a promising future in computer programming.

If you choose to make a reaction timer then build it step by step - don't try to include cheat detection until it works already for honest users. To get you started, here's a traffic light image you can use (though you don't have to). If you choose to use this image then you will probably have to work on it in a graphics program such as Paint Shop Pro in order to make the red, amber and green versions (my program has three backgrounds for the stage, each with a different colour).

One last thing to say about my reaction timer: I don't think the times are at all accurate. I've written reaction timers in other programming languages and my reaction time is usually less than 0.2 seconds which doesn't seem possible here. There isn't actually anything wrong with the code in my program but operations like changing backgrounds take a few moments and these 'moments' interfere with the accuracy of the timing. Even though the times are not accurate you can still use the timer to compare your reaction times with your friends.