Skip to content

Commit 890431b

Browse files
authored
Merge pull request #1978 from tekktrik/feature/remove-gamepadshift-jump-game
Remove gamepadshift from PyBadge jump game example
2 parents 3dab0b2 + 0bbbd05 commit 890431b

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

PyBadge_Blinka_Jump_Game/code.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,27 @@
1111
import adafruit_imageload
1212
import digitalio
1313
import simpleio
14-
from gamepadshift import GamePadShift
14+
from keypad import ShiftRegisterKeys, Event
1515
from adafruit_display_text import label
1616

1717
# 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)
3235
last_read = 0
3336

3437
# enables speaker
@@ -205,12 +208,14 @@ def life():
205208
blue = 0
206209
smoke = 0
207210
monster = 0
211+
# jump button press state
212+
jump_pressed = False
208213

209214
while True:
210215

211216
# checks if button has been pressed
212217
if (last_read + 0.01) < time.monotonic():
213-
buttons = pad.get_pressed()
218+
pad.events.get_into(latest_event)
214219
last_read = time.monotonic()
215220
# new game
216221
if new_game and not game_over:
@@ -221,12 +226,12 @@ def life():
221226
sparky0_grid.x = 5
222227
sparky1_grid.x = 40
223228
sparky2_grid.x = 65
224-
score_area.text = 300
229+
score_area.text = str(300)
225230
new_game_text.text = "BLINKA JUMP"
226231
life()
227232
# 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:
230235
# prepares display for gameplay
231236
print("start game")
232237
new_game_text.text = " "
@@ -245,7 +250,7 @@ def life():
245250
# adds 10 points every time a Sparky is cleared
246251
total_score = score + jump_score
247252
# displays score as text
248-
score_area.text = int(total_score)
253+
score_area.text = str(int(total_score))
249254

250255
# puts Sparky states and x location into callable arrays
251256
for s in range(3):
@@ -306,9 +311,13 @@ def life():
306311

307312
# if the A button is pressed then Blinka is no longer in the default
308313
# 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
311317
snake = False
318+
else:
319+
jump_pressed = False
320+
snake = True
312321

313322
# heart sprites are displayed to show life count
314323
life()
@@ -354,10 +363,12 @@ def life():
354363
# special victory tone is played
355364
simpleio.tone(board.SPEAKER, 523.25, 0.005)
356365
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
361372

362373
# if there are no more lives, the game is over
363374
if game_over and not new_game:
@@ -374,8 +385,8 @@ def life():
374385
end = True
375386

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

0 commit comments

Comments
 (0)