-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (62 loc) · 2.58 KB
/
main.py
File metadata and controls
67 lines (62 loc) · 2.58 KB
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
import pygame
from json import load
from core.models import Grid
from core.algorithms import astar, dijkstra
with open(r'config\config.json', mode='r') as config_file:
config = load(config_file)
WIDTH = config["screen_width"]
WIN = pygame.display.set_mode((WIDTH, WIDTH))
pygame.display.set_caption("Path Finding Algorithm")
def main(win, width):
grid_object = Grid(config["ROWS"], width)
grid = grid_object.make_grid()
start_node = None
end_node = None
run = True
working = False
while run:
grid_object.draw(win)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
row, col = grid_object.get_clicked_pos(pos)
node = grid[row][col]
if not start_node and node != end_node:
start_node = node
start_node.make_start()
elif not end_node and node != start_node:
end_node = node
end_node.make_end()
elif node != end_node and node != start_node:
node.make_wall()
elif pygame.mouse.get_pressed()[2]:
pos = pygame.mouse.get_pos()
row, col = grid_object.get_clicked_pos(pos)
node = grid[row][col]
node.reset()
if node == start_node:
start_node = None
elif node == end_node:
end_node = None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a and start_node and end_node and not working:
working = True
for row in grid:
for node in row:
node.update_neighbors(grid)
astar.algorithm(lambda: grid_object.draw(win), grid, start_node, end_node)
if event.key == pygame.K_d and start_node and end_node and not working:
dijkstra.dijkstra(grid_object, start_node, end_node, WIN)
shortest_path = dijkstra.get_shortest_path(end_node)
for node in shortest_path:
if node != start_node and node != end_node:
node.make_path()
if event.key == pygame.K_c:
working = False
start_node = None
end_node = None
grid = grid_object.make_grid()
pygame.quit()
main(WIN, WIDTH)