BBC micro:bit

Shake Dice

Turn the micro:bit into a dice: shake it, watch it roll, and see where it lands.

Day 3 45 to 60 minutes 6 steps Key Stage 3
Start step 1
The dice has landed on five.

Board games need dice, and your micro:bit can be one. It feels you shake it, spins through a few faces, then lands on a random number with a ding. Fair, fast and impossible to lose under the sofa.

You will learn

  • Sense movement with the accelerometer
  • Pick random numbers with random.randint()
  • Keep pictures in a list and pick one by its number
  • Animate with a for loop

You will need

  • A computer with Chrome or Edge
  • A micro:bit and a USB data cable, or the simulator
  • Optional: a battery pack, so the dice works away from the computer

Words to know

accelerometer
A sensor that feels movement, tilting and shaking.
random
Unpredictable. random.randint(1, 6) could be any whole number from 1 to 6.
list
Many values in one variable. The first item is number 0.
animation
Pictures shown quickly one after another, so they seem to move.

Step by step

  1. Step 1: Feel a shake

    Start a new project called Shake Dice. The accelerometer senses movement. accelerometer.was_gesture("shake") is True if the micro:bit has been shaken since the last time you asked. In the simulator, choose shake under Accelerometer and send the gesture.

    main.pyMicroPython micro:bit editor
    from microbit import * while True:    if accelerometer.was_gesture("shake"):        display.show(Image.SURPRISED)    sleep(100)
    Send a shake: the micro:bit looks surprised.
  2. Step 2: Roll a number

    import random brings in Python's random numbers, and random.randint(1, 6) picks a whole number from 1 to 6, just like a dice. Show it instead of the face.

    New: lines 2, 6 and 7.

    main.pyMicroPython micro:bit editor
    from microbit import *import random while True:    if accelerometer.was_gesture("shake"):        number = random.randint(1, 6)        display.show(number)    sleep(100)
    Each shake shows a number from 1 to 6.
  3. Step 3: Real dice faces

    Dice show dots, not digits. Put six pictures of dots in a list called faces. Lists count from 0, so the face for 1 is faces[0], and the face for any number is faces[number - 1].

    New: lines 3 to 11 and 16.

    main.pyMicroPython micro:bit editor
    from microbit import *import random faces = [    Image("00000:00000:00900:00000:00000"),    Image("90000:00000:00000:00000:00009"),    Image("90000:00000:00900:00000:00009"),    Image("90009:00000:00000:00000:90009"),    Image("90009:00000:00900:00000:90009"),    Image("90009:00000:90009:00000:90009"),] while True:    if accelerometer.was_gesture("shake"):        number = random.randint(1, 6)        display.show(faces[number - 1])    sleep(100)
    Now the dice shows dots.
  4. Step 4: Make it roll

    Before the real number, flash eight random faces, 80 milliseconds each, so the dice looks as if it is tumbling. random.choice(faces) picks any item from the list.

    New: lines 15 to 17.

    main.pyMicroPython micro:bit editor
    from microbit import *import random faces = [    Image("00000:00000:00900:00000:00000"),    Image("90000:00000:00000:00000:00009"),    Image("90000:00000:00900:00000:00009"),    Image("90009:00000:00000:00000:90009"),    Image("90009:00000:00900:00000:90009"),    Image("90009:00000:90009:00000:90009"),] while True:    if accelerometer.was_gesture("shake"):        for i in range(8):            display.show(random.choice(faces))            sleep(80)        number = random.randint(1, 6)        display.show(faces[number - 1])    sleep(100)
    Shake, and the faces tumble before the dice lands.
  5. Step 5: Ready, roll, ding!

    Show a small square while the dice waits to be shaken, and play a ding when it lands. import music brings in the micro:bit's sounds.

    New: lines 3, 14, 15 and 23.

    main.pyMicroPython micro:bit editor
    from microbit import *import randomimport music faces = [    Image("00000:00000:00900:00000:00000"),    Image("90000:00000:00000:00000:00009"),    Image("90000:00000:00900:00000:00009"),    Image("90009:00000:00000:00000:90009"),    Image("90009:00000:00900:00000:90009"),    Image("90009:00000:90009:00000:90009"),] display.show(Image.SQUARE_SMALL) while True:    if accelerometer.was_gesture("shake"):        for i in range(8):            display.show(random.choice(faces))            sleep(80)        number = random.randint(1, 6)        display.show(faces[number - 1])        music.play(music.BA_DING)    sleep(100)
    It lands, with a ding.
    Tip A micro:bit V2 has a speaker built in. A V1 needs headphones or a small speaker clipped to pin 0 and GND.
  6. Step 6: Roll the real thing

    Send it to your micro:bit and play a game with a friend. With a battery pack, the dice works away from the computer.

    Send it, then shake your real micro:bit.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Rock, paper, scissors

    Make a shake pick rock, paper or scissors, each as a picture you design.

    Show a hint

    Make three Images and put them in a list. On a shake, show random.choice() of the list.

    Show one answer
    main.pyMicroPython micro:bit editor
    from microbit import *import random rock = Image("00000:09990:99999:09990:00000")paper = Image("99999:90009:90009:90009:99999")scissors = Image("99009:99090:00900:99090:99009")hands = [rock, paper, scissors] while True:    if accelerometer.was_gesture("shake"):        display.show(random.choice(hands))    sleep(100)
  2. Two dice

    Roll two numbers and scroll their total.

    Show a hint

    Roll a and b with two calls to random.randint(1, 6), then display.scroll(a + b).

  3. Secret cheat

    If button A is held down during the shake, always roll a 6. Do not tell anyone!

    Show a hint

    Before you show the face: if button_a.is_pressed(): then number = 6.

The finished code

Show the whole program
main.pyMicroPython micro:bit editor
from microbit import *import randomimport music faces = [    Image("00000:00000:00900:00000:00000"),    Image("90000:00000:00000:00000:00009"),    Image("90000:00000:00900:00000:00009"),    Image("90009:00000:00000:00000:90009"),    Image("90009:00000:00900:00000:90009"),    Image("90009:00000:90009:00000:90009"),] display.show(Image.SQUARE_SMALL) while True:    if accelerometer.was_gesture("shake"):        for i in range(8):            display.show(random.choice(faces))            sleep(80)        number = random.randint(1, 6)        display.show(faces[number - 1])        music.play(music.BA_DING)    sleep(100)
Notes for the teacher

Timing

  1. 15 minutes: steps 1 and 2.
  2. 20 minutes: steps 3 and 4. The list index (number - 1) is the tricky idea: write the list on the board with its numbers 0 to 5 underneath.
  3. 15 minutes: step 5, sending, the tests, and a quick game.

Mistakes to look out for

  1. faces[number] instead of faces[number - 1]: a 6 crashes with IndexError, and a 1 shows two dots.
  2. A missing comma between the pictures in the list.
  3. import random forgotten: a NameError.
  4. No sound on a V1 micro:bit, which has no speaker.

Going further

Is the dice fair? Roll 60 times and tally the results as a class. Then compare with what probability says to expect, about 10 of each.