-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
108 lines (86 loc) · 3.04 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
import pygame
from node import Node
from a_star import aStar
from dijkstra import dijkstra
pygame.init()
HEIGHT, WIDTH = 900, 900
window = pygame.display.set_mode((HEIGHT, WIDTH))
pygame.display.set_caption("Pathfinding visualizer")
GREY = (128, 128, 128)
def algorithm(draw, grid, start, end):
aStar(draw, grid, start, end)
# dijkstra(draw, grid, start, end)
def buildGrid(row, width):
grid = []
node_width = width // row
for i in range(row):
grid.append([])
for j in range(row):
grid[i].append(Node(i, j, node_width, row))
return grid
def drawGridLines(window, rows, width):
gap = width // rows
for i in range(rows):
pygame.draw.line(window, GREY, (0, i * gap), (width, i * gap))
pygame.draw.line(window, GREY, (i * gap, 0), (i * gap, width))
def draw(window, grid, rows, width):
for row in grid:
for node in row:
node.draw(window)
drawGridLines(window, rows, width)
pygame.display.update()
def getClickedPosition(position, rows, width):
gap = width // rows
x, y = position
row, column = x // gap, y // gap
return (row, column)
def main(window, WIDTH):
ROWS = 90
grid = buildGrid(ROWS, WIDTH)
start, end = None, None
started = False
run = True
started = False
while run:
draw(window, grid, ROWS, WIDTH)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if started:
continue
if pygame.mouse.get_pressed()[0]:
position = pygame.mouse.get_pos()
row, column = getClickedPosition(position, ROWS, WIDTH)
node = grid[row][column]
if not start and node != end:
start = node
start.makeStartNode()
elif not end and node != start:
end = node
end.makeEndNode()
elif node != start and node != end:
node.makeObstacle()
elif pygame.mouse.get_pressed()[2]:
position = pygame.mouse.get_pos()
row, column = getClickedPosition(position, ROWS, WIDTH)
node = grid[row][column]
node.resetNode()
if node == start:
start = None
if node == end:
end = None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not started:
started = True
for row in grid:
for node in row:
node.updateNeighbors(grid)
algorithm(lambda: draw(window, grid, ROWS, WIDTH), grid, start, end)
started = False
if event.key == pygame.K_c:
start = None
end = None
grid = buildGrid(ROWS, WIDTH)
draw(window, grid, ROWS, WIDTH)
pygame.quit()
main(window, WIDTH)