-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisplay_Maze_Generator.py
190 lines (139 loc) · 5.67 KB
/
Display_Maze_Generator.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#* **** MAZE GENERATOR ****
# by Shrey Biswas
#* IMPORTS
import random
import pygame
from time import sleep
from maze_objects import *
from screen_details import *
# pylint: disable=no-member
def build_blank_grid():
x = 20 #* x axis co-ordinates of the top left cell
y = 0 #* y axis co-ordinates of the top left cell
grid = []
cellsY = (screenHeight//cellWidth)-1 #* total number of cells fitting on y axis
cellsX = (screenWidth//cellWidth)-1 #* total number of cells fitting on x axis
for row_num in range(1,cellsY):
#* begins at top left corner, works horizontally and moves down in line
x = cellWidth #* reset to start of line
y += cellWidth #* move down to next row of cell
row = []
for col_num in range(1,cellsX):
cell = Node([x,y],cellWidth,window) #* create cell, add to row and draw
row.append(cell)
cell.draw(WHITE)
cell.position = (row_num,col_num)
x += cellWidth #* move to next cell in row
grid.append(row)
#* [a],[b],[c]
#* [d],[e],[f]
#* [g],[h],[i]
return grid
def add_blocks(grid,start=False,end=False):
setup = True
setup_stage = ['Start','Target','Block'] # weight1, weight2
if start:
start.draw(YELLOW)
start.cell_type = 'Start'
setup_stage.remove('Start')
if end:
end.draw(YELLOW)
end.cell_type = 'Target'
setup_stage.remove('Target')
setup_stage = iter(setup_stage)
setup_action = next(setup_stage)
print(setup_action)
selecting = False
while setup:
return grid
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and setup_action in ('Weight1','Block'):
try:
setup_action = next(setup_stage)
print(setup_action)
except:
return grid
elif event.type == pygame.MOUSEBUTTONDOWN:
if setup_action in ('Start','Target'): #* Selecting 'Start' or 'Target'
for row in grid:
for cell in row:
if cell.rect.collidepoint(event.pos):
cell.draw(YELLOW)
cell.cell_type = setup_action
setup_action = next(setup_stage) # advance if cell is clicked
print(setup_action)
continue
selecting = True #* if blocking or weighting, lets you hold down
elif event.type == pygame.MOUSEBUTTONUP:
selecting = False
elif event.type == pygame.MOUSEMOTION:
if selecting:
#* check where click is
for row in grid:
for cell in row:
if cell.rect.collidepoint(event.pos):
if setup_action == 'Block':
cell.draw(BLACK)
cell.cell_type = 'Block'
elif setup_action == 'Weight1':
cell.draw(GREY)
cell.weight += 1
def generate_maze(grid,animate=True): #* Depth-First Generation
for row in grid: # find start cell
for cell in row:
cell.children = [] # reset
cell.visited = False
if cell.cell_type == 'Start':
current_cell = cell
if animate:
current_cell.draw(YELLOW)
frontier = []
while True:
current_cell.visited = True
current_cell.children=[] # empty children, to rebuild in maze
current_cell.find_neighbours(grid,False) # maze cannot be diagonal
random.shuffle(current_cell.neighbours)
frontier = list(filter(lambda cell: cell not in current_cell.neighbours, frontier)) #* if neighbour is in frontier, move it to the end
frontier+=current_cell.neighbours
try:
next_cell = frontier.pop(-1)
except:
break # if frontier is empty, maze is complete
current_cell = backtrack(current_cell,next_cell,animate) #* if next_cell is not next to current_cell, finds proper parent
next_cell.parent = current_cell
current_cell.children.append(next_cell)
if current_cell.cell_type == 'Start':
print('start')
print(next_cell.corner)
print([cell.corner for cell in current_cell.children])
if animate:
next_cell.draw(CYAN)
pygame.display.update()
sleep(cellWidth/4000)
current_cell.connect(next_cell,GREEN,False) #diagonal is false if maze is called
current_cell = next_cell
if animate:
pygame.display.update()
sleep(cellWidth/2000)
pygame.display.update()
return grid
def backtrack(current_cell, next_cell,animate):
while current_cell not in next_cell.potential_parents: #* backtracks current_cell towards next_cell
if animate:
current_cell.draw(BLUE)
current_cell.parent.draw(CYAN)
sleep(cellWidth/3000)
pygame.display.update()
current_cell = current_cell.parent
return current_cell
#* main
if __name__ == '__main__':
grid = build_blank_grid()
grid = add_blocks(grid,start=grid[0][0],end=grid[-1][-1])
grid = generate_maze(grid,animate=True)
while True:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()