|
7 | 7 | Pressing either button returns to the dice selection mode. |
8 | 8 | """ |
9 | 9 |
|
| 10 | +import time |
| 11 | +import board |
10 | 12 | from random import randint |
11 | 13 | from adafruit_clue import clue |
12 | 14 | 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 |
13 | 19 |
|
14 | 20 |
|
15 | 21 | # input constraints |
|
20 | 26 | SELECTING = 0 |
21 | 27 | ROLL_RESULT = 1 |
22 | 28 |
|
23 | | -number_of_dice = 1 |
| 29 | +number_of_dice = 0 # 0-relative, gets adjusted to 1-relative before display/use |
24 | 30 | side_selection = 0 |
25 | 31 |
|
26 | 32 | button_a = Debouncer(lambda: clue.button_a) |
27 | 33 | button_b = Debouncer(lambda: clue.button_b) |
28 | | -shake = Debouncer(lambda: False) |
29 | 34 |
|
30 | 35 |
|
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 |
36 | 57 |
|
| 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) |
37 | 66 |
|
38 | | -def roll(count, sides) |
39 | | - roll = sum([randint(1, sides) for d in range(count)]) |
40 | 67 |
|
| 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 = '' |
41 | 72 |
|
| 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 |
42 | 83 | mode = SELECTING |
| 84 | +update_display(number_of_dice, side_selection) |
43 | 85 |
|
44 | 86 | while True: |
45 | 87 | button_a.update() |
46 | 88 | button_b.update() |
47 | | - shaken.update() |
48 | 89 |
|
49 | 90 | if mode == SELECTING: |
50 | 91 | 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) |
53 | 94 | elif button_b.rose: |
54 | 95 | 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): |
57 | 98 | 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) |
58 | 102 | roll(number_of_dice, SIDES[side_selection]) |
59 | 103 | else: |
60 | | - if button_a.rose or button_b.rose: |
| 104 | + if button_a.rose or button_b.rose: # back to dice selection |
61 | 105 | 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