1
1
import pygame
2
2
from queue import PriorityQueue
3
3
4
- WIDTH = 800
4
+ WIDTH = 700
5
5
WIN = pygame .display .set_mode ((WIDTH , WIDTH ))
6
6
pygame .display .set_caption ("A* Path Finding Algorithm Visualizer" )
7
7
@@ -19,8 +19,8 @@ class Node:
19
19
def __init__ (self , row , col , width , total_rows ):
20
20
self .row = row
21
21
self .col = col
22
- self .x = row * width
23
- self .y = col * width
22
+ self .x = row * width
23
+ self .y = col * width
24
24
self .color = WHITE
25
25
self .neighhbours = []
26
26
self .width = width
@@ -66,32 +66,37 @@ def make_path(self):
66
66
self .color = YELLOW
67
67
68
68
def draw (self , win ):
69
- pygame .draw .rect (
70
- win , self .color , (self .x , self .y , self .width , self .width ))
69
+ pygame .draw .rect (win , self .color , (self .x , self .y , self .width , self .width ))
71
70
72
71
def update_neighbours (self , grid ):
73
72
self .neighhbours = []
74
73
# down
75
- if self .row < self .total_rows - 1 and not grid [self .row + 1 ][self .col ].is_barrier ():
76
- self .neighhbours .append (grid [self .row + 1 ][self .col ])
74
+ if (
75
+ self .row < self .total_rows - 1
76
+ and not grid [self .row + 1 ][self .col ].is_barrier ()
77
+ ):
78
+ self .neighhbours .append (grid [self .row + 1 ][self .col ])
77
79
# up
78
- if self .row > 0 and not grid [self .row - 1 ][self .col ].is_barrier ():
79
- self .neighhbours .append (grid [self .row - 1 ][self .col ])
80
+ if self .row > 0 and not grid [self .row - 1 ][self .col ].is_barrier ():
81
+ self .neighhbours .append (grid [self .row - 1 ][self .col ])
80
82
# right
81
- if self .col < self .total_rows - 1 and not grid [self .row ][self .col + 1 ].is_barrier ():
82
- self .neighhbours .append (grid [self .row ][self .col + 1 ])
83
+ if (
84
+ self .col < self .total_rows - 1
85
+ and not grid [self .row ][self .col + 1 ].is_barrier ()
86
+ ):
87
+ self .neighhbours .append (grid [self .row ][self .col + 1 ])
83
88
# left
84
- if self .col > 0 and not grid [self .row ][self .col - 1 ].is_barrier ():
85
- self .neighhbours .append (grid [self .row ][self .col - 1 ])
89
+ if self .col > 0 and not grid [self .row ][self .col - 1 ].is_barrier ():
90
+ self .neighhbours .append (grid [self .row ][self .col - 1 ])
86
91
87
- def __lt__ (self , other ):
92
+ def __lt__ (self ):
88
93
return False
89
94
90
95
91
96
def h (p1 , p2 ):
92
97
x1 , y1 = p1
93
98
x2 , y2 = p2
94
- return abs (x1 - x2 )+ abs (y1 - y2 )
99
+ return abs (x1 - x2 ) + abs (y1 - y2 )
95
100
96
101
97
102
def reconstruct_path (came_from , current , draw ):
@@ -127,12 +132,13 @@ def algorithm(draw, grid, start, end):
127
132
return True
128
133
129
134
for neighbour in current .neighhbours :
130
- temp_g_score = g_score [current ]+ 1
135
+ temp_g_score = g_score [current ] + 1
131
136
if temp_g_score < g_score [neighbour ]:
132
137
came_from [neighbour ] = current
133
138
g_score [neighbour ] = temp_g_score
134
- f_score [neighbour ] = temp_g_score + \
135
- h (neighbour .get_pos (), end .get_pos ())
139
+ f_score [neighbour ] = temp_g_score + h (
140
+ neighbour .get_pos (), end .get_pos ()
141
+ )
136
142
if neighbour not in open_set_hash :
137
143
count += 1
138
144
open_set .put ((f_score [neighbour ], count , neighbour ))
@@ -147,7 +153,7 @@ def algorithm(draw, grid, start, end):
147
153
148
154
def make_grid (rows , width ):
149
155
grid = []
150
- gap = width // rows
156
+ gap = width // rows
151
157
for i in range (rows ):
152
158
grid .append ([])
153
159
for j in range (rows ):
@@ -157,11 +163,11 @@ def make_grid(rows, width):
157
163
158
164
159
165
def draw_grid (win , rows , width ):
160
- gap = width // rows
166
+ gap = width // rows
161
167
for i in range (rows ):
162
- pygame .draw .line (win , GREY , (0 , i * gap ), (width , i * gap ))
168
+ pygame .draw .line (win , GREY , (0 , i * gap ), (width , i * gap ))
163
169
for j in range (rows ):
164
- pygame .draw .line (win , GREY , (j * gap , 0 ), (j * gap , width ))
170
+ pygame .draw .line (win , GREY , (j * gap , 0 ), (j * gap , width ))
165
171
166
172
167
173
def draw (win , grid , rows , width ):
@@ -174,14 +180,14 @@ def draw(win, grid, rows, width):
174
180
175
181
176
182
def get_clicked_pos (pos , rows , width ):
177
- gap = width // rows
183
+ gap = width // rows
178
184
y , x = pos
179
- row = y // gap
180
- col = x // gap
185
+ row = y // gap
186
+ col = x // gap
181
187
return row , col
182
188
183
189
184
- def main (win , width ):
190
+ def main (win , width ): # sourcery no-metrics
185
191
ROWS = 50
186
192
grid = make_grid (ROWS , width )
187
193
@@ -204,7 +210,7 @@ def main(win, width):
204
210
elif not end and spot != start :
205
211
end = spot
206
212
end .make_end ()
207
- elif spot != start and spot != end :
213
+ elif spot not in [ start , end ] :
208
214
spot .make_barrier ()
209
215
elif pygame .mouse .get_pressed ()[2 ]: # right mouse btn
210
216
pos = pygame .mouse .get_pos ()
@@ -220,8 +226,7 @@ def main(win, width):
220
226
for row in grid :
221
227
for spot in row :
222
228
spot .update_neighbours (grid )
223
- algorithm (lambda : draw (win , grid , ROWS , width ),
224
- grid , start , end )
229
+ algorithm (lambda : draw (win , grid , ROWS , width ), grid , start , end )
225
230
if event .key == pygame .K_c :
226
231
start = None
227
232
end = None
0 commit comments