-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimonGame.py
114 lines (96 loc) · 2.82 KB
/
SimonGame.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
import time
import random
from adafruit_circuitplayground.express import cpx
cpx.pixels.brightness = 0.1 # adjust NeoPixel brightness to your liking
REGION_LEDS = (
(5, 6, 7), # yellow region
(2, 3, 4), # blue region
(7, 8, 9), # red region
(0, 1, 2), # green region
)
REGION_COLOR = (
(255, 255, 0), # yellow region
(0, 0, 255), # blue region
(255, 0, 0), # red region
(0, 255, 0), # green region
)
REGION_TONE = (
252, # yellow region
209, # blue region
310, # red region
415, # green region
)
PAD_REGION = {
'A1': 0, # yellow region
'A2': 2, # red region
'A3': 2, # red region
'A4': 3, # green region
'A5': 3, # green region
'A6': 1, # blue region
'A7': 1, # blue region
}
def light_region(region, duration=1):
# turn the LEDs for the selected region on
for led in REGION_LEDS[region]:
cpx.pixels[led] = REGION_COLOR[region]
# play a tone for the selected region
cpx.start_tone(REGION_TONE[region])
# wait the requested amount of time
time.sleep(duration)
# stop the tone
cpx.stop_tone()
# turn the LEDs for the selected region off
for led in REGION_LEDS[region]:
cpx.pixels[led] = (0, 0, 0)
def read_region(timeout=5):
val = 0
start_time = time.time()
while time.time() - start_time < timeout:
if cpx.touch_A1:
val = PAD_REGION['A1']
elif cpx.touch_A2:
val = PAD_REGION['A2']
elif cpx.touch_A3:
val = PAD_REGION['A3']
elif cpx.touch_A4:
val = PAD_REGION['A4']
elif cpx.touch_A5:
val = PAD_REGION['A5']
elif cpx.touch_A6:
val = PAD_REGION['A6']
elif cpx.touch_A7:
val = PAD_REGION['A7']
return val
def play_sequence(sequence):
duration = 1 - len(sequence) * 0.05
if duration < 0.1:
duration = 0.1
for region in sequence:
light_region(region, duration)
def read_sequence(sequence):
for region in sequence:
if read_region() != region:
# the player made a mistake!
return False
light_region(region, 0.25)
return True
def play_error():
for led in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
cpx.pixels[led] = (255, 0, 0)
cpx.start_tone(160)
time.sleep(1)
cpx.stop_tone()
for led in [0, 1, 2, 3, 4, 5, 6, 7, 8 ,9]:
cpx.pixels[led] = (0, 0, 0)
def play_game():
sequence = []
while True:
sequence.append(random.randint(0, 3))
play_sequence(sequence)
if not read_sequence(sequence):
# game over
play_error()
break
time.sleep(1)
while True:
play_game()