Lesson 2: Rock Paper Scissors
To Know - Pseudocode standards.
Selection, iteration and random number generation program structures.
To Be Able To - Design a program using pseudocode standards.
Utilise programming structures to develop a program.
Step 1: Make It
Play this classic game with two micro:bits and learn about selection, variables and random numbers at the same time.
Rock, paper, scissors is a classic game of chance for two people. You and a partner shake your fists 3 times and then make gestures at random to show a rock, paper or scissors. Rock beats scissors, scissors beat paper and paper beats rock (it wraps the rock!).
When the micro:bit accelerometer detects a shake movement, it sets the variable tool to a random number: 0, 1 or 2.
We use 0 because computers start counting at 0, and it’s good to remember that 0 is a number!
The program uses selection to decide what image to show on the LED display. If the random number was 0, it shows a rock icon, if it was 1 it shows the icon representing paper. If it wasn’t 0 or 1, it must be 2 because we instructed the micro:bit to only pick random numbers between 0 and 2, so in that case it shows scissors.
In pairs, work through the worksheet attached to breakdown the program.
Use the description above to find your answers.
Step 2: Code It
Function - This line imports all the micro:bit libraries, which contain tools for using its buttons, screen, and sensors.
Python Code - from microbit import *
Function - The second line imports the random library, which helps us create random numbers. We’ll use this to make the program choose between different images.
Python Code - import random
Function - This starts a loop that will keep running as long as the micro:bit is on. It lets us continuously check if a shake gesture is detected.
Python Code - while True:
Function - This line checks if the micro:bit was shaken.
Python Code - if accelerometer.was_gesture('shake'):
Function - This line picks a random number between 0 and 2 and stores it in a variable called tool. Each number will match one of three possible images.
Python Code - tool = random.randint(0,2)
Function - This line uses the random number stored in tool to show a rock image when equal to 0.
Python Code -
if tool == 0:
display.show(Image.SQUARE_SMALL)
Function - This line uses the random number stored in tool to show a paper image when equal to 1.
Python Code -
elif tool == 1:
display.show(Image.SQUARE)
Function - This line uses the random number stored in tool to show a scissors image when equal to 2.
Python Code -
else:
display.show(Image.SCISSORS)
Step 3: Improve It
Improve Tool Image Quality
Create your own custom images to represent ROCK and PAPER, to make it a complete "Rock-Paper-Scissors" game.
Add More Tool Images
Add new tools to your game to add variety. An example of this is Rock, Paper, Scissors, Lizard, Spock.
Play Sounds for Each Tool
Add sounds to make each tool unique when displayed. For example, you could add a Music.BA_DING sound for one tool and Music.JUMP_DOWN for another. This would add a fun audio effect to the shake action.
Count and Display the Number of Times Each Tool is Shown
Keep track of how many times each tool (shape) appears. This can be done by adding counters for each tool and showing the count on the display when a button is pressed.