-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
77 lines (61 loc) · 2.27 KB
/
main.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
import pygame
from Window import Window
from algorithm import bfs
from algorithm import dfs
from algorithm import a_star
WIDTH = 650
ROWS = 50
def main():
"""
implements an engine loop that runs the processes every event
:return: void
"""
window = Window(WIDTH, ROWS)
start = None
end = None
run = True
while run:
window.render_window()
for event in pygame.event.get():
# if user clicks exit button
if event.type == pygame.QUIT:
run = False
# if user clicks left mouse button
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
row, col = window.get_clicked_pos(pos)
node = window.grid.grid[row, col]
if not start and node != end:
start = node
start.make_start()
elif not end and node != start:
end = node
end.make_end()
elif node != end and node != start:
node.make_barrier()
# if user clicks right mouse button
elif pygame.mouse.get_pressed()[2]:
pos = pygame.mouse.get_pos()
row, col = window.get_clicked_pos(pos)
node = window.grid.grid[row, col]
node.reset()
if node == start:
start = None
elif node == end:
end = None
if event.type == pygame.KEYDOWN:
# if user presses key space, start visualization
if event.key == pygame.K_SPACE and start and end:
for row in window.grid.grid:
for node in row:
node.update_neighbors(window.grid.grid)
bfs(lambda: window.render_window(), start, end)
# dfs(lambda: window.render_window(), start, end)
# a_star(lambda: window.render_window(), window.grid.grid, start, end)
# if user presses key c, reset visualization
if event.key == pygame.K_c:
start = None
end = None
window.grid.reset_grid()
pygame.quit()
main()