-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: Add a 20-sided die roller to the examples.
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
die20.py | ||
~~~~~~~~ | ||
Rolls a 20-sided die. If a 20 is rolled, display a short series of images and | ||
play some music. The skull image represents 20. If a 1 is rolled, display a sad | ||
face and play some music. | ||
The music isn't required, but if you want to hear it, you'll need a | ||
speaker/buzzer/headphones connected to P0 and GND. | ||
""" | ||
|
||
from microbit import * | ||
import random | ||
import music | ||
|
||
# A list of images to display if 20 (crit) is rolled | ||
critimages = [Image.HEART, Image.HEART_SMALL, Image.SKULL] | ||
|
||
# Loop forever | ||
while True: | ||
# User can press button A or shake the microbit to "roll" | ||
if button_a.was_pressed() or accelerometer.was_gesture("shake"): | ||
dice = random.randint(1, 20) | ||
if dice == 20: | ||
# Set wait to False to that it's not blocking. | ||
# Music will play while images are cycled through. | ||
music.play(music.POWER_UP, wait=False) | ||
display.show(critimages, delay=1400) | ||
elif dice == 1: | ||
music.play(music.WAWAWAWAA, wait=False) | ||
display.show(Image.SAD) | ||
# For single digit numbers, show the number. Scroll for double-digits. | ||
elif 10 > dice > 1: | ||
display.show(str(dice)) | ||
else: | ||
display.scroll(str(dice), wait=False, loop=True) |