-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
151 lines (129 loc) · 4.56 KB
/
app.py
File metadata and controls
151 lines (129 loc) · 4.56 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
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
from tkinter import messagebox, Tk
import pygame
import sys
# Initiliaze Display
window_width = 500
window_height = 500
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Pathfinding Algorithm Visualizer")
columns = 50
rows = 50
box_width = window_width // columns
box_height = window_height // rows
grid = []
queue = []
path = []
class Box:
def __init__(self, i, j):
self.x = i
self.y = j
self.start = False
self.wall = False
self.target = False
self.queued = False
self.visited = False
self.neighbours = []
self.prior = None
def draw(self, win, color):
''' Draw a box '''
pygame.draw.rect(win, color, (self.x * box_width, self.y * box_height, box_width-2, box_height-2))
def set_neighbours(self):
''' Set the nearest neighbours for the current box '''
if self.x > 0:
self.neighbours.append(grid[self.x - 1][self.y])
if self.x < columns - 1:
self.neighbours.append(grid[self.x + 1][self.y])
if self.y > 0:
self.neighbours.append(grid[self.x][self.y - 1])
if self.y < rows - 1:
self.neighbours.append(grid[self.x][self.y + 1])
# Create Grid
for i in range(columns):
arr = []
for j in range(rows):
arr.append(Box(i, j))
grid.append(arr)
# Set Neighbours
for i in range(columns):
for j in range(rows):
grid[i][j].set_neighbours()
# Set Starting Box
start_box = grid[0][0]
start_box.start = True
start_box.visited = True
queue.append(start_box)
def main():
begin_search = False
target_box_set = False
searching = True
target_box = None
while True:
for event in pygame.event.get():
# Quit Window
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Mouse Controls
elif event.type == pygame.MOUSEMOTION:
x = pygame.mouse.get_pos()[0]
y = pygame.mouse.get_pos()[1]
# Draw Wall
if event.buttons[0]:
i = x // box_width
j = y // box_height
grid[i][j].wall = True
# Set Target
if event.buttons[2] and not target_box_set:
i = x // box_width
j = y // box_height
target_box = grid[i][j]
target_box.target = True
target_box_set = True
# Start Algorithm
if event.type == pygame.KEYDOWN and target_box_set:
begin_search = True
# Dijkstra's Algorithm
if begin_search:
if len(queue) > 0 and searching:
current_box = queue.pop(0)
current_box.visited = True
if current_box == target_box:
searching = False
while current_box.prior != start_box:
path.append(current_box.prior)
current_box = current_box.prior
else:
for neighbour in current_box.neighbours:
if not neighbour.queued and not neighbour.wall:
neighbour.queued = True
neighbour.prior = current_box
queue.append(neighbour)
# If no path found show error window
else:
if searching:
Tk().wm_withdraw()
messagebox.showinfo("No Solution", "There is no solution!")
searching = False
window.fill((0, 0, 0)) # Fill window with black
# Draw Grid
for i in range(columns):
for j in range(rows):
box = grid[i][j]
box.draw(window, (50, 50, 50)) # Set default color to gray
if box.queued:
box.draw(window, (0, 0, 200))
if box.visited:
# set visited color to teal green
box.draw(window, (0, 200, 200)) # Set queued color to cyan
if box in path:
box.draw(window, (200, 200, 0)) # Yellow
if box.start:
# purple
box.draw(window, (200, 0, 200))
if box.wall:
box.draw(window, (90, 90, 90))
if box.target:
box.draw(window, (200, 0, 0))
pygame.display.flip() # Update the display
if __name__ == "__main__":
main()