-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
167 lines (146 loc) · 5.25 KB
/
Copy pathmain.py
File metadata and controls
167 lines (146 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from picounicorn import PicoUnicorn
import time
picounicorn = PicoUnicorn()
MAX_WIDTH = picounicorn.get_width()
MAX_HEIGHT = picounicorn.get_height()
MAX_BAR_LENGTH = MAX_WIDTH / 2
PLAYER_COUNT = 4
BAR_WIDTH = 3
# Assume player 1 (index 0) sits immediately to left of GM and moving around the table clockwise
BUTTONS_TO_PLAYER = {
picounicorn.BUTTON_B: 0,
picounicorn.BUTTON_A: 1,
picounicorn.BUTTON_X: 2,
picounicorn.BUTTON_Y: 3,
}
# Common colours to use for the bars
COLOURS = {
"red": [255, 0, 0],
"blue": [0, 0, 255],
"green": [0, 255, 0],
"yellow": [255, 200, 0],
"orange": [255, 90, 0],
"pink": [255, 90, 180],
"purple": [150, 0, 150],
"white": [255, 255, 255],
}
# Accessibility setting - set to True to disable the flashing when a user tries to extend a bar
# that is already at max length
DISABLE_FLASH = False
class ActionDisplay:
def __init__(self):
self.player_colours = {
0: COLOURS["green"],
1: COLOURS["purple"],
2: COLOURS["yellow"],
3: COLOURS["blue"],
}
self.actions_count = {
0: 0,
1: 0,
2: 0,
3: 0
}
self.button_presses = {
picounicorn.BUTTON_A: False,
picounicorn.BUTTON_B: False,
picounicorn.BUTTON_X: False,
picounicorn.BUTTON_Y: False,
}
# Calculate a rectangle to draw for a given player
def get_bar_coords(self, player_number):
is_top = player_number in [1, 2]
is_left = player_number in [0, 1]
length = self.actions_count[player_number]
# (0,0) is top left on a unicorn screen
if is_top:
y0 = 0
y1 = BAR_WIDTH
else:
y0 = 4
y1 = 4 + BAR_WIDTH
if is_left:
x0 = 0
x1 = length
else:
x0 = MAX_WIDTH - length
x1 = MAX_WIDTH
return (x0, x1, y0, y1)
# Draws a bar for a given player
def draw_bar(self, player_number, colour_override = None):
x0, x1, y0, y1 = self.get_bar_coords(player_number)
if colour_override is None:
r, g, b = self.player_colours[player_number]
else:
r, g, b = colour_override
for x in range(x0, x1):
for y in range(y0, y1):
picounicorn.set_pixel(x, y, r, g, b)
# Flashes a bar when it is already full, but the user tries adding an action for that player
# can be disabled with the DISABLE_FLASH constant
def flash_bar(self, player_number):
if DISABLE_FLASH:
return
number_flashes = 3
flash_duration = 0.15
colour = self.player_colours[player_number]
for i in range(number_flashes):
self.draw_bar(player_number)
time.sleep(flash_duration)
self.draw_bar(player_number, [100, 100, 100])
time.sleep(flash_duration)
def increment_action_count(self, player_number):
if self.actions_count[player_number] == MAX_BAR_LENGTH:
self.flash_bar(player_number)
else:
self.actions_count[player_number] += 1
# Reset all counters to 0 if B and X are pressed simulataneously
# Returns True if a reset was performed
def check_for_reset(self):
if picounicorn.is_pressed(picounicorn.BUTTON_B) and picounicorn.is_pressed(picounicorn.BUTTON_X):
self.actions_count = {
0: 0,
1: 0,
2: 0,
3: 0
}
time.sleep(0.5)
return True
return False
# Check if buttons have been pressed. Only record a new press after previous press has been lifted
# Unless checking for a reset condition
def check_for_button_press(self, button_code):
if picounicorn.is_pressed(button_code) and (not self.button_presses[button_code]):
self.button_presses[button_code] = True
self.increment_action_count(BUTTONS_TO_PLAYER[button_code])
return True
elif (not picounicorn.is_pressed(button_code)) and self.button_presses[button_code]:
self.button_presses[button_code] = False
return False
# Resets the screen and draws all 4 bars
def draw(self):
for x in range(MAX_WIDTH):
for y in range(MAX_HEIGHT):
picounicorn.set_pixel(x, y, 0, 0, 0)
for i in range(PLAYER_COUNT):
self.draw_bar(i)
# Checks if any of the buttons have been pressed and returns True if they have been
def check_input(self):
if self.check_for_reset():
return True
elif self.check_for_button_press(picounicorn.BUTTON_A):
return True
elif self.check_for_button_press(picounicorn.BUTTON_B):
return True
elif self.check_for_button_press(picounicorn.BUTTON_X):
return True
elif self.check_for_button_press(picounicorn.BUTTON_Y):
return True
if __name__ == "__main__":
display = ActionDisplay()
while True:
button_pressed = display.check_input()
# No need to re-render if no buttons pressed
if button_pressed:
display.draw()
time.sleep(1.0 / 60)