-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpathfinding.py
133 lines (118 loc) · 4.84 KB
/
pathfinding.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
import pygame as pg
from os import path
from collections import deque
TILESIZE = 32
GRIDWIDTH = 28
GRIDHEIGHT = 15
WIDTH = TILESIZE * GRIDWIDTH
HEIGHT = TILESIZE * GRIDHEIGHT
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (14, 183, 63)
BLUE = (55, 121, 179)
RED = (255, 84, 76)
YELLOW = (221, 232, 63)
DARKGRAY = (40, 40, 40)
LIGHTGRAY = (100, 100, 100)
pg.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
clock = pg.time.Clock()
vec2 = pg.math.Vector2
class SquareGrid:
def __init__(self, width, height):
self.width = width
self.height = height
self.walls = []
self.connections = [vec2(1, 0), vec2(-1, 0), vec2(0, 1), vec2(0, -1)]
self.connections += [vec2(1, 1), vec2(1, -1), vec2(-1, 1), vec2(-1, -1)]
def in_bounds(self, node):
return 0 <= node.x < self.width and 0 <= node.y < self.height
def passable(self, node):
return node not in self.walls
def find_neighbors(self, node):
neighbors = [node + conn for conn in self.connections]
if (node.x + node.y) % 2:
neighbors.reverse()
neighbors = filter(self.in_bounds, neighbors)
neighbors = filter(self.passable, neighbors)
return neighbors
def draw(self):
for wall in self.walls:
rect = pg.Rect(wall * TILESIZE, (TILESIZE, TILESIZE))
pg.draw.rect(screen, LIGHTGRAY, rect)
def vec2int(v):
return(int(v.x), int(v.y))
def bfs(graph, start, end):
frontier = deque()
frontier.append(start)
path = {}
path[vec2int(start)] = None
while len(frontier) > 0:
current = frontier.popleft()
if current == end:
break
for next_node in graph.find_neighbors(current):
if vec2int(next_node) not in path:
frontier.append(next_node)
path[vec2int(next_node)] = current - next_node
return path
folder = path.dirname(__file__)
arrows = {}
arrow_img = pg.image.load(path.join(folder, 'arrowRight.png')).convert_alpha()
arrow_img = pg.transform.scale(arrow_img, (TILESIZE, TILESIZE))
for dir in [(1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (-1, 1), (1, -1), (-1, -1)]:
arrows[dir] = pg.transform.rotate(arrow_img, vec2(dir).angle_to(vec2(1, 0)))
g = SquareGrid(GRIDWIDTH, GRIDHEIGHT)
walls = [(10, 7), (11, 7), (12, 7), (13, 7), (14, 7), (15, 7), (16, 7), (7, 7), (6, 7), (5, 7), (5, 5), (5, 6), (1, 6), (2, 6), (3, 6), (5, 10), (5, 11), (5, 12), (5, 9), (5, 8), (12, 8), (12, 9), (12, 10), (12, 11), (15, 14), (15, 13), (15, 12), (15, 11), (15, 10), (17, 7), (18, 7), (21, 7), (21, 6), (21, 5), (21, 4), (21, 3), (22, 5), (23, 5), (24, 5), (25, 5), (18, 10), (20, 10), (19, 10), (21, 10), (22, 10), (23, 10), (14, 4), (14, 5), (14, 6), (14, 0), (14, 1), (9, 2), (9, 1), (7, 3), (8, 3), (10, 3), (9, 3), (11, 3), (2, 5), (2, 4), (2, 3), (2, 2), (2, 0), (2, 1), (0, 11), (1, 11), (2, 11), (21, 2), (20, 11), (20, 12), (23, 13), (23, 14), (24, 10), (25, 10), (6, 12), (7, 12), (10, 12), (11, 12), (12, 12), (5, 3), (6, 3), (5, 4)]
for wall in walls:
g.walls.append(vec2(wall))
start = vec2(14, 8)
end = vec2(14, 10)
path = bfs(g, start, end)
def draw_grid():
for x in range(0, WIDTH, TILESIZE):
pg.draw.line(screen, LIGHTGRAY, (x, 0), (x, HEIGHT))
for y in range(0, HEIGHT, TILESIZE):
pg.draw.line(screen, LIGHTGRAY, (0, y), (WIDTH, y))
running = True
while running:
clock.tick(60)
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == pg.KEYDOWN and event.key == pg.K_m:
print([(int(loc.x), int(loc.y)) for loc in g.walls])
if event.type == pg.MOUSEBUTTONDOWN:
mpos = vec2(pg.mouse.get_pos()) // TILESIZE
if event.button == 1:
if mpos in g.walls:
g.walls.remove(mpos)
else:
g.walls.append(mpos)
if event.button == 3:
start = mpos
path = bfs(g, start, end)
screen.fill(DARKGRAY)
# fill explored area
for node in path:
x, y = node
rect = pg.Rect(x * TILESIZE, y * TILESIZE, TILESIZE, TILESIZE)
pg.draw.rect(screen, (75, 75, 75), rect)
draw_grid()
g.draw()
# draw path
current = end + path[vec2int(end)]
while current != start:
x, y = current
x = x * TILESIZE + TILESIZE / 2
y = y * TILESIZE + TILESIZE / 2
img = arrows[vec2int(path[(current.x, current.y)])]
r = img.get_rect(center=(x, y))
screen.blit(img, r)
current = current + path[vec2int(current)]
start_center = (int(start.x*TILESIZE+TILESIZE/2), int(start.y*TILESIZE+TILESIZE/2))
pg.draw.circle(screen, RED, start_center, TILESIZE//2)
end_center = (int(end.x*TILESIZE+TILESIZE/2), int(end.y*TILESIZE+TILESIZE/2))
pg.draw.circle(screen, GREEN, end_center, TILESIZE//2)
pg.display.flip()
pg.quit()