Home CodePython Micro:bit and dice simulator in python

Micro:bit and dice simulator in python

by shedboy71

This is a fairly simple dice throwing game for the micro:bit, in this example you shake the micro:bit and a dice image will be displayed on the LEDs

The example is written in python, I used Mu.

Parts List

 Micro:bit Development Board

Code

Fairly simple to understand the first part is all to do with creating the dice images, we then generate a random number and then show the necessary image

[codesyntax lang=”python”]

from microbit import *
import random

one = Image("00000:"
            "00000:"
            "00900:"
            "00000:"
            "00000")

two = Image("00000:"
            "09000:"
            "00000:"
            "00090:"
            "00000")         

three = Image("90000:"
              "00000:"
              "00900:"
              "00000:"
              "00009")            

four = Image("00000:"
             "09090:"
             "00000:"
             "09090:"
             "00000")

five = Image("00000:"
             "09090:"
             "00900:"
             "09090:"
             "00000")           

six = Image("09090:"
            "00000:"
            "09090:"
            "00000:"
            "09090")

display.scroll('Shake to play')

while True:
    if accelerometer.was_gesture('shake'):
        throw = random.randint(1, 6)
        if throw == 1:
            display.show(one)
        if throw == 2:
            display.show(two)
        if throw == 3:
            display.show(three)
        if throw == 4:
            display.show(four)
        if throw == 5:
            display.show(five)
        if throw == 6:
            display.show(six)

[/codesyntax]

You may also like

Leave a Comment