generated from mwc/project_game
Last milestone completed
Completed the project. I wanted to replace the player with a penguin, but it didn't fit the maze, so I kept it as an '>'. It shows where you begin and where you need to go to finish, and each game is a different maze. I thought this was very difficult and i needed to learn how to make a maze, I was not prepared for that.
This commit is contained in:
BIN
__pycache__/markers.cpython-312.pyc
Normal file
BIN
__pycache__/markers.cpython-312.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -3,15 +3,13 @@ import random
|
|||||||
from retro.game import Game
|
from retro.game import Game
|
||||||
from player import Player
|
from player import Player
|
||||||
from wall import Wall
|
from wall import Wall
|
||||||
|
from markers import StartMarker, FinishMarker
|
||||||
|
|
||||||
|
|
||||||
def generate_maze_grid(width, height):
|
def generate_maze_grid(width, height):
|
||||||
"""
|
"""
|
||||||
Generate a 'perfect' maze on a width x height grid.
|
Generate a 'perfect' maze on a width x height grid.
|
||||||
1 = wall, 0 = passage.
|
1 = wall, 0 = passage.
|
||||||
|
|
||||||
Uses depth-first search on a grid of cells spaced 2 apart,
|
|
||||||
which gives a nice twisty maze like your example image.
|
|
||||||
"""
|
"""
|
||||||
grid = [[1 for _ in range(width)] for _ in range(height)]
|
grid = [[1 for _ in range(width)] for _ in range(height)]
|
||||||
|
|
||||||
@@ -63,26 +61,35 @@ def create_maze_walls(board_size):
|
|||||||
def main():
|
def main():
|
||||||
board_size = (100, 40)
|
board_size = (100, 40)
|
||||||
|
|
||||||
player = Player(board_size)
|
|
||||||
|
|
||||||
walls, end_position = create_maze_walls(board_size)
|
walls, end_position = create_maze_walls(board_size)
|
||||||
print("Exit (goal) will be around:", end_position)
|
print("Exit (goal) will be around:", end_position)
|
||||||
|
|
||||||
|
start_position = (1, 1)
|
||||||
|
|
||||||
|
player = Player(board_size, end_position)
|
||||||
|
|
||||||
|
start_marker = StartMarker(start_position)
|
||||||
|
finish_marker = FinishMarker(end_position)
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
"win": False,
|
"win": False,
|
||||||
|
"message": "",
|
||||||
}
|
}
|
||||||
|
|
||||||
game = Game(
|
game = Game(
|
||||||
agents=[player] + walls,
|
agents=[
|
||||||
|
start_marker,
|
||||||
|
finish_marker,
|
||||||
|
player,
|
||||||
|
] + walls,
|
||||||
state=state,
|
state=state,
|
||||||
board_size=board_size,
|
board_size=board_size,
|
||||||
debug=True,
|
debug=False,
|
||||||
framerate=24,
|
framerate=24,
|
||||||
color="white_on_black",
|
color="white_on_black",
|
||||||
)
|
)
|
||||||
|
|
||||||
game.play()
|
game.play()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
24
markers.py
Normal file
24
markers.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
class StartMarker:
|
||||||
|
"""
|
||||||
|
Labels the start of the maze with 's'.
|
||||||
|
"""
|
||||||
|
character = "S"
|
||||||
|
color= "red_on_white"
|
||||||
|
blocks_movement = False
|
||||||
|
|
||||||
|
def __init__(self, position):
|
||||||
|
# position is a tuple (x, y)
|
||||||
|
self.position = position
|
||||||
|
|
||||||
|
|
||||||
|
class FinishMarker:
|
||||||
|
"""
|
||||||
|
Labels the finish of the maze with 'f'.
|
||||||
|
"""
|
||||||
|
character = "F"
|
||||||
|
color = "red_on_black"
|
||||||
|
blocks_movement = False
|
||||||
|
|
||||||
|
def __init__(self, position):
|
||||||
|
# position is a tuple (x, y)
|
||||||
|
self.position = position
|
||||||
32
player.py
32
player.py
@@ -1,21 +1,36 @@
|
|||||||
|
|
||||||
class Player:
|
class Player:
|
||||||
"""
|
"""
|
||||||
Player agent for Escape the Maze.
|
Player agent for Escape the Maze.
|
||||||
Moves one step at a time using the arrow keys.
|
Moves one step at a time using the arrow keys.
|
||||||
|
Ends the game when it reaches the goal position.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
name = "player"
|
name = "player"
|
||||||
character = ">" # 🐧Penguin icon
|
character = ">"
|
||||||
|
|
||||||
def __init__(self, board_size):
|
def __init__(self, board_size, goal_position):
|
||||||
# Start near the top-left corner
|
# Start near the top-left corner
|
||||||
width, height = board_size
|
width, height = board_size
|
||||||
self.position = (1, 1)
|
self.position = (1, 1)
|
||||||
|
self.goal_position = goal_position
|
||||||
|
|
||||||
|
def can_move_to(self, position, game):
|
||||||
|
"""
|
||||||
|
Returns True if there is no blocking agent (e.g., Wall) on this tile.
|
||||||
|
Non-blocking agents (like start/finish markers) are ignored.
|
||||||
|
"""
|
||||||
|
for agent in game.agents:
|
||||||
|
agent_pos = getattr(agent, "position", None)
|
||||||
|
if agent_pos == position:
|
||||||
|
if getattr(agent, "blocks_movement", False):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
def handle_keystroke(self, keystroke, game):
|
def handle_keystroke(self, keystroke, game):
|
||||||
"""
|
"""
|
||||||
Called once for each key pressed since the last turn.
|
Called for each key pressed since the last turn.
|
||||||
Moves the player using the arrow keys.
|
Moves the player using the arrow keys and checks for win.
|
||||||
"""
|
"""
|
||||||
x, y = self.position
|
x, y = self.position
|
||||||
new_position = None
|
new_position = None
|
||||||
@@ -29,10 +44,13 @@ class Player:
|
|||||||
elif keystroke.name == "KEY_RIGHT":
|
elif keystroke.name == "KEY_RIGHT":
|
||||||
new_position = (x + 1, y)
|
new_position = (x + 1, y)
|
||||||
|
|
||||||
|
|
||||||
if new_position is None:
|
if new_position is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if game.on_board(new_position) and self.can_move_to(new_position, game):
|
||||||
if game.on_board(new_position) and game.is_empty(new_position):
|
|
||||||
self.position = new_position
|
self.position = new_position
|
||||||
|
|
||||||
|
if self.position == self.goal_position:
|
||||||
|
game.state["win"] = True
|
||||||
|
game.state["message"] = "Congratulations! You've escaped the maze!"
|
||||||
|
game.end()
|
||||||
1
wall.py
1
wall.py
@@ -3,6 +3,7 @@ class Wall:
|
|||||||
A simple wall tile. It just sits on the board and blocks movement.
|
A simple wall tile. It just sits on the board and blocks movement.
|
||||||
"""
|
"""
|
||||||
character = "█"
|
character = "█"
|
||||||
|
blocks_movement = True
|
||||||
def __init__(self, position):
|
def __init__(self, position):
|
||||||
# position is a tuple (x, y)
|
# position is a tuple (x, y)
|
||||||
self.position = position
|
self.position = position
|
||||||
Reference in New Issue
Block a user