Skip to content

Commit d889187

Browse files
committed
Refactored and formatted code
1 parent 863d060 commit d889187

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

app.py

+34-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pygame
22
from queue import PriorityQueue
33

4-
WIDTH = 800
4+
WIDTH = 700
55
WIN = pygame.display.set_mode((WIDTH, WIDTH))
66
pygame.display.set_caption("A* Path Finding Algorithm Visualizer")
77

@@ -19,8 +19,8 @@ class Node:
1919
def __init__(self, row, col, width, total_rows):
2020
self.row = row
2121
self.col = col
22-
self.x = row*width
23-
self.y = col*width
22+
self.x = row * width
23+
self.y = col * width
2424
self.color = WHITE
2525
self.neighhbours = []
2626
self.width = width
@@ -66,32 +66,37 @@ def make_path(self):
6666
self.color = YELLOW
6767

6868
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))
7170

7271
def update_neighbours(self, grid):
7372
self.neighhbours = []
7473
# 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])
7779
# 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])
8082
# 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])
8388
# 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])
8691

87-
def __lt__(self, other):
92+
def __lt__(self):
8893
return False
8994

9095

9196
def h(p1, p2):
9297
x1, y1 = p1
9398
x2, y2 = p2
94-
return abs(x1-x2)+abs(y1-y2)
99+
return abs(x1 - x2) + abs(y1 - y2)
95100

96101

97102
def reconstruct_path(came_from, current, draw):
@@ -127,12 +132,13 @@ def algorithm(draw, grid, start, end):
127132
return True
128133

129134
for neighbour in current.neighhbours:
130-
temp_g_score = g_score[current]+1
135+
temp_g_score = g_score[current] + 1
131136
if temp_g_score < g_score[neighbour]:
132137
came_from[neighbour] = current
133138
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+
)
136142
if neighbour not in open_set_hash:
137143
count += 1
138144
open_set.put((f_score[neighbour], count, neighbour))
@@ -147,7 +153,7 @@ def algorithm(draw, grid, start, end):
147153

148154
def make_grid(rows, width):
149155
grid = []
150-
gap = width//rows
156+
gap = width // rows
151157
for i in range(rows):
152158
grid.append([])
153159
for j in range(rows):
@@ -157,11 +163,11 @@ def make_grid(rows, width):
157163

158164

159165
def draw_grid(win, rows, width):
160-
gap = width//rows
166+
gap = width // rows
161167
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))
163169
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))
165171

166172

167173
def draw(win, grid, rows, width):
@@ -174,14 +180,14 @@ def draw(win, grid, rows, width):
174180

175181

176182
def get_clicked_pos(pos, rows, width):
177-
gap = width//rows
183+
gap = width // rows
178184
y, x = pos
179-
row = y//gap
180-
col = x//gap
185+
row = y // gap
186+
col = x // gap
181187
return row, col
182188

183189

184-
def main(win, width):
190+
def main(win, width): # sourcery no-metrics
185191
ROWS = 50
186192
grid = make_grid(ROWS, width)
187193

@@ -204,7 +210,7 @@ def main(win, width):
204210
elif not end and spot != start:
205211
end = spot
206212
end.make_end()
207-
elif spot != start and spot != end:
213+
elif spot not in [start, end]:
208214
spot.make_barrier()
209215
elif pygame.mouse.get_pressed()[2]: # right mouse btn
210216
pos = pygame.mouse.get_pos()
@@ -220,8 +226,7 @@ def main(win, width):
220226
for row in grid:
221227
for spot in row:
222228
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)
225230
if event.key == pygame.K_c:
226231
start = None
227232
end = None

0 commit comments

Comments
 (0)