Skip to content

Commit 8f4ab1d

Browse files
authored
Add files via upload
1 parent 856d289 commit 8f4ab1d

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed

main.py

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
import pygame
2+
import math
3+
from queue import PriorityQueue
4+
5+
width = 650
6+
win = pygame.display.set_mode((width, width))
7+
pygame.display.set_caption('A* Path Finding Algorithm')
8+
9+
red = (255, 0, 0)
10+
green = (0, 255, 0)
11+
blue = (0, 0, 255)
12+
yellow = (255, 255, 0)
13+
white = (255, 255, 255)
14+
black = (0, 0, 0)
15+
purple = (128, 0, 128)
16+
orange = (255, 165, 0)
17+
grey = (128, 128, 128)
18+
turquoise = (64, 224, 208)
19+
20+
class Node:
21+
def __init__(self, row, col, width, total_rows):
22+
self.row = row
23+
self.col = col
24+
self.x = row*width
25+
self.y = col*width
26+
self.colour = white
27+
self.neighborgs = []
28+
self.width = width
29+
self.total_rows = total_rows
30+
31+
def pos_get(self):
32+
return self.row, self.col
33+
34+
def closed(self):
35+
return self.colour == red
36+
37+
def is_open(self):
38+
return self.colour == green
39+
40+
def barrier(self):
41+
return self.colour == black
42+
43+
def start(self):
44+
return self.colour == orange
45+
46+
def end(self):
47+
return self.colour == turquoise
48+
49+
def reset(self):
50+
self.colour = white
51+
52+
def make_start(self):
53+
self.colour = orange
54+
55+
def make_closed(self):
56+
self.colour = red
57+
58+
def make_open(self):
59+
self.colour = green
60+
61+
def make_barrier(self):
62+
self.colour = black
63+
64+
def make_end(self):
65+
self.colour = turquoise
66+
67+
def make_path(self):
68+
self.colour = purple
69+
70+
def draw(self, win):
71+
pygame.draw.rect(win, self.colour, (self.x, self.y, self.width, self.width))
72+
73+
def update_neighbours(self, grid):
74+
self.neighborgs = []
75+
if self.row < self.total_rows - 1 and not grid[self.row + 1][self.col].barrier():
76+
self.neighborgs.append(grid[self.row + 1][self.col])
77+
78+
if self.row > 0 and not grid[self.row - 1][self.col].barrier():
79+
self.neighborgs.append(grid[self.row - 1][self.col])
80+
81+
if self.col < self.total_rows - 1 and not grid[self.row][self.col + 1].barrier():
82+
self.neighborgs.append(grid[self.row][self.col + 1])
83+
84+
if self.col > 0 and not grid[self.row][self.col - 1].barrier():
85+
self.neighborgs.append(grid[self.row][self.col - 1])
86+
87+
88+
def __lt__(self, other):
89+
return False
90+
91+
def h(p1, p2):
92+
x1, y1 = p1
93+
x2, y2 = p2
94+
return abs(x1-x2) + abs(y1-y2)
95+
96+
def reconstruct_path(came_from, current, draw):
97+
while current in came_from:
98+
current = came_from[current]
99+
current.make_path()
100+
draw()
101+
102+
def algorithm(draw, grid, start, end):
103+
count = 0
104+
open_set = PriorityQueue()
105+
open_set.put((0, count, start))
106+
came_from = {}
107+
g_score = {node: float("inf") for row in grid for node in row}
108+
g_score[start] = 0
109+
f_score = {node: float("inf") for row in grid for node in row}
110+
f_score[start] = h(start.pos_get(), end.pos_get())
111+
112+
open_set_hash = {start}
113+
114+
while not open_set.empty():
115+
for event in pygame.event.get():
116+
if event.type == pygame.QUIT:
117+
pygame.quit()
118+
119+
current = open_set.get()[2]
120+
open_set_hash.remove(current)
121+
122+
if current == end:
123+
reconstruct_path(came_from, end, draw)
124+
end.make_end()
125+
return True
126+
127+
for neighbor in current.neighborgs:
128+
temp_g_score = g_score[current] + 1
129+
if temp_g_score < g_score[neighbor]:
130+
came_from[neighbor] = current
131+
g_score[neighbor] = temp_g_score
132+
f_score[neighbor] = temp_g_score + h(neighbor.pos_get(), end.pos_get())
133+
if neighbor not in open_set_hash:
134+
count += 1
135+
open_set.put((f_score[neighbor], count, neighbor))
136+
open_set_hash.add(neighbor)
137+
neighbor.make_open()
138+
draw()
139+
if current != start:
140+
current.make_closed()
141+
142+
143+
def make_grid(rows, width):
144+
grid = []
145+
gap = width // rows
146+
for i in range(rows):
147+
grid.append([])
148+
for j in range(rows):
149+
node = Node(i, j, gap, rows)
150+
grid[i].append(node)
151+
152+
return grid
153+
154+
def draw_grid(win, rows, width):
155+
gap = width//rows
156+
for i in range(rows):
157+
pygame.draw.line(win, grey, (0, i*gap), (width, i*gap))
158+
for j in range(rows):
159+
pygame.draw.line(win, grey, (j*gap, 0), (j*gap, width))
160+
161+
def draw(win, grid, rows, width):
162+
win.fill(white)
163+
164+
for row in grid:
165+
for node in row:
166+
node.draw(win)
167+
draw_grid(win, rows, width)
168+
pygame.display.update()
169+
170+
def get_clicked_pos(pos, rows, width):
171+
gap = width // rows
172+
y, x = pos
173+
row = y // gap
174+
col = x // gap
175+
return row, col
176+
177+
def main(win, width):
178+
ROWS = 50
179+
grid = make_grid(ROWS, width)
180+
181+
start = None
182+
end = None
183+
184+
run = True
185+
186+
while run:
187+
draw(win, grid, ROWS, width)
188+
for event in pygame.event.get():
189+
if event.type == pygame.QUIT:
190+
run = False
191+
192+
if pygame.mouse.get_pressed()[0]:
193+
pos = pygame.mouse.get_pos()
194+
row, col = get_clicked_pos(pos, ROWS, width)
195+
node = grid[row][col]
196+
if not start and node != end:
197+
start = node
198+
start.make_start()
199+
elif not end and node != start:
200+
end = node
201+
end.make_end()
202+
elif node != end and node != start:
203+
node.make_barrier()
204+
205+
elif pygame.mouse.get_pressed()[2]:
206+
pos = pygame.mouse.get_pos()
207+
row, col = get_clicked_pos(pos, ROWS, width)
208+
node = grid[row][col]
209+
node.reset()
210+
if node == start:
211+
start = None
212+
elif node == end:
213+
end = None
214+
215+
if event.type == pygame.KEYDOWN:
216+
if event.key == pygame.K_SPACE and start and end:
217+
for row in grid:
218+
for node in row:
219+
node.update_neighbours(grid)
220+
algorithm(lambda: draw(win, grid, ROWS, width), grid, start, end)
221+
222+
if event.key == pygame.K_RETURN:
223+
start = None
224+
end = None
225+
grid = make_grid(ROWS, width)
226+
227+
pygame.quit()
228+
229+
main(win, width)

0 commit comments

Comments
 (0)