generated from mwc/project_game
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.
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -1,25 +1,88 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
from retro.game import Game
|
from retro.game import Game
|
||||||
from player import Player
|
from player import Player
|
||||||
|
from wall import Wall
|
||||||
|
|
||||||
|
|
||||||
|
def generate_maze_grid(width, height):
|
||||||
|
"""
|
||||||
|
Generate a 'perfect' maze on a width x height grid.
|
||||||
|
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)]
|
||||||
|
|
||||||
|
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():
|
def main():
|
||||||
board_size = (100, 40)
|
board_size = (100, 40)
|
||||||
|
|
||||||
player = Player(board_size)
|
player = Player(board_size)
|
||||||
|
|
||||||
|
walls, end_position = create_maze_walls(board_size)
|
||||||
|
print("Exit (goal) will be around:", end_position)
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
"win": False,
|
"win": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
game = Game(
|
game = Game(
|
||||||
agents=[player],
|
agents=[player] + walls,
|
||||||
state=state,
|
state=state,
|
||||||
board_size=board_size,
|
board_size=board_size,
|
||||||
debug=False,
|
debug=True,
|
||||||
framerate=24,
|
framerate=24,
|
||||||
color="white_on_black",
|
color="white_on_black",
|
||||||
)
|
)
|
||||||
|
|
||||||
game.play()
|
game.play()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
@@ -5,7 +5,7 @@ class Player:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
name = "player"
|
name = "player"
|
||||||
character = "🐧" # Penguin icon
|
character = ">" # 🐧Penguin icon
|
||||||
|
|
||||||
def __init__(self, board_size):
|
def __init__(self, board_size):
|
||||||
# Start near the top-left corner
|
# Start near the top-left corner
|
||||||
|
|||||||
Reference in New Issue
Block a user