7
7
import adafruit_imageload
8
8
import digitalio
9
9
import simpleio
10
- from gamepadshift import GamePadShift
10
+ from keypad import ShiftRegisterKeys , Event
11
11
from adafruit_display_text import label
12
12
13
13
# setup for PyBadge buttons
14
- BUTTON_LEFT = const (128 )
15
- BUTTON_UP = const (64 )
16
- BUTTON_DOWN = const (32 )
17
- BUTTON_RIGHT = const (16 )
18
- BUTTON_SEL = const (8 )
19
- BUTTON_START = const (4 )
20
- BUTTON_A = const (2 )
21
- BUTTON_B = const (1 )
22
-
23
- pad = GamePadShift (digitalio .DigitalInOut (board .BUTTON_CLOCK ),
24
- digitalio .DigitalInOut (board .BUTTON_OUT ),
25
- digitalio .DigitalInOut (board .BUTTON_LATCH ))
26
-
27
- current_buttons = pad .get_pressed ()
14
+ BUTTON_LEFT = const (7 )
15
+ BUTTON_UP = const (6 )
16
+ BUTTON_DOWN = const (5 )
17
+ BUTTON_RIGHT = const (4 )
18
+ BUTTON_SEL = const (3 )
19
+ BUTTON_START = const (2 )
20
+ BUTTON_A = const (1 )
21
+ BUTTON_B = const (0 )
22
+
23
+ pad = ShiftRegisterKeys (clock = board .BUTTON_CLOCK ,
24
+ data = board .BUTTON_OUT ,
25
+ latch = board .BUTTON_LATCH ,
26
+ key_count = 8 ,
27
+ value_when_pressed = True ,
28
+ max_events = 1 )
29
+
30
+ latest_event = Event (key_number = 8 )
28
31
last_read = 0
29
32
30
33
# enables speaker
@@ -201,12 +204,14 @@ def life():
201
204
blue = 0
202
205
smoke = 0
203
206
monster = 0
207
+ # jump button press state
208
+ jump_pressed = False
204
209
205
210
while True :
206
211
207
212
# checks if button has been pressed
208
213
if (last_read + 0.01 ) < time .monotonic ():
209
- buttons = pad .get_pressed ( )
214
+ pad .events . get_into ( latest_event )
210
215
last_read = time .monotonic ()
211
216
# new game
212
217
if new_game and not game_over :
@@ -221,8 +226,8 @@ def life():
221
226
new_game_text .text = "BLINKA JUMP"
222
227
life ()
223
228
# if start is pressed...
224
- if current_buttons != buttons :
225
- if buttons & BUTTON_START :
229
+ if latest_event :
230
+ if latest_event . key_number == BUTTON_START :
226
231
# prepares display for gameplay
227
232
print ("start game" )
228
233
new_game_text .text = " "
@@ -302,9 +307,13 @@ def life():
302
307
303
308
# if the A button is pressed then Blinka is no longer in the default
304
309
# slither animation aka she jumps
305
- if current_buttons != buttons :
306
- if buttons & BUTTON_A :
310
+ if latest_event .key_number == BUTTON_A :
311
+ if latest_event .pressed :
312
+ jump_pressed = True
307
313
snake = False
314
+ else :
315
+ jump_pressed = False
316
+ snake = True
308
317
309
318
# heart sprites are displayed to show life count
310
319
life ()
@@ -350,10 +359,12 @@ def life():
350
359
# special victory tone is played
351
360
simpleio .tone (board .SPEAKER , 523.25 , 0.005 )
352
361
simpleio .tone (board .SPEAKER , 783.99 , 0.005 )
353
- # resets back to Blinka animation
354
- snake = True
355
- # resets that Blinka has not jumped over a Sparky
356
- cleared = False
362
+
363
+ if not jump_pressed :
364
+ # resets back to Blinka animation
365
+ snake = True
366
+ # resets that Blinka has not jumped over a Sparky
367
+ cleared = False
357
368
358
369
# if there are no more lives, the game is over
359
370
if game_over and not new_game :
@@ -370,8 +381,8 @@ def life():
370
381
end = True
371
382
372
383
# if the start button is pressed...
373
- if ( current_buttons != buttons ) and game_over :
374
- if buttons & BUTTON_START :
384
+ if latest_event and game_over :
385
+ if latest_event . key_number == BUTTON_START :
375
386
# display, states and score are reset for gameplay
376
387
game_over_text .text = " "
377
388
life_count = 3
0 commit comments