Rock-Paper-Scissors

from microbit import *
import random

while True:
    if accelerometer.was_gesture("shake"):
        choice = random.randint(0, 2)
        if choice == 0:
            display.show(Image.SQUARE_SMALL)
        elif choice == 1:
            display.show(Image.SQUARE)
        else:
            display.show(Image.SCISSORS)

  • Run an endless loop.
  • Check for the "shake" gesture on every iteration.
  • When detected, pick a random number between 0 and 2.
  • Depending on the number display an image for "rock", "paper", or "scissors".

Key concepts:

  • Random numbers
  • The Micro:bit accelerometer and "gestures"
  • Displaying images

Clap, Rock-Paper-Scissors

A version of the program, by Cass and Rada that reacts to claps (or other loud noises) instead of the shake gesture.

from microbit import *
import random

lightsOn = False

while True:
    if microphone.was_event(SoundEvent.LOUD):
        lightsOn = not lightsOn
        if lightsOn:
            choice = random.randint(0, 2)
            if choice == 0:
                display.show(Image.SQUARE_SMALL)
            elif choice == 1:
                display.show(Image.SQUARE)
            else:
                display.show(Image.SCISSORS)
        else:
            display.clear()
    sleep(100)