Getting started
Click here for the Python code editor
Built in images
The BBC Microbit has images saved on it. To access these images you have to use the correct syntax e.g. Image.HEART. Below is the code to display an image and a list of all the built in images (use the code below but change the image):
Image.HEART
Image.HEART_SMALL
Image.HAPPY
Image.SMILE
Image.SAD
Image.CONFUSED
Image.ANGRY
Image.ASLEEP
Image.SURPRISED
Image.SILLY
Image.FABULOUS
Image.MEH
Image.YES
Image.NO
Image.TRIANGLE
Image.TRIANGLE_LEFT
Image.CHESSBOARD
Image.DIAMOND
Image.DIAMOND_SMALL
Image.SQUARE
Image.SQUARE_SMALL
Image.RABBIT
Image.COW
Image.MUSIC_CROTCHET
Image.MUSIC_QUAVER
Image.MUSIC_QUAVERS
Image.PITCHFORK
Image.XMAS
Image.PACMAN
Image.TARGET
Image.TSHIRT
Image.ROLLERSKATE
Image.DUCK
Image.HOUSE
Image.TORTOISE
Image.BUTTERFLY
Image.STICKFIGURE
Image.GHOST
Image.SWORD
Image.GIRAFFE
Image.SKULL
Image.UMBRELLA
Image.SNAKE
Image.CLOCK12, Image.CLOCK11, Image.CLOCK10, Image.CLOCK9, Image.CLOCK8, Image.CLOCK7, Image.CLOCK6, Image.CLOCK5, Image.CLOCK4, Image.CLOCK3, Image.CLOCK2, Image.CLOCK1
Image.ARROW_N, Image.ARROW_NE, Image.ARROW_E, Image.ARROW_SE, Image.ARROW_S, Image.ARROW_SW, Image.ARROW_W, Image.ARROW_NW
Image.HEART_SMALL
Image.HAPPY
Image.SMILE
Image.SAD
Image.CONFUSED
Image.ANGRY
Image.ASLEEP
Image.SURPRISED
Image.SILLY
Image.FABULOUS
Image.MEH
Image.YES
Image.NO
Image.TRIANGLE
Image.TRIANGLE_LEFT
Image.CHESSBOARD
Image.DIAMOND
Image.DIAMOND_SMALL
Image.SQUARE
Image.SQUARE_SMALL
Image.RABBIT
Image.COW
Image.MUSIC_CROTCHET
Image.MUSIC_QUAVER
Image.MUSIC_QUAVERS
Image.PITCHFORK
Image.XMAS
Image.PACMAN
Image.TARGET
Image.TSHIRT
Image.ROLLERSKATE
Image.DUCK
Image.HOUSE
Image.TORTOISE
Image.BUTTERFLY
Image.STICKFIGURE
Image.GHOST
Image.SWORD
Image.GIRAFFE
Image.SKULL
Image.UMBRELLA
Image.SNAKE
Image.CLOCK12, Image.CLOCK11, Image.CLOCK10, Image.CLOCK9, Image.CLOCK8, Image.CLOCK7, Image.CLOCK6, Image.CLOCK5, Image.CLOCK4, Image.CLOCK3, Image.CLOCK2, Image.CLOCK1
Image.ARROW_N, Image.ARROW_NE, Image.ARROW_E, Image.ARROW_SE, Image.ARROW_S, Image.ARROW_SW, Image.ARROW_W, Image.ARROW_NW
Coding the Microbit buttons
Use the following code and see what happens
Change the images using the built in images
Random
Dice task:
When the microbit is shaken, it should display spots like a dice.
It should randomly display 1 spot or 2 spots (all the way to 6) like a dice.
Use and tweak the "Random code" from here
http://microbit-micropython.readthedocs.io/en/latest/accelerometer.html
Use and tweak the "Image" code from here
http://microbit-micropython.readthedocs.io/en/latest/tutorials/images.html
hint:
random.randint(1,6)
This line of code generates a random integer between 1 and 6
When the microbit is shaken, it should display spots like a dice.
It should randomly display 1 spot or 2 spots (all the way to 6) like a dice.
Use and tweak the "Random code" from here
http://microbit-micropython.readthedocs.io/en/latest/accelerometer.html
Use and tweak the "Image" code from here
http://microbit-micropython.readthedocs.io/en/latest/tutorials/images.html
hint:
random.randint(1,6)
This line of code generates a random integer between 1 and 6
Answer
from microbit import *
import random
while True:
display.show(Image.HAPPY)
if accelerometer.was_gesture('shake'):
display.clear()
sleep(1000)
answer = random.randint(1,6)
if answer == 1:
one = Image("00000:00000:00900:00000:00000")
display.show(one)
sleep(1000)
elif answer == 2:
two = Image("00000:00090:00000:09000:00000")
display.show(two)
sleep(1000)
elif answer == 3:
three = Image("00009:00000:00900:00000:90000")
display.show(three)
sleep(1000)
elif answer == 4:
four = Image("00000:09090:00000:09090:00000")
display.show(four)
sleep(1000)
elif answer == 5:
five = Image("00000:09090:00900:09090:00000")
display.show(five)
sleep(1000)
elif answer == 6:
six = Image("09090:00000:09090:00000:09090")
display.show(six)
sleep(1000)
sleep(10)
import random
while True:
display.show(Image.HAPPY)
if accelerometer.was_gesture('shake'):
display.clear()
sleep(1000)
answer = random.randint(1,6)
if answer == 1:
one = Image("00000:00000:00900:00000:00000")
display.show(one)
sleep(1000)
elif answer == 2:
two = Image("00000:00090:00000:09000:00000")
display.show(two)
sleep(1000)
elif answer == 3:
three = Image("00009:00000:00900:00000:90000")
display.show(three)
sleep(1000)
elif answer == 4:
four = Image("00000:09090:00000:09090:00000")
display.show(four)
sleep(1000)
elif answer == 5:
five = Image("00000:09090:00900:09090:00000")
display.show(five)
sleep(1000)
elif answer == 6:
six = Image("09090:00000:09090:00000:09090")
display.show(six)
sleep(1000)
sleep(10)