Skip to content

Commit 09d0b62

Browse files
authored
Create maze.py
1 parent ebab023 commit 09d0b62

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Shadow-Labyrinth/maze.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pygame
2+
import random
3+
4+
TILE_SIZE = 40
5+
WALL_COLOR = (255, 255, 255)
6+
7+
class Wall(pygame.sprite.Sprite):
8+
def __init__(self, x, y):
9+
super().__init__()
10+
self.image = pygame.Surface((TILE_SIZE, TILE_SIZE))
11+
self.image.fill(WALL_COLOR)
12+
self.rect = self.image.get_rect(topleft=(x * TILE_SIZE, y * TILE_SIZE))
13+
14+
def generate_maze(width, height):
15+
maze = [[1] * width for _ in range(height)]
16+
def carve(x, y):
17+
maze[y][x] = 0
18+
dirs = [(0, -2), (0, 2), (-2, 0), (2, 0)]
19+
random.shuffle(dirs)
20+
for dx, dy in dirs:
21+
nx, ny = x + dx, y + dy
22+
if 0 <= nx < width and 0 <= ny < height and maze[ny][nx] == 1:
23+
maze[y + dy // 2][x + dx // 2] = 0
24+
carve(nx, ny)
25+
carve(1, 1)
26+
maze[1][1] = 0
27+
maze[height - 2][width - 2] = 0
28+
29+
walls = pygame.sprite.Group()
30+
for y in range(height):
31+
for x in range(width):
32+
if maze[y][x] == 1:
33+
walls.add(Wall(x, y))
34+
return walls

0 commit comments

Comments
 (0)