Skip to content

Commit c7069c7

Browse files
committed
do not allow player to move outside the map. fix (a) buttonlistener for pybadge
1 parent ee6647a commit c7069c7

File tree

1 file changed

+44
-36
lines changed
  • Tilemap_Game_With_CircuitPython

1 file changed

+44
-36
lines changed

Tilemap_Game_With_CircuitPython/code.py

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -289,41 +289,49 @@ def load_map(file_name):
289289
# helper function returns true if player is allowed to move given direction
290290
# based on can_walk property of the tiles next to the player
291291
def can_player_move(direction):
292-
if direction == UP:
293-
tile_above_coords = (
294-
GAME_STATE["PLAYER_LOC"][0],
295-
GAME_STATE["PLAYER_LOC"][1] - 1,
296-
)
297-
return TILES[
298-
GAME_STATE["CURRENT_MAP"][tile_above_coords[0], tile_above_coords[1]]
299-
]["can_walk"]
300-
301-
if direction == DOWN:
302-
tile_below_coords = (
303-
GAME_STATE["PLAYER_LOC"][0],
304-
GAME_STATE["PLAYER_LOC"][1] + 1,
305-
)
306-
return TILES[
307-
GAME_STATE["CURRENT_MAP"][tile_below_coords[0], tile_below_coords[1]]
308-
]["can_walk"]
309-
310-
if direction == LEFT:
311-
tile_left_of_coords = (
312-
GAME_STATE["PLAYER_LOC"][0] - 1,
313-
GAME_STATE["PLAYER_LOC"][1],
314-
)
315-
return TILES[
316-
GAME_STATE["CURRENT_MAP"][tile_left_of_coords[0], tile_left_of_coords[1]]
317-
]["can_walk"]
318-
319-
if direction == RIGHT:
320-
tile_right_of_coords = (
321-
GAME_STATE["PLAYER_LOC"][0] + 1,
322-
GAME_STATE["PLAYER_LOC"][1],
323-
)
324-
return TILES[
325-
GAME_STATE["CURRENT_MAP"][tile_right_of_coords[0], tile_right_of_coords[1]]
326-
]["can_walk"]
292+
try:
293+
if direction == UP:
294+
tile_above_coords = (
295+
GAME_STATE["PLAYER_LOC"][0],
296+
GAME_STATE["PLAYER_LOC"][1] - 1,
297+
)
298+
299+
return TILES[
300+
GAME_STATE["CURRENT_MAP"][tile_above_coords[0], tile_above_coords[1]]
301+
]["can_walk"]
302+
303+
if direction == DOWN:
304+
tile_below_coords = (
305+
GAME_STATE["PLAYER_LOC"][0],
306+
GAME_STATE["PLAYER_LOC"][1] + 1,
307+
)
308+
return TILES[
309+
GAME_STATE["CURRENT_MAP"][tile_below_coords[0], tile_below_coords[1]]
310+
]["can_walk"]
311+
312+
if direction == LEFT:
313+
tile_left_of_coords = (
314+
GAME_STATE["PLAYER_LOC"][0] - 1,
315+
GAME_STATE["PLAYER_LOC"][1],
316+
)
317+
return TILES[
318+
GAME_STATE["CURRENT_MAP"][
319+
tile_left_of_coords[0], tile_left_of_coords[1]
320+
]
321+
]["can_walk"]
322+
323+
if direction == RIGHT:
324+
tile_right_of_coords = (
325+
GAME_STATE["PLAYER_LOC"][0] + 1,
326+
GAME_STATE["PLAYER_LOC"][1],
327+
)
328+
return TILES[
329+
GAME_STATE["CURRENT_MAP"][
330+
tile_right_of_coords[0], tile_right_of_coords[1]
331+
]
332+
]["can_walk"]
333+
except KeyError:
334+
return False
327335

328336
return None
329337

@@ -462,7 +470,7 @@ def show_splash(new_text, color, vertical_offset=18):
462470
cur_down = cur_btn_vals & ugame.K_DOWN
463471
cur_right = cur_btn_vals & ugame.K_RIGHT
464472
cur_left = cur_btn_vals & ugame.K_LEFT
465-
cur_a = cur_btn_vals & ugame.K_O
473+
cur_a = cur_btn_vals & ugame.K_O or cur_btn_vals & ugame.K_X
466474

467475
if GAME_STATE["STATE"] == STATE_WAITING:
468476
print(cur_a)

0 commit comments

Comments
 (0)