You are going to create a 2-player game to see who has the fastest reactions. The game will work by showing an image after a random amount of time - whoever presses their button first is the winner.
Instructions: If you’re reading this online, wait for an image to display on the micro:bit below and then press A or B first to see who is the fastest.
Step 1: Wait for it!
Let’s start by displaying an image after a random amount of time.
Go to jumpto.cc/pxt-new to start a new project in the MakeCode (PXT) editor. Call your new project ‘Reaction’.
Before displaying an image, the game should wait for a random amount of time.
pause block into the
forever block and change the pause time to 1000 ms:
- Add another
pauseblock and then drag a
pick randomblock to
pauseblock and set its value to 4000:
Remember that 1000ms is 1 second, so there will be a pause of at least 1 second up to a maximum of 5 seconds (1000 + 4000 ms).
You can change the ‘1000’ and ‘4000’ numbers to change the minimum and maximum pause if you like.
- After waiting, your game should show an image so that players know when to press their button.
Click ‘run’ to test your project. You should see your image appear after a random delay.
Add code at the end of the
foreverloop to display your image for 1 second and then clear the display.
- Test your project. You should see your image appear randomly and then disappear.
Challenge: Choose your own image
Can you change the image that’s displayed?
Challenge: Choose your own delay
Change the numbers in your
random block. You can speed up your game to make it harder, or slow it down to add suspense!
Step 2: Waiting for a winner
Let’s add code to wait until a button is pressed.
- After displaying an image, you’ll need to wait until someone presses their button.
Another way of saying this is that you’ll need to wait as long as button A and button B have not been pressed.
To do this, add a
while loop from the ‘Control’ section. The
while loop should be added in just after the
- Drag an
andblock from ‘Logic’ to your
- Drag a
notfrom ‘Logic’ into the left hand side of the
- Drag an
button A is pressedblock from ‘Input’ to after the
- Repeat the 2 steps above to add
not button B is pressedinto the
rightside of your while loop.
- You can then add a very short (20ms) delay, so that your
whileloop waits as long as a button hasn’t been pressed.
- Test your project. Your game should now display an image and then wait as long as buttons A and B have not been pressed.
Step 3: Who is the fastest?
Let’s find out who pressed their button first.
- If button A was pressed, we want to point to player A. To do this, add an
ifblock after your
whileloop, and replace
button A is pressed.
- You can then use the
show ledsblock to show an arrow pointing to player A.
- You should also do the same for button B.
Challenge: Keep score
Can you use two variables called
playerB to keep track of each player’s score?
You’ll need to set both scores to 0 at the start of the game by placing code inside the ‘on start’ block.
And add 1 to whichever player wins each round.
You’ll also need to think of a way to display the score.