Random Dice

from microbit import *
import random

# A list of all dice images
dice = [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.HAPPY)

while True:
    if accelerometer.was_gesture('shake'):
        display.show(dice, loop=True, wait=False, delay=100)
        sleep(4000)
        display.show(random.choice(dice))                     
    sleep(10)

This is similar to the 8-Ball example also on the website.

A list of images is given to the microbit. When it is shaken, the microbit uses random.choice to pick an item from the list.

Code Explained

There are two main elements to this script: representing images as strings and picking a random item from a list.

Using Images

Image class

The microbit has the Image class which contains many images, such as Image.HAPPY and Image.SAD. These are simply string representations of the images. We can connect to the microbit over REPL and see these strings:

>>> print(Image.HAPPY)
Image(
    '00000:'
    '09090:'
    '00000:'
    '90009:'
    '09990:'
)
>>>

0 is off whereas 9 is the LED lit at full intensity.

Making Custom Images

It is possible for use to then draw our own images on the display. Here add_sign variable is a representation of an addition sign.

add_sign = Image("00900:"
                 "00900:"
                 "99999:"
                 "00900:"
                 "00900")

Formatting Image Strings

There are many ways of representing this string. With a colon (as above) or with a new line (\n):

add_sign = Image("00900\n"
                 "00900\n"
                 "99999\n"
                 "00900\n"
                 "00900")

It is also possible to keep the image representation all on one line:

Image("00900:00900:99999:00900:00900")

Lists of Images

In this program, all six dice faces are in a list called dice. There are six items in the list in total.

The list begins and ends with a [ ] square brackets. Each item in the list is separated with a comma.

dice = [
        Image("00000:00000:00900:00000:00000"), # 1
        Image("90000:00000:00000:00000:00009"), # 2
        Image("90000:00000:00900:00000:00009"), # 3
        Image("90009:00000:00000:00000:90009"), # 4
        Image("90009:00000:00900:00000:90009"), # 5
        Image("90009:00000:90009:00000:90009")  # 6
        ]

This is exactly the same as the main example above except the image strings are all on one line.

Logic

Was it Shaken?

if accelerometer.was_gesture("shake") is True:

.was_gesture(gesture_name) returns True or False if gesture_name was the most recently detected gesture. .was_gesture("shake") returns True if it was shaken.

Pick Random Item

The dice list has 6 items. random.choice is used on the list to return a random item:

display.show(random.choice(dice))

Using random requires the random module to be imported. This is done at the beginning of the program:

from microbit import *
import random
Share On
Facebook Twitter Google Plus

Published: January 18, 2017 Updated: January 18, 2017

Random Dice

Make a dice to display a random image each time the microbit is shaken.