-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_star.py
280 lines (218 loc) · 6.71 KB
/
a_star.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import pygame
import time
HEIGHT= 600
WIDTH = 600
ROWS = 50
COLS = 50
DELAY_TIME = float("0.001")
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pathfinding - A star algorithm")
WHITE = (255, 255, 255)
TURQUOISE = (64, 224, 208)
GREY = (128, 128, 128)
PURPLE = (128, 0, 128)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
ORANGE = (255, 165 ,0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
class Node:
height = HEIGHT // ROWS
width = WIDTH // COLS
def __init__(self, x, y):
self.x = x
self.y = y
self.colour = None
self.parent = None
self.g = None
self.h = None
self.f = None
self.is_barrier = False
if self.x == 0 or self.x == (COLS - 1):
self.make_barrier()
elif self.y == 0 or self.y == (ROWS - 1):
self.make_barrier()
else:
self.colour = WHITE
def __repr__(self):
return f"x:{self.x}y:{self.y}"
def make_start(self):
self.colour = TURQUOISE
def make_end(self):
self.colour = ORANGE
def make_barrier(self):
self.colour = BLACK
self.is_barrier = True
def draw(self, win):
pygame.draw.rect(win, self.colour, (self.x * self.width, self.y * self.height, self.height, self.width))
def distance_between(node_a, node_b):
x_distance = abs(node_a.x - node_b.x)
y_distance = abs(node_a.y - node_b.y)
distance = x_distance + y_distance
return distance
def mouse_to_grid(x, y):
node_height = WIDTH // ROWS
node_width = WIDTH // COLS
column = (y - 1) // node_height
row = (x - 1) // node_width
return row, column
def draw_window():
WIN.fill(WHITE)
draw_grid()
draw_gridlines()
pygame.display.update()
def draw_grid():
for row in grid:
for node in row:
node.draw(WIN)
def make_grid():
grid = []
for i in range(ROWS):
grid.append([])
for j in range(COLS):
node = Node(i, j)
grid[i].append(node)
return grid
def draw_gridlines():
for i in range(ROWS):
pygame.draw.line(WIN, GREY, (0, (i * Node.height)), (WIDTH, (i * Node.height)))
for i in range(COLS):
pygame.draw.line(WIN, GREY, ((i * Node.width), 0), ((i * Node.width), HEIGHT))
def assign_node_type(mouse_x, mouse_y, type):
grid_pos = mouse_to_grid(mouse_x, mouse_y)
node = grid[grid_pos[0]][grid_pos[1]]
if node.x == 0 or node.x == (COLS - 1) or node.y == 0 or node.y == (ROWS - 1):
pass
elif type == "start":
global start
start = node
node.make_start()
elif type == "end":
global end
end = node
node.make_end()
elif type == "barrier":
if node != start and node != end:
node.make_barrier()
pass
def get_neighbours(node):
neighbours = []
i = node.x
j = node.y
neighbours.append(grid[i][j-1]) #above
neighbours.append(grid[i][j+1]) #below
neighbours.append(grid[i-1][j]) #left
neighbours.append(grid[i+1][j]) #right
return neighbours
def colour_list(list):
if list == open:
colour = GREEN
elif list == closed:
colour = RED
elif list == path:
colour = PURPLE
for node in list:
if node == start or node == end:
continue
node.colour = colour
def construct_path():
global path
path = []
pathnode = end
while pathnode.parent != None:
path.append(pathnode)
pathnode = pathnode.parent
colour_list(path)
time.sleep(0.04)
draw_window()
def algorithm():
global open
global closed
open = []
closed = []
finished = False
start.g = 0
start.f = 0
open.append(start)
while finished == False:
def node_sort(node):
return node.f
open = sorted(open, key = node_sort)
current = open[0]
closed.append(current)
colour_list(closed)
time.sleep(DELAY_TIME)
draw_window()
del open[0]
colour_list(open)
time.sleep(DELAY_TIME)
draw_window()
if current == end:
construct_path()
finished = True
break
neighbours = get_neighbours(current)
def is_in_closed(neighbour):
for closed_node in closed:
if closed_node == neighbour:
return True
return False
for neighbour in neighbours:
is_in_closed(neighbour)
if neighbour.is_barrier == True or is_in_closed(neighbour) == True:
continue
def is_in_open(neighbour):
for open_node in open:
if open_node == neighbour:
return True
return False
is_in_open(neighbour)
if is_in_open(neighbour) == False:
open.append(neighbour)
colour_list(open)
time.sleep(DELAY_TIME)
draw_window()
neighbour.parent = current
neighbour.g = current.g + 1
neighbour.h = distance_between(neighbour, end)
neighbour.f = neighbour.g + neighbour.h
if is_in_open(neighbour) == True:
if (current.g + 1) < neighbour.g:
neighbour.g = (current.g + 1)
neighbour.parent = current
neighbour.f = neighbour.g + neighbour.h
def node_sort(node):
return node.f
open = sorted(open, key = node_sort)
if current != end and len(open) == 0:
print ("Failed to find path")
finished = True
def main():
global grid
grid = make_grid()
global start
global end
start = None
end = None
run = True
pathfinding = False
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if pygame.mouse.get_pressed()[0] and pathfinding == False:
mouse_pos = pygame.mouse.get_pos()
if start == None and end == None:
assign_node_type(mouse_pos[0], mouse_pos[1], "start")
elif start != None and end == None:
assign_node_type(mouse_pos[0], mouse_pos[1], "end")
elif start and end:
assign_node_type(mouse_pos[0], mouse_pos[1], "barrier")
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and start and end:
pathfinding = True
algorithm()
draw_window()
pygame.quit()
if __name__ == "__main__":
main()