-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·136 lines (104 loc) · 4.94 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
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
import pygame
from settings import *
from utils import dijkstra, a_star
from utils.draw import draw_square, reset_cell
from utils.geometry_2d import get_node_pos_by_mouse_pos
from utils.graph_creator import create_boxed_graph, add_vector, get_distance_from_neighbour_vector
from utils.graph_creator import neighbour_displacements
# CALCULATED CONSTANTS (NO MESSING HERE)
N_NODES_ON_EDGE = WINDOW_SIZE // GRID_SIZE
GRAPH = None
#
# WORKING_VARIABLES (USED BY THIS PROGRAM)
source_coord = None
backup_source_coord = None
destination_coord = None
backup_destination_coord = None
prev_path = []
obstacles = set()
last_mouse_button_pressed = None
#
pygame.init()
pygame.display.set_caption("PathFinder Algorithm Visualizer")
screen = pygame.display.set_mode([WINDOW_SIZE, WINDOW_SIZE])
clock = pygame.time.Clock()
def prepare_background():
global GRAPH
screen.fill(BACKGROUND_COLOR) # Setting Background Color
# Drawing Grid
# - # Drawing Horizontal Lines
for y in range(WINDOW_SIZE // GRID_SIZE):
pygame.draw.line(screen, GRID_COLOR, (0, y * GRID_SIZE), (WINDOW_SIZE, y * GRID_SIZE))
# - # Drawing Vertical Lines
for x in range(WINDOW_SIZE // GRID_SIZE):
pygame.draw.line(screen, GRID_COLOR, (x * GRID_SIZE, 0), (x * GRID_SIZE, WINDOW_SIZE))
# Updating changes on screen
pygame.display.flip()
GRAPH = create_boxed_graph(N_NODES_ON_EDGE)
prepare_background()
def mainloop():
global source_coord, backup_source_coord, destination_coord, backup_destination_coord, prev_path, GRAPH
def clean_previous_path():
for coord_ in prev_path[1:-1]:
if coord_ not in obstacles:
reset_cell(screen, coord_)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_q):
pygame.quit()
return 0
elif event.type == pygame.KEYDOWN and event.key == pygame.K_s:
prepare_background()
pygame.display.flip()
mouse_pos = pygame.mouse.get_pos()
box_coord = get_node_pos_by_mouse_pos(mouse_pos, GRID_SIZE, N_NODES_ON_EDGE)
draw_square(screen, box_coord, START_END_CELL_COLOR)
source_coord = backup_source_coord = box_coord
elif event.type == pygame.KEYDOWN and event.key == pygame.K_d:
mouse_pos = pygame.mouse.get_pos()
box_coord = get_node_pos_by_mouse_pos(mouse_pos, GRID_SIZE, N_NODES_ON_EDGE)
if box_coord in obstacles:
continue
draw_square(screen, box_coord, START_END_CELL_COLOR)
destination_coord = box_coord
backup_destination_coord = box_coord
if source_coord is None:
source_coord = backup_source_coord
for coord in prev_path[1:]:
if coord not in obstacles and coord != destination_coord:
reset_cell(screen, coord)
elif pygame.mouse.get_pressed()[0]:
box_coord = get_node_pos_by_mouse_pos(pygame.mouse.get_pos(), GRID_SIZE, N_NODES_ON_EDGE)
draw_square(screen, box_coord, OBSTACLE_COLOR)
obstacles.add(box_coord)
GRAPH.kill_node(box_coord)
if box_coord in prev_path:
source_coord = backup_source_coord
destination_coord = backup_destination_coord
clean_previous_path()
elif pygame.mouse.get_pressed()[2]:
box_coord = get_node_pos_by_mouse_pos(pygame.mouse.get_pos(), GRID_SIZE, N_NODES_ON_EDGE)
reset_cell(screen, box_coord)
for displacement_vector in neighbour_displacements:
neighbour_node = add_vector(displacement_vector, box_coord, 2)
if -1 not in neighbour_node and neighbour_node in GRAPH.data.keys():
GRAPH.add_edge(box_coord, neighbour_node,
get_distance_from_neighbour_vector(displacement_vector))
obstacles.discard(box_coord)
source_coord = backup_source_coord
destination_coord = backup_destination_coord
clean_previous_path()
if source_coord and destination_coord:
if ALGORITHM == 1:
path_finding_algorithm = dijkstra
elif ALGORITHM == 2:
path_finding_algorithm = a_star
else:
raise Exception("INVALID ALGORITHM CHOSEN! use 1 or 2 as OPTION in setting for ALGORITHM!")
path, distance = path_finding_algorithm(GRAPH, source_coord, destination_coord, screen)
prev_path = path.copy()
source_coord = destination_coord = None
for coord in path[1:-1]:
draw_square(screen, coord, PATH_COLOR)
if __name__ == '__main__':
mainloop()