Files
project_game/player.py
ilmabura e2273773eb created the maze walls and collision detection
the hardest part was creating a maze with a solution. I realized I didn't know how to do that and had to google how to create a maze.
Once I had a maze generator, I was unsure of whether there was a solution to it. Figuring that out took some time. Then I changed the look to be more digestible. I chose a maze game because I thought it would be simple, but it didn't feel simple.
2025-12-07 12:32:24 -05:00

38 lines
1.1 KiB
Python

class Player:
"""
Player agent for Escape the Maze.
Moves one step at a time using the arrow keys.
"""
name = "player"
character = ">" # 🐧Penguin icon
def __init__(self, board_size):
# Start near the top-left corner
width, height = board_size
self.position = (1, 1)
def handle_keystroke(self, keystroke, game):
"""
Called once for each key pressed since the last turn.
Moves the player using the arrow keys.
"""
x, y = self.position
new_position = None
if keystroke.name == "KEY_UP":
new_position = (x, y - 1)
elif keystroke.name == "KEY_DOWN":
new_position = (x, y + 1)
elif keystroke.name == "KEY_LEFT":
new_position = (x - 1, y)
elif keystroke.name == "KEY_RIGHT":
new_position = (x + 1, y)
if new_position is None:
return
if game.on_board(new_position) and game.is_empty(new_position):
self.position = new_position