Start a new, empty project at makecode.microbit.org
Drag radio set group
and radio send string
into on start
– change the group number to your pair number, and set the string to your name.
Add an on radio received receivedString
block, and drag a show string
block into it. Rather than showing a fixed string, we’re going to show the text that your microbit has received. Find the receivedString
variable in the Variables drawer, and drag it into show string
block.
Download the code onto your microbit, and get your partner to do the same. One of you try restarting your microbit with the button on the back. What happens? Now let your partner try!
It’s great that you can send your name to your partner, but it would be even more fun if you could surprise them with a different word each time.
Let’s change your code, so that you can send one of three words—rock, paper, or scissors— by pressing either the “A” button, “B” button, or both “A+B” buttons at the same time.
Remove the radio send string
block from your on start
block. We don’t need it any more.
Create a new variable called me
. We will use this to store our choice of word.
Create a new On button pressed
block, with a set to
block inside. Pick me
as the variable to set, and drag in the empty string block, from inside the “Advanced > Text” drawer. Type “rock” between the quote marks.
Finish your button pressed
block by dragging in a radio send string
block. Drag your me
variable, from the Variables drawer, into the end of the radio send string
block.
Now duplicate your entire button pressed
block twice, changing the buttons and the strings, like this:
Download the code onto your microbit, and get your partner to do the same. The first time each of you restarts your microbit, it will send your name to your partner, like before. But what happens when you press the “A” button, the “B” button, or both “A+B” at the same time?
That’s all fine, but to play Rock Paper Scissors, both players need to make their choice at the same time.
First, let’s show a countdown when the microbit is turned on. Use show number
blocks and pause (ms)
blocks, to display the numbers “3”, “2”, and “1”, with a 1 second (1000ms) pause between them.
At the end of our countdown, rather than showing the number “0”, we’ll show a picture that lets players know they can now make their choice. Use the show icon
block from the “Basic” drawer, and pick the empty square icon.
We want to display that icon for 1 second (1000ms) and then clear the screen. You can find the clear screen
block inside the “Basic > More” drawer.
At the moment, players can make their choice at any time during this countdown. We want to change it, so that choices can only be made when the square icon is displayed.
Create a variable called votingOpen
. Set votingOpen
to “1” before the square icon is displayed, and then set it to “0” after the screen has been cleared.
Finally, drag a radio send string
block from one of your button pressed
blocks, and put it at the end of the on start
block instead, after set votingOpen to 0
. Remove the radio send string
blocks from your other two button pressed
blocks.
Now add an if
block inside each of your button press blocks. Drag the votingOpen
variable into the if
condition, and then drag your set me to …
block into the then
segment.
Now your code should look like this:
Download the code onto your microbit, and get your partner to do the same. Notice how pressing the “A” or “B” buttons does nothing, until the square icon is shown and voting is opened. Then, a second later, voting closes, and your choice is sent to your partner’s microbit.
So, when you restart your microbit, a countdown displays. But there’s no countdown on your partner’s microbit, so they can’t play. Let’s fix that!
Add a radio send string
block just after the radio set group
, in your on start
block. And type in the word “start”.
Now, drag an if then else
block into your on radio received
block. Put the show string
block into the else
segment.
Find the 0 = 0
block from the “Logic” drawer and drag it into your if
condition. Then drag the receivedString
variable into the first part of the 0 = 0
block, and drag an empty string block from the “Advanced > Text” drawer into the second part. Type “start” into the empty text block, like this:
Now, finally, go back to your on start
block, duplicate all the blocks from show number 3
down to the end, and drag them over into the then
part of your if then else
block. Your code should look like this:
Download the code onto your microbit, and get your partner to do the same. While you are both downloding the code onto your microbits, they might start counting down. Just wait a few seconds for both microbits to go quiet, before testing them out.
To test them out, one of you should start a game by pressing the power button on the back on your microbit. Notice how both microbits show the same countdown at the same time. What happens when you both press “A”, “B”, or “A+B” together when the square icon is shown?
You will have noticed that, when you choose “rock”, “paper”, or “scissors”, all that happens is your choice gets displayed on your partner’s screen.
Let’s program each microbit to compare your vote and your partner’s vote, to show who has won.
First we need to add some else if
segments to the if then else
block inside your on radio received
block. You can add more else if
segments by clicking the little blue gear icon, and dragging else if
blocks into the stack. Add 3 else if
blocks, and remove the else
block:
Click on the little blue gear icon again to close the balloon.
Drag a 0 = 0
block from the “Logic” drawer into the first empty else if
condition. Drag the receivedString
variable into the first part of the 0 = 0
condition, and drag an empty string block (from the “Advanced > Text” drawer) into the second part. Type “rock” into the empty string block, like this:
Now do the same for receivedString = paper
and receivedString = scissors
in the other two else if
conditions:
Go to your first receivedString = rock
condition, and add another else if
conditions. Use the 0 = 0
block, empty string block, and the me
variable, to set up 3 conditions like this:
Now draw faces inside each of the three “then” segments:
Now do the same again for each of the remaining two receivedString
conditions. This table might help you work out who wins and who loses in each case:
receivedString | me | (result) |
---|---|---|
rock | rock | It’s a draw |
paper | I win | |
scissors | I lose | |
paper | rock | I lose |
paper | It’s a draw | |
scissors | I win | |
scissors | rock | I win |
paper | I lose | |
scissors | It’s a draw |
Your finished code should look like this:
Finally, before we test the finished game out, we need to handle two edge-cases that might sometimes happen.
The first is that a player might forget to press a button during the voting period. This will cause problems later on, so instead, we set me
to a default choice of “rock” before the countdown starts.
Add a “set me to” block before radio send string start
, and set the string to “rock”:
Second, there’s a chance that two microbits restarted at exactly the same time can end up broadcasting over each other, causing confusion at voting time. To avoid this, we can add a tiny delay on the microbit receiving the start
command.
Add a pause (ms)
block before the radio send string me
in the on radio received
block, and set the pause duration to “50” ms:
Download the code onto your microbit, and get your partner to do the same. Remember to wait until both microbits have restarted and gone quiet, and then one of you can start a game by restarting one of your microbits. See who can win the most games in a row!