Rock Paper Scissors

We are all familiar with the game Rock Paper Scissors. Let's take a look at how we can code our micro:bits to play

What You Will Learn

  • How to create and use a variable.
  • How to use the micro:bit on shake function.
  • How to display images/shapes on the LED matrix.
  • How to use conditional if/else statements.
  • How to use comparison operators.
  • How to randomise choices.

What You Will Need

  • 1 x micro:bit
  • 1 x micro USB cable
  • 1 x battery pack for the micro:bit (optional)

Coding

  1. Open your favourite browser and go to app.edublocks.org and press enter.
  2. Click on micro:bit to open the micro:bit coding editor.
  3. Click on Basic. Click and drag a from microbit import * block to the code area and drop it.
  4. Click on Basic. Click and drag an import random block to the code area and attach it under the from microbit import * block.
  5. Click on Basic. Click and drag a while True: block to the code area and attach it under the import random block.
  6. Click on Display. Click and drag an image = block to the code area and attach it within the while True: block.
  7. Create the following image to represent Paper by typing 9 (this represents the brightness of the LEDs) in the corresponding squares.
    Representation of Paper
    Click on the drop-down arrow next to image and click New variable and name it Paper.
  8. Follow step 6 this time attaching the block under Paper.
  9. Create the following image to represent Rock by typing 9 within the corresponding squares.
    Representation of Rock
    Click on the arrow next to image and click New variable and name it Rock.
  10. Follow step 6 this time attaching the block below Rock.
  11. Create the following image to represent Scissors by typing 9 within the corresponding boxes.
    Representation of Scissors
    Click on the arrow next to image and click New variable and name it Scissors. Your code should look like this:
    Your code so far
  12. Click on Basic. Click and drag an if True: block to the code area and attach it under Scissors.
  13. Click on Accelerometer. Click and drag an accelerometer.was_gesture(‘shake’) block to the code area and attach it within the True of the if block.

    What is a variable

    Think of a variable as a box that stores information that can be used throughout our program. We give variables a descriptive name so we and others can understand what is going on within our program.

  14. Click on Variables. Click create variable and name it choice. Click OK.
  15. Click on Variables. Click and drag a choice = block to the code area and attach it within the if accelerometer.was_gesture(‘shake’) block. Within the blank space type random.randint(0,2).
  16. Click on Basic. Click and drag an if True: block to the code area and attach it below choice = random.randint(0,2) block.
  17. Click on Basic. Click and drag a 0 == 0 block to the code area and attach it within the True of the if block.
  18. Click on Variables. Click and drag a choice block to the code area and attach it within the first 0 of the if block.
  19. Click on Display. Click and drag a display.show(Image.HAPPY) block to the code area and attach it within the if choice == 0: block.
  20. Click on Variables. Click and drag the Paper block to the code area and attach it within Image.HAPPY. Your code should look like this:
    Your code so far. Getting there
  21. Click on Basic. Click and drag an elif block to the code area and attach it below the if choice == 0: block.
  22. Right-click on choice == 0 and click duplicate. Attach the duplicated block within the True of the elif block. Click on 0 and type 1.
  23. Right-click on the display.show(Paper) block and click duplicate. Attach the duplicated block within elif choice == 1: block. Click on Paper and choose Rock. Your code should look like this:
    Your code so far. Nearly Finished
  24. Click on Basic. Click and drag an else: block to the code area and attach it under the elif choice == 1: block.
  25. Right-click on display.show(Rock) and click duplicate. Attach the duplicated block within the else: block. Click on Rock and choose Scissors. Completed Code:
    Completed Code

We have now finished coding rock paper scissors the next step is to download our code to the miro:bit.

Downloading the Code

  1. Take the micro USB cable and connect the micro:bit to the computer.
  2. Click on Download Hex.
  3. Locate your micro:bit within the file explorer. Click on it to open the drive and click on Save. Once the light on the back of the micro:bit stops blinking your code has finished downloading.

You have now successfully coded a micro:bit to play raock paper scissors.

Find a partner and start playing.