Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a 20-sided die roller to the examples #297

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/d20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
d20.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)