-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
411 lines (341 loc) · 16.8 KB
/
app.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import sys
from settings import *
from buttons import Button
from bfs import BreadthFirst
from dfs import DepthFirst
from astar import Astar
from dijkstra import Dijkstra
from visualize_path import VisualizePath
pygame.init()
class App:
def __init__(self):
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
self.running = True
self.state = 'main menu'
self.algorithm_state = ''
self.grid_square_length = 24
# self.load()
self.start_end_checker = 0
self.mouse_drag = 0
# start and end nodes coordinates
self.start_node_x = None
self.start_node_y = None
self.end_node_x = None
self.end_node_y = None
# wall nodes list
self.wall_pos = wall_nodes_coords_list.copy()
# main menu buttons
self.bfs_button = Button(self, WHITE, 338, MAIN_BUTTON_Y_POS, MAIN_BUTTON_LENGTH, MAIN_BUTTON_HEIGHT, 'BFS')
self.dfs_button = Button(self, WHITE, 558, MAIN_BUTTON_Y_POS, MAIN_BUTTON_LENGTH, MAIN_BUTTON_HEIGHT, 'DFS')
self.astar_button = Button(self, WHITE, 778, MAIN_BUTTON_Y_POS, MAIN_BUTTON_LENGTH, MAIN_BUTTON_HEIGHT, 'Astar')
self.dijkstra_button = Button(self, WHITE, 998, MAIN_BUTTON_Y_POS, MAIN_BUTTON_LENGTH,
MAIN_BUTTON_HEIGHT, 'Dijkstra')
# grid menu options
self.start_end_node_button = Button(self, AQUAMARINE, 20, START_END_BUTTON_HEIGHT,
GRID_BUTTON_LENGTH, GRID_BUTTON_HEIGHT, 'Start/End Node')
self.wall_node_button = Button(self, AQUAMARINE, 20, START_END_BUTTON_HEIGHT +
GRID_BUTTON_HEIGHT + BUTTON_SPACER,
GRID_BUTTON_LENGTH, GRID_BUTTON_HEIGHT,
'Wall Node')
self.reset_button = Button(self, AQUAMARINE, 20, START_END_BUTTON_HEIGHT +
GRID_BUTTON_HEIGHT * 2 + BUTTON_SPACER * 2,
GRID_BUTTON_LENGTH, GRID_BUTTON_HEIGHT, 'RESET')
self.start_button = Button(self, AQUAMARINE, 20, START_END_BUTTON_HEIGHT +
GRID_BUTTON_HEIGHT * 3 + BUTTON_SPACER * 3,
GRID_BUTTON_LENGTH, GRID_BUTTON_HEIGHT, 'Visualize')
self.main_menu_button = Button(self, AQUAMARINE, 20, START_END_BUTTON_HEIGHT +
GRID_BUTTON_HEIGHT * 4 + BUTTON_SPACER * 4,
GRID_BUTTON_LENGTH, GRID_BUTTON_HEIGHT, 'Main Menu')
def run(self):
while self.running:
if self.state == 'main menu':
self.main_menu_events()
if self.state == 'grid window':
self.grid_events()
if self.state == 'draw S/E' or self.state == 'draw walls':
self.draw_nodes()
if self.state == 'start visualizing':
self.execute_algo()
if self.state == 'aftermath':
self.reset_or_main_menu()
pygame.quit()
sys.exit()
def load(self):
# self.main_menu_background = pygame.image.load('background.png')
# self.grid_background = pygame.image.load('grid.png')
pass
@staticmethod
def draw_text(words, screen, pos, size, color, font_name, center=False):
font = pygame.font.SysFont(font_name, size)
text = font.render(words, False, color)
text_size = text.get_size()
if center:
pos[0] = pos[0] - text_size[0] // 2
pos[1] = pos[1] - text_size[1] // 2
screen.blit(text, pos)
def sketch_main_menu(self):
# draw background
# self.screen.blit(self.main_menu_background, (0, 0))
self.bfs_button.draw_button(AQUAMARINE)
self.dfs_button.draw_button(AQUAMARINE)
self.astar_button.draw_button(AQUAMARINE)
self.dijkstra_button.draw_button(AQUAMARINE)
def sketch_hotbar(self):
self.screen.fill(BLACK)
pygame.draw.rect(self.screen, WHITE, (0, 0, 240, 768), 0)
# draw grid background
# self.screen.blit(self.grid_background, (0, 0))
def sketch_grid(self):
# add borders
pygame.draw.rect(self.screen, ALICE, (240, 0, WIDTH, HEIGHT), 0)
pygame.draw.rect(self.screen, AQUAMARINE, (264, 24, GRID_WIDTH, GRID_HEIGHT), 0)
# draw grid with size 52, 30
for x in range(52):
pygame.draw.line(self.screen, ALICE, (GS_X + x * self.grid_square_length, GS_Y),
(GS_X + x * self.grid_square_length, GE_Y))
for y in range(30):
pygame.draw.line(self.screen, ALICE, (GS_X, GS_Y + y * self.grid_square_length),
(GE_X, GS_Y + y * self.grid_square_length))
def sketch_grid_buttons(self):
# draw buttons
self.start_end_node_button.draw_button(STEELBLUE)
self.wall_node_button.draw_button(STEELBLUE)
self.reset_button.draw_button(STEELBLUE)
self.start_button.draw_button(STEELBLUE)
self.main_menu_button.draw_button(STEELBLUE)
def set_menu_buttons_color(self, color):
self.start_end_node_button.color = color
self.wall_node_button.color = color
self.reset_button.color = color
self.start_button.color = color
self.main_menu_button.color = color
def grid_window_buttons(self, pos, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.start_end_node_button.isOver(pos):
self.state = 'draw S/E'
elif self.wall_node_button.isOver(pos):
self.state = 'draw walls'
elif self.reset_button.isOver(pos):
self.execute_reset()
elif self.start_button.isOver(pos):
self.state = 'start visualizing'
elif self.main_menu_button.isOver(pos):
self.back_to_menu()
if event.type == pygame.MOUSEMOTION:
if self.start_end_node_button.isOver(pos):
self.start_end_node_button.color = MINT
elif self.wall_node_button.isOver(pos):
self.wall_node_button.color = MINT
elif self.reset_button.isOver(pos):
self.reset_button.color = MINT
elif self.start_button.isOver(pos):
self.start_button.color = MINT
elif self.main_menu_button.isOver(pos):
self.main_menu_button.color = MINT
else:
self.set_menu_buttons_color(STEELBLUE)
def grid_button_keep_color(self):
if self.state == 'draw S/E':
self.start_end_node_button.color = MINT
elif self.state == 'draw walls':
self.wall_node_button.color = MINT
def execute_reset(self):
self.start_end_checker = 0
# reset start and end nodes coordinates
self.start_node_x = None
self.start_node_y = None
self.end_node_x = None
self.end_node_y = None
# reset wall nodes list
self.wall_pos = wall_nodes_coords_list.copy()
# reset state
self.state = 'grid window'
def back_to_menu(self):
self.start_end_checker = 0
# reset start and end nodes coordinates
self.start_node_x = None
self.start_node_y = None
self.end_node_x = None
self.end_node_y = None
# reset wall nodes list
self.wall_pos = wall_nodes_coords_list.copy()
# switch state
self.state = 'main menu'
def set_algo_buttons_color(self, color):
self.bfs_button.color = color
self.dfs_button.color = color
self.astar_button.color = color
self.dijkstra_button.color = color
def main_menu_events(self):
# draw background
pygame.display.update()
self.sketch_main_menu()
self.draw_text('Path Finding', self.screen, [1200, 720], 28, WHITE, FONT, center=False)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
pos = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN:
if self.bfs_button.isOver(pos):
self.algorithm_state = 'bfs'
self.state = 'grid window'
if self.dfs_button.isOver(pos):
self.algorithm_state = 'dfs'
self.state = 'grid window'
if self.astar_button.isOver(pos):
self.algorithm_state = 'astar'
self.state = 'grid window'
if self.dijkstra_button.isOver(pos):
self.algorithm_state = 'dijkstra'
self.state = 'grid window'
if event.type == pygame.MOUSEMOTION:
if self.bfs_button.isOver(pos):
self.bfs_button.color = AQUAMARINE
elif self.dfs_button.isOver(pos):
self.dfs_button.color = AQUAMARINE
elif self.astar_button.isOver(pos):
self.astar_button.color = AQUAMARINE
elif self.dijkstra_button.isOver(pos):
self.dijkstra_button.color = AQUAMARINE
else:
self.set_algo_buttons_color(WHITE)
def grid_events(self):
self.sketch_hotbar()
self.sketch_grid()
self.sketch_grid_buttons()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
pos = pygame.mouse.get_pos()
self.grid_window_buttons(pos, event)
def draw_nodes(self):
self.grid_button_keep_color()
self.sketch_grid_buttons()
pygame.display.update()
pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
self.grid_window_buttons(pos, event)
if 264 < pos[0] < 1512 and 24 < pos[1] < 744:
x_grid_pos = (pos[0] - 264) // 24
y_grid_pos = (pos[1] - 24) // 24
if event.type == pygame.MOUSEBUTTONDOWN:
self.mouse_drag = 1
if self.state == 'draw S/E' and self.start_end_checker < 2:
# color and coordinates for start node
if self.start_end_checker == 0:
node_color = TOMATO
self.start_node_x = x_grid_pos + 1
self.start_node_y = y_grid_pos + 1
self.start_end_checker += 1
# color and coordinates for end node, making sure it's different from start node
elif self.start_end_checker == 1 \
and (x_grid_pos + 1, y_grid_pos + 1) != (self.start_node_x, self.start_node_y):
node_color = ROYALBLUE
self.end_node_x = x_grid_pos + 1
self.end_node_y = y_grid_pos + 1
self.start_end_checker += 1
else:
continue
# draw on the grid
pygame.draw.rect(self.screen, node_color, (264 + x_grid_pos * 24, 24 + y_grid_pos * 24, 24, 24), 0)
elif event.type == pygame.MOUSEBUTTONUP:
self.mouse_drag = 0
# check to see if mouse drag is available to draw wall nodes
if self.mouse_drag == 1:
# draw wall nodes and append them accordingly
# check for duplicates in the list and if they are overlapping start/end nodes
if self.state == 'draw walls':
if (x_grid_pos + 1, y_grid_pos + 1) not in self.wall_pos \
and (x_grid_pos + 1, y_grid_pos + 1) != (self.start_node_x, self.start_node_y) \
and (x_grid_pos + 1, y_grid_pos + 1) != (self.end_node_x, self.end_node_y):
pygame.draw.rect(self.screen, BLACK, (264 + x_grid_pos * 24, 24 + y_grid_pos * 24, 24, 24), 0)
self.wall_pos.append((x_grid_pos + 1, y_grid_pos + 1))
for x in range(52):
pygame.draw.line(self.screen, ALICE, (GS_X + x * self.grid_square_length, GS_Y),
(GS_X + x * self.grid_square_length, GE_Y))
for y in range(30):
pygame.draw.line(self.screen, ALICE, (GS_X, GS_Y + y * self.grid_square_length),
(GE_X, GS_Y + y * self.grid_square_length))
def execute_algo(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
# BFS
if self.algorithm_state == 'bfs':
self.bfs = BreadthFirst(self, self.start_node_x, self.start_node_y, self.end_node_x, self.end_node_y,
self.wall_pos)
if self.start_node_x or self.end_node_x is not None:
self.bfs.bfs_execute()
if self.bfs.route_found:
self.draw_path = VisualizePath(self.screen, self.start_node_x, self.start_node_y, self.bfs.route, [])
self.draw_path.get_path_coords()
self.draw_path.draw_path()
else:
self.draw_text('NO ROUTE FOUND!', self.screen, [768, 384], 50, RED, FONT, center=True)
# DFS
elif self.algorithm_state == 'dfs':
self.dfs = DepthFirst(self, self.start_node_x, self.start_node_y, self.end_node_x, self.end_node_y,
self.wall_pos)
if self.start_node_x or self.end_node_x is not None:
self.dfs.dfs_execute()
if self.dfs.route_found:
self.draw_path = VisualizePath(self.screen, self.start_node_x, self.start_node_y, self.dfs.route, [])
self.draw_path.get_path_coords()
self.draw_path.draw_path()
else:
self.draw_text('NO ROUTE FOUND!', self.screen, [768, 384], 50, RED, FONT, center=True)
# ASTAR
elif self.algorithm_state == 'astar':
self.astar = Astar(self, self.start_node_x, self.start_node_y, self.end_node_x, self.end_node_y,
self.wall_pos)
if self.start_node_x or self.end_node_x is not None:
self.astar.astar_execute()
if self.astar.route_found:
self.draw_path = VisualizePath(self.screen, self.start_node_x, self.start_node_y,
None, self.astar.route)
self.draw_path.draw_path()
else:
self.draw_text('NO ROUTE FOUND!', self.screen, [768, 384], 50, RED, FONT, center=True)
# DIJKSTRA
elif self.algorithm_state == 'dijkstra':
self.dijkstra = Dijkstra(self, self.start_node_x, self.start_node_y, self.end_node_x, self.end_node_y,
self.wall_pos)
if self.start_node_x or self.end_node_x is not None:
self.dijkstra.dijkstra_execute()
if self.dijkstra.route_found:
self.draw_path = VisualizePath(self.screen, self.start_node_x, self.start_node_y,
None, self.dijkstra.route)
self.draw_path.draw_path()
else:
self.draw_text('NO ROUTE FOUND!', self.screen, [768, 384], 50, RED, FONT, center=True)
pygame.display.update()
self.state = 'aftermath'
def reset_or_main_menu(self):
self.sketch_grid_buttons()
pygame.display.update()
pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.MOUSEMOTION:
if self.start_end_node_button.isOver(pos):
self.start_end_node_button.color = MINT
elif self.wall_node_button.isOver(pos):
self.wall_node_button.color = MINT
elif self.reset_button.isOver(pos):
self.reset_button.color = MINT
elif self.start_button.isOver(pos):
self.start_button.color = MINT
elif self.main_menu_button.isOver(pos):
self.main_menu_button.color = MINT
else:
self.set_menu_buttons_color(STEELBLUE)
if event.type == pygame.MOUSEBUTTONDOWN:
if self.reset_button.isOver(pos):
self.execute_reset()
elif self.main_menu_button.isOver(pos):
self.back_to_menu()