-
Notifications
You must be signed in to change notification settings - Fork 1
/
mydemo.py
158 lines (124 loc) · 3.58 KB
/
mydemo.py
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
import time
import urandom
import picokeypad as keypad
keypad.init()
keypad.set_brightness(1.0)
game_in_progress = 0
level = 0;
# 16 keys:
# 0 1 2 3
# 4 5 6 7
# 8 9 a b
# c d e f
######### COLOURS #######
red = [0x80, 0x00, 0x00]
blue = [0x00, 0x00, 0x80]
purple = [0x30, 0x00, 0xff]
pink = [0x50, 0x10, 0x8f]
yellow = [0x50, 0x50, 0x00]
green = [0x10, 0xff, 0x00]
teal = [0x00, 0x30, 0xff]
########### LEVELS ########
levels = {}
levels[1] = {
9: red,
6: yellow,
}
levels[2] = {
1: yellow,
7: teal,
8: green
}
levels[3] = {
0: pink,
2: blue,
7: blue,
4: pink,
12: purple
}
###### FUNCTIONS ####
def draw_pattern(pattern):
keypad.clear()
for i in range(16):
if i in pattern:
button = pattern[i]
keypad.illuminate(i, button[0], button[1], button[2])
keypad.update()
def won():
on = 1
while True:
# flash key 15
if on:
keypad.illuminate(15, 0x00, 0x80, 0x00)
on = 0
else:
keypad.illuminate(15, 0x00, 0x00, 0x00)
on = 1
keypad.update()
time.sleep(1)
# return when key 15 is pressed
if keypad.get_button_states() == 32768:
return
##### MAIN PROGRAM ###
def init(pattern):
# draw pattern and wait a few seconds before the next step
draw_pattern(pattern)
time.sleep(3)
# shuffle and draw current_lights, kind of DIY solution
current_lights = {}
# represents the original pattern, this handles gaps
options = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
for i in range(16):
# pick an option
pick = urandom.choice(options)
# use that value from the original pattern, remove this from the options
if pick in pattern:
current_lights[i] = pattern[pick]
options.remove(pick)
draw_pattern(current_lights)
return current_lights
######### EVENT LOOP #########
while True:
# initialise if appropriate
if game_in_progress == 0:
level = level + 1
if level not in levels:
level = 1
winning_pattern = levels[level]
current_lights = init(winning_pattern)
game_in_progress = 1
pressed = []
button_states = keypad.get_button_states()
# figure out what's pressed
for i in range(16):
if (button_states >> i) & 0x01 != 0:
pressed.append(i)
# were two buttons pressed? Swap them and redraw
if len(pressed) == 2:
lights = {}
indexes = {}
place = "first"
for p in pressed:
indexes[place] = p
if p in current_lights:
lights[place] = current_lights[p]
else:
lights[place] = []
place = "second"
# does this index exist? and should it after this? (err... twice)
if len(lights["second"]):
current_lights[indexes["first"]] = lights["second"]
else:
if indexes["first"] in current_lights:
del current_lights[indexes["first"]]
if len(lights["first"]):
current_lights[indexes["second"]] = lights["first"]
else:
if indexes["second"] in current_lights:
del current_lights[indexes["second"]]
draw_pattern(current_lights)
time.sleep(0.5)
# check if we won (suspense intentional)
if current_lights == winning_pattern:
game_in_progress = 0
won()