Rock Paper Scissors - Micro:bit
by Jayden Lee in Circuits > Wireless
1630 Views, 5 Favorites, 0 Comments
Rock Paper Scissors - Micro:bit
The goal of this project is to implement a game of Rock Paper Scissors between two Micro:bits.
Supplies
- Two Micro:Bits and accompanying wires (Optional)
- Internet access
Creating Images
The first step to creating Rock Paper Scissors is to create the images that will be displayed during the game.
Hand Selection
The next step is to allow the user to choose which option they want to use. We can do this by cycling through the possible options using the A button. As button A is pressed, we need to keep track of which image needs to be shown. This can be done using a variable we will name next_image. next_image starts off a 0 but increments modulo 5 each time button A is pressed. The current hand selection is represented by the me variable.
Sending the Selection
The next step is to send the selection we have just made to the other micro:bit. To ensure that this is only sent once per round, we can change the selected variable to true. As this is only reset to false when the game is started again, the value can only be sent once.
Receiving the Selection
Next, we need to receive the opponent's hand selection over the radio. To indicate that this value has been received, we set the received variable to true. This value is reset to false each time the game is played.
Playing the Game
This step is the most important, as it defines Rock, Paper, Scissors. Once both selected and received are set to true, we know that we are ready to start the logic section. It works by first displaying what selection we received from the opponent. If this value is equivalent to the value we selected, we display a surprised face to indicate a tie.
Then we check if our value is the corresponding "winning" value compared to the opponent's value. If this is the case, we display a happy face. If this is not the case, it means that we have lost, and we display a sad face accordingly.
Resetting the Game
At the beginning of the game and when the game is played, all the necessary variables need to be returned to their correct beginning states. This can be done with an init function that resets the selected, received, next_image and me variables. It also shows a waiting image to inform the user of the games current state.
Completion
And we're done! That's all you need to do to implement Rock Paper Scissors onto two Micro:bits. Although please be mindful that the program can occasionally get stuck in a certain state due to packet loss while transmitting. This can be easily fixed with a hard reset of the game.