generated from mwc/project_game
Compare commits
5 Commits
7907a4cad3
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2af3d6c2e7 | ||
|
|
e2273773eb | ||
|
|
f0840a02fc | ||
|
|
cdafa795fc | ||
|
|
bce3c8508c |
BIN
__pycache__/markers.cpython-312.pyc
Normal file
BIN
__pycache__/markers.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/maze_layout.cpython-312.pyc
Normal file
BIN
__pycache__/maze_layout.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/player.cpython-312.pyc
Normal file
BIN
__pycache__/player.cpython-312.pyc
Normal file
Binary file not shown.
BIN
__pycache__/wall.cpython-312.pyc
Normal file
BIN
__pycache__/wall.cpython-312.pyc
Normal file
Binary file not shown.
95
escape_the_maze.py
Normal file
95
escape_the_maze.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import random
|
||||
|
||||
from retro.game import Game
|
||||
from player import Player
|
||||
from wall import Wall
|
||||
from markers import StartMarker, FinishMarker
|
||||
|
||||
|
||||
def generate_maze_grid(width, height):
|
||||
"""
|
||||
Generate a 'perfect' maze on a width x height grid.
|
||||
1 = wall, 0 = passage.
|
||||
"""
|
||||
grid = [[1 for _ in range(width)] for _ in range(height)]
|
||||
|
||||
for y in range(1, height - 1, 2):
|
||||
for x in range(1, width - 1, 2):
|
||||
grid[y][x] = 0
|
||||
|
||||
visited = set()
|
||||
|
||||
def dfs(cx, cy):
|
||||
visited.add((cx, cy))
|
||||
|
||||
directions = [(2, 0), (-2, 0), (0, 2), (0, -2)]
|
||||
random.shuffle(directions)
|
||||
|
||||
for dx, dy in directions:
|
||||
nx, ny = cx + dx, cy + dy
|
||||
if 1 <= nx < width - 1 and 1 <= ny < height - 1 and (nx, ny) not in visited:
|
||||
wall_x = cx + dx // 2
|
||||
wall_y = cy + dy // 2
|
||||
grid[wall_y][wall_x] = 0
|
||||
dfs(nx, ny)
|
||||
|
||||
dfs(1, 1)
|
||||
|
||||
return grid
|
||||
|
||||
|
||||
def create_maze_walls(board_size):
|
||||
width, height = board_size
|
||||
|
||||
grid = generate_maze_grid(width, height)
|
||||
|
||||
center_x = (width // 2) | 1
|
||||
center_y = (height // 2) | 1
|
||||
grid[center_y][center_x] = 0
|
||||
end_position = (center_x, center_y)
|
||||
|
||||
wall_positions = []
|
||||
for y in range(height):
|
||||
for x in range(width):
|
||||
if grid[y][x] == 1:
|
||||
wall_positions.append((x, y))
|
||||
|
||||
walls = [Wall(pos) for pos in wall_positions]
|
||||
return walls, end_position
|
||||
|
||||
|
||||
def main():
|
||||
board_size = (100, 40)
|
||||
|
||||
walls, end_position = create_maze_walls(board_size)
|
||||
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 = {
|
||||
"win": False,
|
||||
"message": "",
|
||||
}
|
||||
|
||||
game = Game(
|
||||
agents=[
|
||||
start_marker,
|
||||
finish_marker,
|
||||
player,
|
||||
] + walls,
|
||||
state=state,
|
||||
board_size=board_size,
|
||||
debug=False,
|
||||
framerate=24,
|
||||
color="white_on_black",
|
||||
)
|
||||
|
||||
game.play()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
8
game_signature.py
Normal file
8
game_signature.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from retro.game import Game
|
||||
import inspect
|
||||
|
||||
print("Game.__init__ signature:")
|
||||
print(inspect.signature(Game.__init__))
|
||||
|
||||
print("\nFull source of Game.__init__:\n")
|
||||
print(inspect.getsource(Game.__init__))
|
||||
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
|
||||
56
player.py
Normal file
56
player.py
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
class Player:
|
||||
"""
|
||||
Player agent for Escape the Maze.
|
||||
Moves one step at a time using the arrow keys.
|
||||
Ends the game when it reaches the goal position.
|
||||
"""
|
||||
|
||||
name = "player"
|
||||
character = ">"
|
||||
|
||||
def __init__(self, board_size, goal_position):
|
||||
# Start near the top-left corner
|
||||
width, height = board_size
|
||||
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):
|
||||
"""
|
||||
Called for each key pressed since the last turn.
|
||||
Moves the player using the arrow keys and checks for win.
|
||||
"""
|
||||
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 self.can_move_to(new_position, game):
|
||||
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()
|
||||
31
proposal.md
Normal file
31
proposal.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# Game Proposal
|
||||
|
||||
## Game Overview
|
||||
Title: Escape the Maze
|
||||
|
||||
Vision: A puzzle style game where the player must complete a maze. I think it's compelling because it combines problem solving and interactive play.
|
||||
|
||||
Layout: A grid based maze with a player and exit.
|
||||
|
||||
Objective: Player uses arrow keys to navigate and end the game by reaching the exit.
|
||||
|
||||
## Core Mechanics
|
||||
- Player movement: Move one step at a time using arrow keys.
|
||||
- Collision detection: Prevents player from walking through walls.
|
||||
- Win condition: End the game when the player reaches the exit.
|
||||
|
||||
## Milestone
|
||||
1. Player agent moves correctly.
|
||||
2. Walls block movement.
|
||||
3. Exit agent ends the game when reached.
|
||||
6. (Optional) Add scoring, timer, or multiple levels.
|
||||
|
||||
## Challenges
|
||||
- Designing the maze
|
||||
- Implementing collision detection
|
||||
- Adding optional features
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user