Skip to content

Commit a5ad8c2

Browse files
committed
Initial code sketch
1 parent cdd795a commit a5ad8c2

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Dice roller for CLUE
3+
4+
Set the number of dice with button A (1-2-3-4-5-6)
5+
and the type with button B (d4-d6-d8-d10-d12-d20-d100).
6+
Roll by shaking.
7+
Pressing either button returns to the dice selection mode.
8+
"""
9+
10+
from random import randint
11+
from adafruit_clue import clue
12+
from adafruit_debouncer import Debouncer
13+
14+
15+
# input constraints
16+
MAX_NUMBER_OF_DICE = 6
17+
SIDES = [4, 6, 8, 12, 20, 100]
18+
19+
# modes: selecting/result
20+
SELECTING = 0
21+
ROLL_RESULT = 1
22+
23+
number_of_dice = 1
24+
side_selection = 0
25+
26+
button_a = Debouncer(lambda: clue.button_a)
27+
button_b = Debouncer(lambda: clue.button_b)
28+
shake = Debouncer(lambda: False)
29+
30+
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+
37+
38+
def roll(count, sides)
39+
roll = sum([randint(1, sides) for d in range(count)])
40+
41+
42+
mode = SELECTING
43+
44+
while True:
45+
button_a.update()
46+
button_b.update()
47+
shaken.update()
48+
49+
if mode == SELECTING:
50+
if button_a.rose:
51+
number_of_dice = (number_of_dice + 1) % MAX_NUMBER_OF_DICE
52+
number_label.text = str(number_of_dice)
53+
elif button_b.rose:
54+
side_selection = (side_selection + 1) % len(SIDES)
55+
sides_label.text = str(SIDES[side_selection])
56+
elif shaken.rose:
57+
mode = ROLL_RESULT
58+
roll(number_of_dice, SIDES[side_selection])
59+
else:
60+
if button_a.rose or button_b.rose:
61+
mode = SELECTING
62+
reset_selection_screen()

0 commit comments

Comments
 (0)