14
14
import math
15
15
import board
16
16
from analogio import AnalogIn
17
- from adafruit_circuitplayground . express import cpx
17
+ from adafruit_circuitplayground import cp
18
18
19
19
FAILURE_TONE = 100
20
20
SEQUENCE_DELAY = 0.8
30
30
1 : { 'pads' :(4 ,5 ), 'pixels' :(0 ,1 ,2 ), 'color' :0x00FF00 , 'freq' :415 },
31
31
2 : { 'pads' :(6 ,7 ), 'pixels' :(2 ,3 ,4 ), 'color' :0xFFFF00 , 'freq' :252 },
32
32
3 : { 'pads' :(1 , ), 'pixels' :(5 ,6 ,7 ), 'color' :0x0000FF , 'freq' :209 },
33
- 4 : { 'pads' :(2 ,3 ), 'pixels' :(7 ,8 ,9 ), 'color' :0xFF0000 , 'freq' :310 },
33
+ 4 : { 'pads' :(2 ,3 ), 'pixels' :(7 ,8 ,9 ), 'color' :0xFF0000 , 'freq' :310 },
34
34
}
35
35
36
36
def choose_skill_level ():
37
37
# Default
38
38
skill_level = 1
39
39
# Loop until button B is pressed
40
- while not cpx .button_b :
40
+ while not cp .button_b :
41
41
# Button A increases skill level setting
42
- if cpx .button_a :
42
+ if cp .button_a :
43
43
skill_level += 1
44
44
skill_level = skill_level if skill_level < 5 else 1
45
45
# Indicate current skill level
46
- cpx .pixels .fill (0 )
46
+ cp .pixels .fill (0 )
47
47
for p in range (skill_level ):
48
- cpx .pixels [p ] = 0xFFFFFF
48
+ cp .pixels [p ] = 0xFFFFFF
49
49
time .sleep (DEBOUNCE )
50
50
return skill_level
51
51
52
52
def new_game (skill_level ):
53
53
# Seed the random function with noise
54
- a4 = AnalogIn (board .A4 )
55
- a5 = AnalogIn ( board . A5 )
56
- a6 = AnalogIn ( board . A6 )
57
- a7 = AnalogIn ( board . A7 )
54
+ with AnalogIn ( board . A4 ) as a4 , AnalogIn ( board . A5 ) as a5 , AnalogIn (board .A6 ) as a6 :
55
+ seed = a4 . value
56
+ seed += a5 . value
57
+ seed += a6 . value
58
58
59
- seed = a4 .value
60
- seed += a5 .value
61
- seed += a6 .value
62
- seed += a7 .value
59
+ random .seed (seed )
63
60
64
- random .seed (seed )
65
-
66
- # Populate the game sequence
67
- return [random .randint (1 ,4 ) for i in range (SEQUENCE_LENGTH [skill_level ])]
61
+ # Populate the game sequence
62
+ return [random .randint (1 ,4 ) for i in range (SEQUENCE_LENGTH [skill_level ])]
68
63
69
64
def indicate_button (button , duration ):
70
65
# Turn them all off
71
- cpx .pixels .fill (0 )
66
+ cp .pixels .fill (0 )
72
67
# Turn on the ones for the given button
73
68
for p in button ['pixels' ]:
74
- cpx .pixels [p ] = button ['color' ]
69
+ cp .pixels [p ] = button ['color' ]
75
70
# Play button tone
76
71
if button ['freq' ] == None :
77
72
time .sleep (duration )
78
73
else :
79
- cpx .play_tone (button ['freq' ], duration )
74
+ cp .play_tone (button ['freq' ], duration )
80
75
# Turn them all off again
81
- cpx .pixels .fill (0 )
82
-
76
+ cp .pixels .fill (0 )
77
+
83
78
def show_sequence (sequence , step ):
84
79
# Set tone playback duration based on current location
85
80
if step <= 5 :
@@ -88,21 +83,21 @@ def show_sequence(sequence, step):
88
83
duration = 0.320
89
84
else :
90
85
duration = 0.220
91
-
86
+
92
87
# Play back sequence up to current step
93
88
for b in range (step ):
94
89
time .sleep (0.05 )
95
- indicate_button (SIMON_BUTTONS [sequence [b ]], duration )
90
+ indicate_button (SIMON_BUTTONS [sequence [b ]], duration )
96
91
97
92
def cap_map (b ):
98
- if b == 1 : return cpx .touch_A1
99
- if b == 2 : return cpx .touch_A2
100
- if b == 3 : return cpx .touch_A3
101
- if b == 4 : return cpx .touch_A4
102
- if b == 5 : return cpx .touch_A5
103
- if b == 6 : return cpx .touch_A6
104
- if b == 7 : return cpx . touch_A7
105
-
93
+ if b == 1 : return cp .touch_A1
94
+ if b == 2 : return cp .touch_A2
95
+ if b == 3 : return cp .touch_A3
96
+ if b == 4 : return cp .touch_A4
97
+ if b == 5 : return cp .touch_A5
98
+ if b == 6 : return cp .touch_A6
99
+ if b == 7 : return cp . touch_TX
100
+
106
101
def get_button_press ():
107
102
# Loop over all the buttons
108
103
for button in SIMON_BUTTONS .values ():
@@ -115,74 +110,74 @@ def get_button_press():
115
110
116
111
def game_lost (step ):
117
112
# Show button that should have been pressed
118
- cpx .pixels .fill (0 )
113
+ cp .pixels .fill (0 )
119
114
for p in SIMON_BUTTONS [sequence [step ]]['pixels' ]:
120
- cpx .pixels [p ] = SIMON_BUTTONS [sequence [step ]]['color' ]
121
-
115
+ cp .pixels [p ] = SIMON_BUTTONS [sequence [step ]]['color' ]
116
+
122
117
# Play sad sound :(
123
- cpx .play_tone (FAILURE_TONE , 1.5 )
124
-
118
+ cp .play_tone (FAILURE_TONE , 1.5 )
119
+
125
120
# And just sit here until reset
126
121
while True :
127
122
pass
128
-
123
+
129
124
def game_won ():
130
125
# Play 'razz' special victory signal
131
126
for i in range (3 ):
132
- indicate_button (SIMON_BUTTONS [4 ], 0.1 )
133
- indicate_button (SIMON_BUTTONS [2 ], 0.1 )
134
- indicate_button (SIMON_BUTTONS [3 ], 0.1 )
135
- indicate_button (SIMON_BUTTONS [1 ], 0.1 )
136
- indicate_button (SIMON_BUTTONS [4 ], 0.1 )
127
+ indicate_button (SIMON_BUTTONS [4 ], 0.1 )
128
+ indicate_button (SIMON_BUTTONS [2 ], 0.1 )
129
+ indicate_button (SIMON_BUTTONS [3 ], 0.1 )
130
+ indicate_button (SIMON_BUTTONS [1 ], 0.1 )
131
+ indicate_button (SIMON_BUTTONS [4 ], 0.1 )
137
132
indicate_button (SIMON_BUTTONS [2 ], 0.1 )
138
133
139
134
# Change tones to failure tone
140
135
for button in SIMON_BUTTONS .values ():
141
136
button ['freq' ] = FAILURE_TONE
142
-
137
+
143
138
# Continue for another 0.8 seconds
144
139
for i in range (2 ):
145
- indicate_button (SIMON_BUTTONS [3 ], 0.1 )
146
- indicate_button (SIMON_BUTTONS [1 ], 0.1 )
147
- indicate_button (SIMON_BUTTONS [4 ], 0.1 )
148
- indicate_button (SIMON_BUTTONS [2 ], 0.1 )
149
-
140
+ indicate_button (SIMON_BUTTONS [3 ], 0.1 )
141
+ indicate_button (SIMON_BUTTONS [1 ], 0.1 )
142
+ indicate_button (SIMON_BUTTONS [4 ], 0.1 )
143
+ indicate_button (SIMON_BUTTONS [2 ], 0.1 )
144
+
150
145
# Change tones to silence
151
146
for button in SIMON_BUTTONS .values ():
152
147
button ['freq' ] = None
153
-
148
+
154
149
# Loop lights forever
155
150
while True :
156
- indicate_button (SIMON_BUTTONS [3 ], 0.1 )
157
- indicate_button (SIMON_BUTTONS [1 ], 0.1 )
158
- indicate_button (SIMON_BUTTONS [4 ], 0.1 )
159
- indicate_button (SIMON_BUTTONS [2 ], 0.1 )
160
-
161
- # Initialize setup
162
- cpx .pixels .fill (0 )
163
- cpx .pixels [0 ] = 0xFFFFFF
151
+ indicate_button (SIMON_BUTTONS [3 ], 0.1 )
152
+ indicate_button (SIMON_BUTTONS [1 ], 0.1 )
153
+ indicate_button (SIMON_BUTTONS [4 ], 0.1 )
154
+ indicate_button (SIMON_BUTTONS [2 ], 0.1 )
155
+
156
+ # Initialize setup
157
+ cp .pixels .fill (0 )
158
+ cp .pixels [0 ] = 0xFFFFFF
164
159
skill_level = choose_skill_level ()
165
160
sequence = new_game (skill_level )
166
161
current_step = 1
167
162
168
- #Loop forever
163
+ # Loop forever
169
164
while True :
170
165
# Show sequence up to current step
171
166
show_sequence (sequence , current_step )
172
-
167
+
173
168
# Read player button presses
174
169
for step in range (current_step ):
175
170
start_guess_time = time .monotonic ()
176
171
guess = None
177
172
while (time .monotonic () - start_guess_time < GUESS_TIMEOUT ) and (guess == None ):
178
173
guess = get_button_press ()
179
174
if not guess == SIMON_BUTTONS [sequence [step ]]:
180
- game_lost (sequence [ step ] )
175
+ game_lost (step )
181
176
182
177
# Advance the game forward
183
178
current_step += 1
184
179
if current_step > len (sequence ):
185
180
game_won ()
186
-
187
- # Small delay before continuing
181
+
182
+ # Small delay before continuing
188
183
time .sleep (SEQUENCE_DELAY )
0 commit comments