11
11
import adafruit_imageload
12
12
import digitalio
13
13
import simpleio
14
- from gamepadshift import GamePadShift
14
+ from keypad import ShiftRegisterKeys , Event
15
15
from adafruit_display_text import label
16
16
17
17
# setup for PyBadge buttons
18
- BUTTON_LEFT = const (128 )
19
- BUTTON_UP = const (64 )
20
- BUTTON_DOWN = const (32 )
21
- BUTTON_RIGHT = const (16 )
22
- BUTTON_SEL = const (8 )
23
- BUTTON_START = const (4 )
24
- BUTTON_A = const (2 )
25
- BUTTON_B = const (1 )
26
-
27
- pad = GamePadShift (digitalio .DigitalInOut (board .BUTTON_CLOCK ),
28
- digitalio .DigitalInOut (board .BUTTON_OUT ),
29
- digitalio .DigitalInOut (board .BUTTON_LATCH ))
30
-
31
- current_buttons = pad .get_pressed ()
18
+ BUTTON_LEFT = const (7 )
19
+ BUTTON_UP = const (6 )
20
+ BUTTON_DOWN = const (5 )
21
+ BUTTON_RIGHT = const (4 )
22
+ BUTTON_SEL = const (3 )
23
+ BUTTON_START = const (2 )
24
+ BUTTON_A = const (1 )
25
+ BUTTON_B = const (0 )
26
+
27
+ pad = ShiftRegisterKeys (clock = board .BUTTON_CLOCK ,
28
+ data = board .BUTTON_OUT ,
29
+ latch = board .BUTTON_LATCH ,
30
+ key_count = 8 ,
31
+ value_when_pressed = True ,
32
+ max_events = 1 )
33
+
34
+ latest_event = Event (key_number = 8 )
32
35
last_read = 0
33
36
34
37
# enables speaker
@@ -205,12 +208,14 @@ def life():
205
208
blue = 0
206
209
smoke = 0
207
210
monster = 0
211
+ # jump button press state
212
+ jump_pressed = False
208
213
209
214
while True :
210
215
211
216
# checks if button has been pressed
212
217
if (last_read + 0.01 ) < time .monotonic ():
213
- buttons = pad .get_pressed ( )
218
+ pad .events . get_into ( latest_event )
214
219
last_read = time .monotonic ()
215
220
# new game
216
221
if new_game and not game_over :
@@ -221,12 +226,12 @@ def life():
221
226
sparky0_grid .x = 5
222
227
sparky1_grid .x = 40
223
228
sparky2_grid .x = 65
224
- score_area .text = 300
229
+ score_area .text = str ( 300 )
225
230
new_game_text .text = "BLINKA JUMP"
226
231
life ()
227
232
# if start is pressed...
228
- if current_buttons != buttons :
229
- if buttons & BUTTON_START :
233
+ if latest_event :
234
+ if latest_event . key_number == BUTTON_START :
230
235
# prepares display for gameplay
231
236
print ("start game" )
232
237
new_game_text .text = " "
@@ -245,7 +250,7 @@ def life():
245
250
# adds 10 points every time a Sparky is cleared
246
251
total_score = score + jump_score
247
252
# displays score as text
248
- score_area .text = int (total_score )
253
+ score_area .text = str ( int (total_score ) )
249
254
250
255
# puts Sparky states and x location into callable arrays
251
256
for s in range (3 ):
@@ -306,9 +311,13 @@ def life():
306
311
307
312
# if the A button is pressed then Blinka is no longer in the default
308
313
# slither animation aka she jumps
309
- if current_buttons != buttons :
310
- if buttons & BUTTON_A :
314
+ if latest_event .key_number == BUTTON_A :
315
+ if latest_event .pressed :
316
+ jump_pressed = True
311
317
snake = False
318
+ else :
319
+ jump_pressed = False
320
+ snake = True
312
321
313
322
# heart sprites are displayed to show life count
314
323
life ()
@@ -354,10 +363,12 @@ def life():
354
363
# special victory tone is played
355
364
simpleio .tone (board .SPEAKER , 523.25 , 0.005 )
356
365
simpleio .tone (board .SPEAKER , 783.99 , 0.005 )
357
- # resets back to Blinka animation
358
- snake = True
359
- # resets that Blinka has not jumped over a Sparky
360
- cleared = False
366
+
367
+ if not jump_pressed :
368
+ # resets back to Blinka animation
369
+ snake = True
370
+ # resets that Blinka has not jumped over a Sparky
371
+ cleared = False
361
372
362
373
# if there are no more lives, the game is over
363
374
if game_over and not new_game :
@@ -374,8 +385,8 @@ def life():
374
385
end = True
375
386
376
387
# if the start button is pressed...
377
- if ( current_buttons != buttons ) and game_over :
378
- if buttons & BUTTON_START :
388
+ if latest_event and game_over :
389
+ if latest_event . key_number == BUTTON_START :
379
390
# display, states and score are reset for gameplay
380
391
game_over_text .text = " "
381
392
life_count = 3
0 commit comments