Story 1: Shake for a numberrandom
The same in Python
# Story 1: Shake for a number. Shake the Micro:bit: it picks a number from 1 to 6.
def on_gesture_shake():
basic.show_number(randint(1, 6))
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
basic.show_icon(IconNames.SQUARE)
Put show number into on shake, and pick random 1 to 6 into the number's place. Every shake
picks a new number. The square at the start shows that the dice is ready.
Your turn: make it a dice for a game that needs numbers from 1 to 10.
You firstYour first roll is yours: drag on shake and show number yourself, then find pick random in Math.
Story 2: Roll with a spinsteps in order
The same in Python
# Story 2: Roll with a spin. A little animation and a sound, then the number.
def on_gesture_shake():
music.play(music.builtin_playable_sound_effect(soundExpression.spring),
music.PlaybackMode.IN_BACKGROUND)
basic.show_icon(IconNames.SMALL_DIAMOND)
basic.show_icon(IconNames.DIAMOND)
basic.show_icon(IconNames.SMALL_DIAMOND)
basic.show_number(randint(1, 6))
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
basic.show_icon(IconNames.SQUARE)
A real dice tumbles before it stops. Three pictures and a sound first, then the number: the blocks run from top
to bottom.
Your turn: make your own spin with other pictures, or a different sound.
Story 3: Rock, paper, scissorsif and else if
The same in Python
# Story 3: Rock, paper, scissors. hand remembers the choice; if and else if show its picture.
hand = 0
def on_gesture_shake():
global hand
hand = randint(0, 2)
if hand == 0:
basic.show_icon(IconNames.SMALL_SQUARE)
elif hand == 1:
basic.show_icon(IconNames.SQUARE)
else:
basic.show_icon(IconNames.SCISSORS)
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
basic.show_icon(IconNames.SQUARE)
The variable hand remembers one random number: 0, 1 or 2. Then if and else if choose the
picture: a small square is the rock, a big square the paper, and the scissors are a picture of their own.
Your turn: play against the person next to you: count, shake, show!
Story 4: Count your sixesa counter
The same in Python
# Story 4: Count your sixes. Every 6 makes a happy sound and counts; button A shows how many sixes you rolled.
roll = 0
sixes = 0
def on_gesture_shake():
global roll, sixes
roll = randint(1, 6)
basic.show_number(roll)
if roll == 6:
sixes += 1
music.play(music.builtin_playable_sound_effect(soundExpression.happy),
music.PlaybackMode.IN_BACKGROUND)
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
def on_button_pressed_a():
basic.show_number(sixes)
input.on_button_pressed(Button.A, on_button_pressed_a)
basic.show_icon(IconNames.SQUARE)
roll keeps the number so the program can show it and ask about it. Every six plays a happy sound and adds
one to sixes. Button A shows how many sixes you have rolled.
Your turn: shake 30 times. How many sixes did you expect? How many did you get?
Real worldGames, lotteries and computer games all use random numbers. People check that they are fair, like you do with 60 rolls.