In this assignment you will program your micro:bit to simulate a single die cube. When you shake your micro:bit it will generate a random pattern of dots from 1 to 6. You may find slides 58 - 69 of the slide presentation helpful in completing this assignment. Here is a simple program that displays an image of a confused face for 1 second when the micro:bit is shaken:
from microbit import *
import random
while True:
display.clear()
if accelerometer.was_gesture('shake'):
display.show(Image.CONFUSED)
sleep(1000)
- Your program must use at least 6 images that you create to show the number of dots that were rolled
- Your program must use the Python
randint()
function - Your program will need at least one variable to store the random number of dots
- Your program will need a number of
if
statements to select the right image based on the number in the variable - Submit both the Python code and an animated gif showing your program running to Google Classroom
Python has a module (part of a larger collection of code called a "library") with a number of useful functions for random numbers. To use this module you need to add import random
at the top of the program (see the sample code with a confused face above). For this assignment, we'll use the randint()
function which takes two arguments:
random.randint(a, b)
It return a random integer between a and b inclusive.
- Start with the sample program above
- Between lines 10 and 11 create a variable
dots
and initialize it to a random number between 1 and 6. Make sure the code is indented so that it "belongs" to theif accelerometer.was_gesture('shake'):
- Replace
display.show(Image.confused)
with anif
statement that checks to see ifdots
is equal to 1. Underneath, display an image with one dot. Be sure to indent thedisplay
so that it belongs to theif
statement. - Add 5 more if statements for the numbers 2 - 6
You can add animations to make "rolling the dice" more dramatic. You could also simulate dice with different numbers of faces like the dice used in Dungeons and Dragons.
Yangyang
Allison
Jordan
Cali
Emerson
Sandy
Naomi
Cilla
Isaac
Stephanie
Linen
Jasmine