-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson2.1.py
103 lines (83 loc) · 3.08 KB
/
lesson2.1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import random
# Initialize Pygame without the start message
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
# Constants
TILE_SIZE = 32
GRID_SIZE = 32
SCREEN_SIZE = TILE_SIZE * GRID_SIZE
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((SCREEN_SIZE, SCREEN_SIZE))
pygame.display.set_caption("Tile Maze Generation")
# Create tile surfaces
def create_tile(right_line=True, bottom_line=True):
tile = pygame.Surface((TILE_SIZE, TILE_SIZE))
tile.fill((150, 150, 150)) # Grey background
if bottom_line:
pygame.draw.line(tile, (0, 0, 0), (0, TILE_SIZE - 1), (TILE_SIZE - 1, TILE_SIZE - 1), 2) # Bottom line
if right_line:
pygame.draw.line(tile, (0, 0, 0), (TILE_SIZE - 1, 0), (TILE_SIZE - 1, TILE_SIZE - 1), 2) # Right line
return tile
# Create all tile variations
tile_default = create_tile(True, True) # Both lines
tile_no_right = create_tile(False, True) # No right line
tile_no_bottom = create_tile(True, False) # No bottom line
tile_open = create_tile(False, False) # No lines
# Grid to store tile types
tile_grid = [[tile_default for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
# Directions for movement
DIRECTIONS = {
"N": (0, -1),
"S": (0, 1),
"E": (1, 0),
"W": (-1, 0)
}
# Create stack and visited set
stack = []
visited = set()
# Start at (0,0)
current_x, current_y = 0, 0
visited.add((current_x, current_y))
stack.append((current_x, current_y))
# Maze generation loop
running = True
while running:
screen.fill((0, 0, 0)) # Black background
# Draw the grid with updated tiles
for row in range(GRID_SIZE):
for col in range(GRID_SIZE):
screen.blit(tile_grid[row][col], (col * TILE_SIZE, row * TILE_SIZE))
pygame.display.flip()
# Check possible unvisited neighbors
neighbors = []
for direction, (dx, dy) in DIRECTIONS.items():
nx, ny = current_x + dx, current_y + dy
if 0 <= nx < GRID_SIZE and 0 <= ny < GRID_SIZE and (nx, ny) not in visited:
neighbors.append((nx, ny, direction))
if neighbors:
# Choose a random unvisited neighbor
next_x, next_y, direction = random.choice(neighbors)
# Update the tile based on movement
if direction == "E":
tile_grid[current_y][current_x] = tile_no_right # Remove right line
elif direction == "S":
tile_grid[current_y][current_x] = tile_no_bottom # Remove bottom line
# Mark as visited and continue
visited.add((next_x, next_y))
stack.append((next_x, next_y))
current_x, current_y = next_x, next_y
else:
# Backtrack if no unvisited neighbors
if stack:
current_x, current_y = stack.pop()
else:
running = False # Exit when all tiles visited
# Wait for a keypress or mouse click before quitting
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN:
waiting = False # Exit on keypress or mouse click
pygame.quit()