Today we are going to be building a reaction timer game. The goal is for our sprite to ask a letter and we keep track of how long it takes the player to press the correct letter.
We are going to be using some of the following scratch features:
- Use a list to store the letters that the player can be asked
- Make use of the timer blocks
- Use variables to store the user’s speed
First up, we need to set the scene up. We need a nice backdrop and a sprite that can ask the player questions.
Task: Add a sprite and a backdrop.
We next need to create a list of keys that we will ask our player to randomly select. To create a list, browse to the data tab and select the ‘Make a List’ button.
I’ve called my list alphabet and I’ve added all of the characters in the alphabet:
The first thing we want to do is hide this list when the player starts our program:
Giving Instructions to the Player
Our player is going to need some instructions on how to play our game, let’s use our sprite to help get the message across:
We can connect these to our green flag hat block because we want the sprite to talk as soon as the player starts the game.
Once the player has the instructions we always want to keep asking the player to press a key. If we always want to do something, we use a forever block. We want to also make the player have to wait a random amount of time before we ask them to type. This makes the game more challenging, they have to be quick!
We also want to pick a different letter to ask each time. We will need a new variable to store the random letter that we have picked out.
I’ve called mine: goalLetter.
I set the value of ‘goalLetter’ to a random letter from the alphabet list. Then I ask the player.
Test: Does it ask the player a different letter each time?
Adding a Timer
Our game is starting to become interesting, but we really need to keep track of how long it takes the player to press the correct key.
To help us keep time, we are going to use the timer blocks:
The first step is to reset the timer, this means that the next time we use the ‘timer’ block it will have the latest time elapsed values.
We also need a way to check to see if the player has pressed the correct key. For this we are going to create a variable called ‘pressedLetter’.
Now we check to see if the ‘pressedLetter’ is the same as the ‘goalLetter’, if they are we can stop the timer.
How do we set the ‘pressedLetter’ value?
At the moment the ‘pressedLetter’ variable is never set, so our code doesn’t work like we want. We need to add some code for each letter on the backdrop:
We continue this for all of the characters that are in our alphabet list.
Once the player has pressed the correct letter, we need to store the value of the timer.
If we want to store or keep something we need to use a variable. Let’s create another variable called ‘reactionTime’ this variable will store the timer’s value.
Now we have the time it took for the player to press the correct key. We can use our sprite to tell us how long we took: