1
1
import os
2
- from typing import Callable
3
2
import maze
4
3
import algo
4
+ import cell
5
5
import pygame
6
6
import pygame_gui
7
- from grid import Grid , GREY
8
- from cell import Cell
7
+ from typing import Callable
9
8
from strenum import StrEnum
9
+ from grid import Grid , GREY
10
+ from legend_cell import LegendCell
11
+ from pygame_gui .core import ObjectID
10
12
11
13
12
14
# """1:1 cell size = 16px"""
18
20
# GRID_DIMENSIONS = (1680, 720) #px
19
21
20
22
21
-
22
23
WIDTH , HEIGHT = 1291 , 765
23
24
GRID_WIDTH , GRID_HEIGHT = 1280 , 680
24
25
GRID_SIZE = (34 , 64 ) #(rows, columns)
38
39
pygame .display .set_icon (LOGO )
39
40
40
41
41
- def draw (win , grid , UI_MANAGER , time_delta ) -> None :
42
+ def draw (win , grid , UI_MANAGER , time_delta , legend_cells ) -> None :
42
43
win .fill (BG_COLOR )
43
- grid .draw_under_grid_cells ()
44
+ grid .draw_under_grid_lines ()
44
45
grid .draw_grid_lines ()
45
- grid .draw_over_grid_cells ()
46
+ grid .draw_over_grid_lines ()
46
47
UI_MANAGER .update (time_delta )
47
-
48
- #grid frame lines
49
- pygame .draw .line (win , GREY , ((grid .x - 3 , grid .y )), (grid .x - 3 , grid .y + grid .height ), width = 5 )
50
- pygame .draw .line (win , GREY , ((grid .x + grid .width + 3 , grid .y )), (grid .x + grid .width + 3 , grid .y + grid .height ), width = 5 )
51
- pygame .draw .line (win , GREY , ((0 , grid .y - 2 )), (WIDTH , grid .y - 2 ), width = 5 )
52
- pygame .draw .line (win , GREY , ((0 , grid .y + grid .height + 2 )), (WIDTH , grid .y + grid .height + 2 ), width = 5 )
48
+ grid .draw_grid_frame ()
49
+
50
+ for legend_cell in legend_cells :
51
+ legend_cell .draw_legend_cell ()
53
52
54
53
UI_MANAGER .draw_ui (WIN )
55
54
pygame .display .update ()
@@ -61,22 +60,22 @@ class Algorithms(StrEnum):
61
60
DFS = "Depth-first Search"
62
61
BFS = "Breadth-first Search"
63
62
GBFS = "Greedy Best-first Search"
63
+ BBFS = "Bidirectional BFS"
64
64
65
65
class Mazes (StrEnum ):
66
66
RECDIV = "Recursive Division Maze"
67
67
RANDOM_DFS = "Randomized Depth-first Search Maze"
68
68
SPIRAL = "Spiral Maze"
69
-
70
69
71
70
72
71
def main () -> None :
73
72
clock = pygame .time .Clock ()
74
73
grid = Grid (WIN , GRID_SIZE , (GRID_WIDTH , GRID_HEIGHT ), GRID_POSITION )
75
74
76
75
#scales images to correct cell size
77
- Cell .scale_cell_imgs (grid .gap , grid .gap )
76
+ cell . Cell .scale_cell_imgs (grid .gap , grid .gap )
78
77
79
- draw_lambda = lambda : draw (WIN , grid , UI_MANAGER , time_delta )
78
+ draw_lambda = lambda : draw (WIN , grid , UI_MANAGER , time_delta , legend_cells )
80
79
81
80
82
81
#gui elements initialization
@@ -108,11 +107,45 @@ def main() -> None:
108
107
text = 'CE' ,
109
108
manager = UI_MANAGER )
110
109
111
- # legend_lable = pygame_gui.elements.UILabel(
112
- # relative_rect=pygame.Rect((grid.x, HEIGHT - 35), (100, 20)),
113
- # text="Legend: ",
114
- # manager=UI_MANAGER,
115
- # object_id="#legend_lable")
110
+ unvisited_cell_lable = pygame_gui .elements .UILabel (
111
+ relative_rect = pygame .Rect ((grid .x + grid .gap , HEIGHT - 35 ), (130 , grid .gap )),
112
+ text = "-unvisited cell" ,
113
+ manager = UI_MANAGER ,
114
+ object_id = ObjectID (class_id = "@legend_cell_lables" ))
115
+
116
+ visited_cell_lable = pygame_gui .elements .UILabel (
117
+ relative_rect = pygame .Rect ((grid .x + 200 + grid .gap , HEIGHT - 35 ), (115 , grid .gap )),
118
+ text = "-visited cell" ,
119
+ manager = UI_MANAGER ,
120
+ object_id = ObjectID (class_id = "@legend_cell_lables" ))
121
+
122
+ open_cell_lable = pygame_gui .elements .UILabel (
123
+ relative_rect = pygame .Rect ((grid .x + 400 + grid .gap , HEIGHT - 35 ), (90 , grid .gap )),
124
+ text = "-open cell" ,
125
+ manager = UI_MANAGER ,
126
+ object_id = ObjectID (class_id = "@legend_cell_lables" ))
127
+
128
+ path_cell_lable = pygame_gui .elements .UILabel (
129
+ relative_rect = pygame .Rect ((grid .x + 600 + grid .gap , HEIGHT - 35 ), (90 , grid .gap )),
130
+ text = "-path cell" ,
131
+ manager = UI_MANAGER ,
132
+ object_id = ObjectID (class_id = "@legend_cell_lables" ))
133
+
134
+ wall_cell_lable = pygame_gui .elements .UILabel (
135
+ relative_rect = pygame .Rect ((grid .x + 800 + grid .gap , HEIGHT - 35 ), (90 , grid .gap )),
136
+ text = "-wall cell" ,
137
+ manager = UI_MANAGER ,
138
+ object_id = ObjectID (class_id = "@legend_cell_lables" ))
139
+
140
+ legend_cells = [
141
+ LegendCell (WIN , grid .x , HEIGHT - 35 , cell .UNVISITED_COLOR , GREY , grid ),
142
+ LegendCell (WIN , grid .x + 200 , HEIGHT - 35 , cell .VISITED_COLOR , GREY , grid ),
143
+ LegendCell (WIN , grid .x + 400 , HEIGHT - 35 , cell .OPEN_COLOR , GREY , grid ),
144
+ LegendCell (WIN , grid .x + 600 , HEIGHT - 35 , cell .PATH_COLOR , GREY , grid ),
145
+ LegendCell (WIN , grid .x + 800 , HEIGHT - 35 , cell .WALL_COLOR , GREY , grid )]
146
+
147
+
148
+
116
149
117
150
start = grid [grid .total_rows // 2 ][grid .total_columns // 2 - 2 ]
118
151
end = grid [grid .total_rows // 2 ][grid .total_columns // 2 + 2 ]
@@ -128,7 +161,7 @@ def main() -> None:
128
161
129
162
while running :
130
163
time_delta = clock .tick (FPS )/ 1000.0
131
- draw (WIN , grid , UI_MANAGER , time_delta )
164
+ draw (WIN , grid , UI_MANAGER , time_delta , legend_cells )
132
165
133
166
134
167
for event in pygame .event .get ():
@@ -258,26 +291,15 @@ def main() -> None:
258
291
pygame .quit ()
259
292
260
293
261
- # def set_default_position_for_points(start: list, end: list, grid: Grid):
262
- # start[0].reset()
263
- # end[0].reset()
264
- # start[0] = grid[grid.total_rows // 2][grid.total_columns // 2 - 2]
265
- # end[0] = grid[grid.total_rows // 2][grid.total_columns // 2 + 2]
266
- # start[0].make_start()
267
- # end[0].make_end()
268
-
269
-
270
-
271
-
272
294
def run_current_algorithm (
273
295
algo_menu : pygame_gui .elements .UIDropDownMenu ,
274
296
draw : Callable ,
275
297
grid : Grid ,
276
- start : Cell ,
277
- end : Cell ,
298
+ start : cell . Cell ,
299
+ end : cell . Cell ,
278
300
animation : bool ) -> None :
279
301
280
- """Calls funcion that visulizes selected algorithm"""
302
+ """Calls function that visulizes selected algorithm"""
281
303
282
304
if algo_menu .selected_option == Algorithms .ASTAR :
283
305
algo .astar (draw , grid , start , end , animation )
@@ -293,6 +315,9 @@ def run_current_algorithm(
293
315
294
316
elif algo_menu .selected_option == Algorithms .GBFS :
295
317
algo .gbfs (draw , grid , start , end , animation )
318
+
319
+ elif algo_menu .selected_option == Algorithms .BBFS :
320
+ algo .bidirectional_bfs (draw , grid , start , end , animation )
296
321
297
322
else :
298
323
algo .astar (draw , grid , start , end , animation )
@@ -302,11 +327,11 @@ def generate_current_maze(
302
327
maze_menu : pygame_gui .elements .UIDropDownMenu ,
303
328
draw : Callable ,
304
329
grid : Grid ,
305
- start : Cell ,
306
- end : Cell ,
330
+ start : cell . Cell ,
331
+ end : cell . Cell ,
307
332
animation : bool ) -> None :
308
333
309
- """Calls funcion that generates selected maze"""
334
+ """Calls function that generates selected maze"""
310
335
311
336
if maze_menu .selected_option == Mazes .RECDIV :
312
337
maze .recursive_division_maze_gen (draw , grid , animation )
0 commit comments