1414import adafruit_dotstar
1515
1616# Initialize dot star led
17- num_pixels = 1
1817pixels = adafruit_dotstar .DotStar (board .APA102_SCK , board .APA102_MOSI ,
19- num_pixels , brightness = 0.1 , auto_write = False )
18+ 1 , brightness = 0.1 )
2019red = (255 ,0 ,0 )
2120green = (0 ,255 ,0 )
2221blue = (0 ,0 ,255 )
3837
3938def intro_game ():
4039 pixels .fill (blue )
41- pixels . show ( )
42- for q in range ( len ( leds )) :
43- leds [ q ] .value = True
40+ time . sleep ( 1 )
41+ for led in leds :
42+ led .value = True
4443 time .sleep (0.25 )
45- for l in range ( len ( leds )) :
46- leds [ l ] .value = False
44+ for led in leds :
45+ led .value = False
4746
4847def wheel (pos ):
4948 # Input a value 0 to 255 to get a color value.
@@ -60,10 +59,9 @@ def wheel(pos):
6059
6160def rainbow_cycle (wait ):
6261 for j in range (255 ):
63- for i in range (num_pixels ):
64- rc_index = (i * 256 // num_pixels ) + j
62+ for i in range (len ( pixels ) ):
63+ rc_index = (i * 256 // 1 ) + j
6564 pixels [i ] = wheel (rc_index & 255 )
66- pixels .show ()
6765 time .sleep (wait )
6866
6967def read_caps ():
@@ -83,17 +81,13 @@ def read_caps():
8381 cap_touches [3 ] = touches [3 ].raw_value > 3000
8482 return cap_touches
8583
86- def record_caps (timeout = 3 ):
84+ def timeout_touch (timeout = 3 ):
8785 start_time = time .monotonic () # start 3 second timer waiting for user input
88- val = None
8986 while time .monotonic () - start_time < timeout :
9087 caps = read_caps ()
9188 for i ,c in enumerate (caps ):
9289 if c :
93- val = i
94- time .sleep (0.1 )
95- if val is not None : # if there's input from a pad exit the timer loop
96- return val
90+ return i
9791
9892def light_cap (cap , duration = 0.5 ):
9993 # turn the LED for the selected cap on
@@ -102,47 +96,36 @@ def light_cap(cap, duration=0.5):
10296 leds [cap ].value = False
10397 time .sleep (duration )
10498
105- def play_sequence (sequence ):
99+ def play_sequence (seq ):
106100 duration = max (0.1 , 1 - len (sequence ) * 0.05 )
107- for cap in sequence :
101+ for cap in seq :
108102 light_cap (cap , duration )
109103
110- def read_sequence (sequence ):
104+ def read_sequence (seq ):
111105 pixels .fill (green )
112- pixels .show ()
113- for cap in sequence :
114- if record_caps () != cap :
106+ for cap in seq :
107+ if timeout_touch () != cap :
115108 # the player made a mistake!
116109 return False
117110 light_cap (cap , 0.5 )
118111 return True
119112
120- def play_error ():
121- # make dot star red
122- pixels .fill (red )
123- pixels .show ()
124- time .sleep (3 )
125-
126- def play_game ():
113+ while True :
127114 intro_game () # led light sequence at beginning of each game
128115 sequence = []
129116 while True :
130117 pixels .fill (blue ) # blue for showing user sequence
131- pixels .show ()
132118 time .sleep (1 )
133119 sequence .append (random .randint (0 , 3 )) # add new light to sequence each time
134120 play_sequence (sequence ) # show the sequence
135121 if not read_sequence (sequence ): # if user inputs wrong sequence, gameover
136- # game over
137- play_error ()
122+ # game over, make dot star red
123+ pixels .fill (red )
124+ time .sleep (3 )
138125 print ("gameover" )
139126 break
140127 else :
141128 print ("Next sequence unlocked!" )
142129 rainbow_cycle (0 ) # Dot star animation after each correct sequence
143130 pixels .fill (0 )
144- pixels .show ()
145131 time .sleep (1 )
146-
147- while True :
148- play_game ()
0 commit comments