Skip to content

Commit 97d0047

Browse files
committed
Use the display
1 parent ee8e643 commit 97d0047

1 file changed

Lines changed: 62 additions & 16 deletions

File tree

CLUE_Dice_Roller/clue_dice_roller.py

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
Pressing either button returns to the dice selection mode.
88
"""
99

10+
import time
11+
import board
1012
from random import randint
1113
from adafruit_clue import clue
1214
from adafruit_debouncer import Debouncer
15+
import displayio
16+
from adafruit_bitmap_font import bitmap_font
17+
import terminalio
18+
from adafruit_display_text import label
1319

1420

1521
# input constraints
@@ -20,43 +26,83 @@
2026
SELECTING = 0
2127
ROLL_RESULT = 1
2228

23-
number_of_dice = 1
29+
number_of_dice = 0 # 0-relative, gets adjusted to 1-relative before display/use
2430
side_selection = 0
2531

2632
button_a = Debouncer(lambda: clue.button_a)
2733
button_b = Debouncer(lambda: clue.button_b)
28-
shake = Debouncer(lambda: False)
2934

3035

31-
def reset_selection_screen()
32-
number_of_dice = 1
33-
side_selection = 0
34-
number_label.text = str(number_of_dice)
35-
sides_label.text = str(SIDES[side_selection])
36+
# Set up display
37+
38+
display = board.DISPLAY
39+
selection_font = bitmap_font.load_font('/Helvetica-Bold-36.bdf')
40+
selection_font.load_glyphs(b'0123456789')
41+
selection_color = 0x0000FF
42+
43+
roll_font = bitmap_font.load_font('/Anton-Regular-104.bdf')
44+
roll_font.load_glyphs(b'0123456789')
45+
roll_color = 0xFFFFFF
46+
47+
selection_label = label.Label(selection_font, x=0, y=25, text='XdXXX', color=selection_color)
48+
roll_label = label.Label(roll_font, x=0, y=150, text='XXX', color=roll_color)
49+
50+
group = displayio.Group()
51+
group.append(selection_label)
52+
group.append(roll_label)
53+
54+
display.show(group)
55+
56+
# Helper functions
3657

58+
def roll(count, sides):
59+
for i in range(15):
60+
roll = sum([randint(1, sides) for d in range(count + 1)])
61+
roll_label.text = str(roll)
62+
roll_label.x = 120 - (roll_label.bounding_box[2] // 2)
63+
duration = (i * 0.05) / 2
64+
clue.play_tone(2000, duration)
65+
time.sleep(duration)
3766

38-
def roll(count, sides)
39-
roll = sum([randint(1, sides) for d in range(count)])
4067

68+
def update_display(count, sides):
69+
selection_label.text = '{0}d{1}'.format(count + 1, SIDES[sides])
70+
selection_label.x = 120 - (selection_label.bounding_box[2] // 2)
71+
roll_label.text = ''
4172

73+
74+
def reset_selection_screen():
75+
global number_of_dice, side_selection
76+
number_of_dice = 0
77+
side_selection = 0
78+
update_display(number_of_dice, side_selection)
79+
80+
81+
82+
display
4283
mode = SELECTING
84+
update_display(number_of_dice, side_selection)
4385

4486
while True:
4587
button_a.update()
4688
button_b.update()
47-
shaken.update()
4889

4990
if mode == SELECTING:
5091
if button_a.rose:
51-
number_of_dice = (number_of_dice + 1) % MAX_NUMBER_OF_DICE
52-
number_label.text = str(number_of_dice)
92+
number_of_dice = ((number_of_dice + 1) % MAX_NUMBER_OF_DICE)
93+
update_display(number_of_dice, side_selection)
5394
elif button_b.rose:
5495
side_selection = (side_selection + 1) % len(SIDES)
55-
sides_label.text = str(SIDES[side_selection])
56-
elif shaken.rose:
96+
update_display(number_of_dice, side_selection)
97+
elif clue.shake(shake_threshold=25):
5798
mode = ROLL_RESULT
99+
if SIDES[side_selection] == 100: # only roll one percentile
100+
number_of_dice = 0
101+
update_display(number_of_dice, side_selection)
58102
roll(number_of_dice, SIDES[side_selection])
59103
else:
60-
if button_a.rose or button_b.rose:
104+
if button_a.rose or button_b.rose: # back to dice selection
61105
mode = SELECTING
62-
reset_selection_screen()
106+
update_display(number_of_dice, side_selection)
107+
elif clue.shake(shake_threshold=25): # reroll
108+
roll(number_of_dice, SIDES[side_selection])

0 commit comments

Comments
 (0)