Skip to content

Commit d9d5c13

Browse files
committed
Change gamepadshift to keypad
1 parent 99a7e2d commit d9d5c13

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

PyBadge_Blinka_Jump_Game/code.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,27 @@
77
import adafruit_imageload
88
import digitalio
99
import simpleio
10-
from gamepadshift import GamePadShift
10+
from keypad import ShiftRegisterKeys, Event
1111
from adafruit_display_text import label
1212

1313
# 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)
2831
last_read = 0
2932

3033
# enables speaker
@@ -201,12 +204,14 @@ def life():
201204
blue = 0
202205
smoke = 0
203206
monster = 0
207+
# jump button press state
208+
jump_pressed = False
204209

205210
while True:
206211

207212
# checks if button has been pressed
208213
if (last_read + 0.01) < time.monotonic():
209-
buttons = pad.get_pressed()
214+
pad.events.get_into(latest_event)
210215
last_read = time.monotonic()
211216
# new game
212217
if new_game and not game_over:
@@ -221,8 +226,8 @@ def life():
221226
new_game_text.text = "BLINKA JUMP"
222227
life()
223228
# 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:
226231
# prepares display for gameplay
227232
print("start game")
228233
new_game_text.text = " "
@@ -302,9 +307,13 @@ def life():
302307

303308
# if the A button is pressed then Blinka is no longer in the default
304309
# 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
307313
snake = False
314+
else:
315+
jump_pressed = False
316+
snake = True
308317

309318
# heart sprites are displayed to show life count
310319
life()
@@ -350,10 +359,12 @@ def life():
350359
# special victory tone is played
351360
simpleio.tone(board.SPEAKER, 523.25, 0.005)
352361
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
357368

358369
# if there are no more lives, the game is over
359370
if game_over and not new_game:
@@ -370,8 +381,8 @@ def life():
370381
end = True
371382

372383
# 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:
375386
# display, states and score are reset for gameplay
376387
game_over_text.text = " "
377388
life_count = 3

0 commit comments

Comments
 (0)