Skip to content

Commit 4083b21

Browse files
Maze Generator and solver Script added
1 parent 08b7bdd commit 4083b21

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

Maze Generator and Solver/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Maze Generator and Solver
2+
3+
## Description
4+
This is a Python maze generator and solver game built using the Pygame library. The game generates random mazes and allows the player to navigate through them using arrow keys or WASD keys. The maze generation is based on the Depth-First Search (DFS) algorithm, and the maze solving uses the Breadth-First Search (BFS) algorithm.
5+
6+
## Features
7+
- Random maze generation using DFS algorithm
8+
- Maze navigation by the player using arrow keys or WASD keys
9+
- Maze solving using BFS algorithm
10+
- Reset option to generate a new maze
11+
- Timer to record the time taken to solve the maze
12+
- Visually appealing graphics and animations
13+
14+
## How to Play
15+
1. Run the script `maze_game.py` using Python (requires Python 3.x and Pygame library).
16+
2. The maze will be generated, and the player will start at the entrance.
17+
3. Use the arrow keys or WASD keys to navigate through the maze.
18+
4. Try to reach the exit point indicated by the green cell.
19+
5. If you get stuck or want to generate a new maze, press the 'r' key to reset.
20+
21+
## Dependencies
22+
- Python 3.x
23+
- Pygame library (Install using `pip install pygame`)
24+
25+
## Controls
26+
- Arrow keys or WASD keys: Move the player
27+
- 'r' key: Reset the maze
28+
29+
## Acknowledgments
30+
The maze generation algorithm is based on Depth-First Search (DFS), and the maze solving algorithm is based on Breadth-First Search (BFS).
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import pygame
2+
import random
3+
import time
4+
5+
MAZE_WIDTH, MAZE_HEIGHT = 20, 20
6+
CELL_SIZE = 30
7+
WINDOW_WIDTH, WINDOW_HEIGHT = MAZE_WIDTH * CELL_SIZE, MAZE_HEIGHT * CELL_SIZE
8+
9+
WHITE = (255, 255, 255)
10+
BLACK = (0, 0, 0)
11+
RED = (255, 0, 0)
12+
GREEN = (0, 255, 0)
13+
14+
class Cell:
15+
WALL = 0
16+
PATH = 1
17+
VISITED = 2
18+
19+
maze = [[Cell.WALL for _ in range(MAZE_WIDTH)] for _ in range(MAZE_HEIGHT)]
20+
21+
def generate_maze(x, y):
22+
maze[y][x] = Cell.VISITED
23+
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
24+
random.shuffle(directions)
25+
26+
for dx, dy in directions:
27+
nx, ny = x + 2 * dx, y + 2 * dy
28+
if 0 <= nx < MAZE_WIDTH and 0 <= ny < MAZE_HEIGHT and maze[ny][nx] == Cell.WALL:
29+
maze[y + dy][x + dx] = Cell.PATH
30+
generate_maze(nx, ny)
31+
32+
class Player:
33+
def __init__(self, x, y):
34+
self.x = x
35+
self.y = y
36+
37+
def move(self, dx, dy):
38+
new_x, new_y = self.x + dx, self.y + dy
39+
if 0 <= new_x < MAZE_WIDTH and 0 <= new_y < MAZE_HEIGHT and maze[new_y][new_x] != Cell.WALL:
40+
self.x = new_x
41+
self.y = new_y
42+
43+
player = Player(1, 1)
44+
45+
pygame.init()
46+
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
47+
pygame.display.set_caption("Maze Generator and Solver")
48+
49+
generate_maze(0, 0)
50+
start_point = (1, 1)
51+
end_point = (MAZE_WIDTH - 2, MAZE_HEIGHT - 2)
52+
path = []
53+
54+
running = True
55+
start_time = time.time()
56+
while running:
57+
for event in pygame.event.get():
58+
if event.type == pygame.QUIT:
59+
running = False
60+
elif event.type == pygame.KEYDOWN:
61+
if event.key == pygame.K_r:
62+
maze = [[Cell.WALL for _ in range(MAZE_WIDTH)] for _ in range(MAZE_HEIGHT)]
63+
generate_maze(0, 0)
64+
start_point = (1, 1)
65+
end_point = (MAZE_WIDTH - 2, MAZE_HEIGHT - 2)
66+
path = []
67+
player.x, player.y = start_point[0], start_point[1]
68+
elif event.key in (pygame.K_UP, pygame.K_w):
69+
player.move(0, -1)
70+
elif event.key in (pygame.K_DOWN, pygame.K_s):
71+
player.move(0, 1)
72+
elif event.key in (pygame.K_LEFT, pygame.K_a):
73+
player.move(-1, 0)
74+
elif event.key in (pygame.K_RIGHT, pygame.K_d):
75+
player.move(1, 0)
76+
77+
window.fill(WHITE)
78+
79+
for y in range(MAZE_HEIGHT):
80+
for x in range(MAZE_WIDTH):
81+
if maze[y][x] == Cell.WALL:
82+
pygame.draw.rect(window, BLACK, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
83+
elif maze[y][x] == Cell.PATH:
84+
pygame.draw.rect(window, WHITE, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
85+
elif maze[y][x] == Cell.VISITED:
86+
pygame.draw.rect(window, GREEN, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
87+
88+
for x, y in path:
89+
pygame.draw.rect(window, RED, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
90+
91+
pygame.draw.rect(window, GREEN, (end_point[0] * CELL_SIZE, end_point[1] * CELL_SIZE, CELL_SIZE, CELL_SIZE))
92+
93+
if (player.x, player.y) == end_point:
94+
elapsed_time = time.time() - start_time
95+
pygame.draw.rect(window, GREEN, (end_point[0] * CELL_SIZE, end_point[1] * CELL_SIZE, CELL_SIZE, CELL_SIZE))
96+
font = pygame.font.SysFont(None, 40)
97+
text = font.render(f"Congratulations! Time: {elapsed_time:.2f} seconds", True, BLACK)
98+
window.blit(text, (10, WINDOW_HEIGHT - 40))
99+
else:
100+
path = []
101+
visited = set()
102+
queue = [(start_point, [])]
103+
104+
while queue:
105+
(x, y), current_path = queue.pop(0)
106+
107+
if (x, y) == end_point:
108+
path = current_path
109+
break
110+
111+
visited.add((x, y))
112+
113+
for dx, dy in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
114+
nx, ny = x + dx, y + dy
115+
if 0 <= nx < MAZE_WIDTH and 0 <= ny < MAZE_HEIGHT and maze[ny][nx] in (Cell.PATH, Cell.VISITED) and (nx, ny) not in visited:
116+
queue.append(((nx, ny), current_path + [(x, y)]))
117+
visited.add((nx, ny))
118+
119+
pygame.display.update()
120+
121+
pygame.quit()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pygame==2.1.3

0 commit comments

Comments
 (0)