-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_star.py
More file actions
107 lines (83 loc) · 3.87 KB
/
a_star.py
File metadata and controls
107 lines (83 loc) · 3.87 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
"""
Project Name: A* Path Finder Visualizer
Author: Utkarsh Sahu
Email: sahuuutkarhs03@gamil.com
Twitter: @Utkarsh4tech ( https://twitter.com/Utkarsh4tech )
Project Description: This is a Visualization tool for helping learners understand
about the A* algorithm. The tech Stack used in Project is
- Pygame and Python.
File Descreption: This is the file to be run to start the visualisation.
Contains the main function and Pygame Action settings.
A* Algorithm: A* Search algorithm is one of the best and popular
technique used in path-finding and graph traversals.
A* is a modification of Dijkstra’s Algorithm that is optimized for a single destination.
Dijkstra’s Algorithm can find paths to all locations;
A* finds paths to one location, or the closest of several locations.
It prioritizes paths that seem to be leading closer to a goal.
The secret to its success is that it combines the pieces of information that
Dijkstra’s Algorithm uses (favoring vertices that are close to the starting point)
and information that Greedy Best-First-Search uses (favoring vertices that are close to the goal).
"""
import pygame as pg
from algorithms import a_star_algorithm
from grid_utility import make_grid,draw,get_clicked_pos
WIDTH = 650
WIN = pg.display.set_mode((WIDTH,WIDTH))
pg.display.set_caption("A* Star PathFinder")
def main(win,width):
ROWS=50
grid=make_grid(ROWS,width )
start,end=None,None
run=True
while run:
draw(win,grid,ROWS,width)
for event in pg.event.get():
if event.type == pg.QUIT:
run=False
if pg.mouse.get_pressed()[0]: #LEFT CLICK
"""
If the node is clicked by Left Click we calculate the position
then we get the node corresponding to that position.
We make the node corresponding to the first node as START,
node corresponding to the second node as END
and all the nodes thereafter as Barriers
"""
pos=pg.mouse.get_pos()
row,col=get_clicked_pos(pos,ROWS, width)
node= grid[row][col]
if not start and node != end:
start=node
start.make_start()
elif not end and node != start:
end=node
end.make_end()
elif node != end and node != start:
node.make_barrier()
elif pg.mouse.get_pressed()[2]: #RIGHT CLICK
"""
If the node is clicked by right click of mouse
we use it to reset the position.
"""
pos=pg.mouse.get_pos()
row,col=get_clicked_pos(pos,ROWS, width)
node= grid[row][col]
node.reset()
if node == start:
start = end
if node == end :
end = None
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE and start and end:
# If space is pressed we start the algorithm
for row in grid:
for node in row:
node.update_neighbors(grid)
a_star_algorithm(lambda:draw(win,grid,ROWS,width),grid,start,end)
if event.key == pg.K_c:
# if 'C' is pressed we clear the screen
start=None
end=None
grid=make_grid(ROWS,width)
pg.quit()
if __name__=="__main__":
main(WIN,WIDTH)