BBC micro:bit
Shake Dice
Turn the micro:bit into a dice: shake it, watch it roll, and see where it lands.
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
forloop
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
Step 1: Feel a shake
Start a new project called Shake Dice. The accelerometer senses movement.
accelerometer.was_gesture("shake")isTrueif the micro:bit has been shaken since the last time you asked. In the simulator, choose shake under Accelerometer and send the gesture.1from microbit import *2 3while True:4 if accelerometer.was_gesture("shake"):5 display.show(Image.SURPRISED)6 sleep(100)ReferenceIdeasAPIProjectShake Dice1from microbit import *2 3while True:4 if accelerometer.was_gesture("shake"):5 display.show(Image.SURPRISED)6 sleep(100)Send to micro:bitSaveOpen…SimulatorAccelerometershakeSend gestureSend a shake: the micro:bit looks surprised. Step 2: Roll a number
import randombrings in Python's random numbers, andrandom.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.
1from microbit import *2import random3 4while True:5 if accelerometer.was_gesture("shake"):6 number = random.randint(1, 6)7 display.show(number)8 sleep(100)ReferenceIdeasAPIProjectShake Dice1from microbit import *2import random3 4while True:5 if accelerometer.was_gesture("shake"):6 number = random.randint(1, 6)7 display.show(number)8 sleep(100)Send to micro:bitSaveOpen…SimulatorAccelerometershakeSend gestureEach shake shows a number from 1 to 6. 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 isfaces[0], and the face for anynumberisfaces[number - 1].New: lines 3 to 11 and 16.
1from microbit import *2import random3 4faces = [5 Image("00000:00000:00900:00000:00000"),6 Image("90000:00000:00000:00000:00009"),7 Image("90000:00000:00900:00000:00009"),8 Image("90009:00000:00000:00000:90009"),9 Image("90009:00000:00900:00000:90009"),10 Image("90009:00000:90009:00000:90009"),11]12 13while True:14 if accelerometer.was_gesture("shake"):15 number = random.randint(1, 6)16 display.show(faces[number - 1])17 sleep(100)ReferenceIdeasAPIProjectShake Dice1from microbit import *2import random3 4faces = [5 Image("00000:00000:00900:00000:00000"),6 Image("90000:00000:00000:00000:00009"),7 Image("90000:00000:00900:00000:00009"),8 Image("90009:00000:00000:00000:90009"),9 Image("90009:00000:00900:00000:90009"),10 Image("90009:00000:90009:00000:90009"),11]12 13while True:Send to micro:bitSaveOpen…SimulatorAccelerometershakeSend gestureNow the dice shows dots. 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.
1from microbit import *2import random3 4faces = [5 Image("00000:00000:00900:00000:00000"),6 Image("90000:00000:00000:00000:00009"),7 Image("90000:00000:00900:00000:00009"),8 Image("90009:00000:00000:00000:90009"),9 Image("90009:00000:00900:00000:90009"),10 Image("90009:00000:90009:00000:90009"),11]12 13while True:14 if accelerometer.was_gesture("shake"):15 for i in range(8):16 display.show(random.choice(faces))17 sleep(80)18 number = random.randint(1, 6)19 display.show(faces[number - 1])20 sleep(100)ReferenceIdeasAPIProjectShake Dice6 Image("90000:00000:00000:00000:00009"),7 Image("90000:00000:00900:00000:00009"),8 Image("90009:00000:00000:00000:90009"),9 Image("90009:00000:00900:00000:90009"),10 Image("90009:00000:90009:00000:90009"),11]12 13while True:14 if accelerometer.was_gesture("shake"):15 for i in range(8):16 display.show(random.choice(faces))17 sleep(80)18 number = random.randint(1, 6)Send to micro:bitSaveOpen…SimulatorAccelerometershakeSend gestureShake, and the faces tumble before the dice lands. Step 5: Ready, roll, ding!
Show a small square while the dice waits to be shaken, and play a ding when it lands.
import musicbrings in the micro:bit's sounds.New: lines 3, 14, 15 and 23.
1from microbit import *2import random3import music4 5faces = [6 Image("00000:00000:00900:00000:00000"),7 Image("90000:00000:00000:00000:00009"),8 Image("90000:00000:00900:00000:00009"),9 Image("90009:00000:00000:00000:90009"),10 Image("90009:00000:00900:00000:90009"),11 Image("90009:00000:90009:00000:90009"),12]13 14display.show(Image.SQUARE_SMALL)15 16while True:17 if accelerometer.was_gesture("shake"):18 for i in range(8):19 display.show(random.choice(faces))20 sleep(80)21 number = random.randint(1, 6)22 display.show(faces[number - 1])23 music.play(music.BA_DING)24 sleep(100)ReferenceIdeasAPIProjectShake Dice1from microbit import *2import random3import music4 5faces = [6 Image("00000:00000:00900:00000:00000"),7 Image("90000:00000:00000:00000:00009"),8 Image("90000:00000:00900:00000:00009"),9 Image("90009:00000:00000:00000:90009"),10 Image("90009:00000:00900:00000:90009"),11 Image("90009:00000:90009:00000:90009"),12]13Send to micro:bitSaveOpen…SimulatorAccelerometershakeSend gestureIt 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.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.
ReferenceIdeasAPIProjectShake Dice1from microbit import *2import random3import music4 5faces = [6 Image("00000:00000:00900:00000:00000"),7 Image("90000:00000:00000:00000:00009"),8 Image("90000:00000:00900:00000:00009"),9 Image("90009:00000:00000:00000:90009"),10 Image("90009:00000:00900:00000:90009"),11 Image("90009:00000:90009:00000:90009"),12]13Send to micro:bitSaveOpen…SimulatorSend 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.
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, showrandom.choice()of the list.Show one answer
1from microbit import *2import random3 4rock = Image("00000:09990:99999:09990:00000")5paper = Image("99999:90009:90009:90009:99999")6scissors = Image("99009:99090:00900:99090:99009")7hands = [rock, paper, scissors]8 9while True:10 if accelerometer.was_gesture("shake"):11 display.show(random.choice(hands))12 sleep(100)Two dice
Roll two numbers and scroll their total.
Show a hint
Roll
aandbwith two calls torandom.randint(1, 6), thendisplay.scroll(a + b).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():thennumber = 6.
The finished code
Show the whole program
1from microbit import *2import random3import music4 5faces = [6 Image("00000:00000:00900:00000:00000"),7 Image("90000:00000:00000:00000:00009"),8 Image("90000:00000:00900:00000:00009"),9 Image("90009:00000:00000:00000:90009"),10 Image("90009:00000:00900:00000:90009"),11 Image("90009:00000:90009:00000:90009"),12]13 14display.show(Image.SQUARE_SMALL)15 16while True:17 if accelerometer.was_gesture("shake"):18 for i in range(8):19 display.show(random.choice(faces))20 sleep(80)21 number = random.randint(1, 6)22 display.show(faces[number - 1])23 music.play(music.BA_DING)24 sleep(100)Notes for the teacher
Timing
- 15 minutes: steps 1 and 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. - 15 minutes: step 5, sending, the tests, and a quick game.
Mistakes to look out for
faces[number]instead offaces[number - 1]: a 6 crashes with IndexError, and a 1 shows two dots.- A missing comma between the pictures in the list.
import randomforgotten: a NameError.- 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.