-
Notifications
You must be signed in to change notification settings - Fork 0
/
A Star Pathfinding.py
167 lines (127 loc) · 4.4 KB
/
A Star 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import pygame, random, math, queue, pdb
pygame.init()
clock = pygame.time.Clock()
SCREEN_WIDTH = 800
COLUMNS = 100
ROWS = 100
CELL_WIDTH = 8
CELL_COLOURS = {
"empty" : (128, 128, 128),
"wall" : (0, 0, 0),
"start" : (0, 255, 0),
"goal" : (255, 0, 0),
"path" : (255, 255, 255),
"explored" : (0, 0, 255),
}
DIRECTIONS = {
"R" : pygame.math.Vector2(1, 0),
"L" : pygame.math.Vector2(-1, 0),
"D" : pygame.math.Vector2(0, 1),
"U" : pygame.math.Vector2(0, -1)
}
from dataclasses import dataclass, field
from typing import Any
@dataclass(order=True)
class PrioritizedItem:
priority: int
item: Any=field(compare=False)
size = (SCREEN_WIDTH, SCREEN_WIDTH)
screen = pygame.display.set_mode(size)
grid = []
for column in range(COLUMNS):
grid.append([])
for row in range(ROWS):
grid[column].append("empty")
for i in range(2500):
x = random.randint(2, ROWS - 3)
y = random.randint(2, COLUMNS - 3)
if grid[y][x] == "empty":
grid[y][x] = "wall"
class Node:
global nodes, grid, CELL_COLOURS
def __init__(self, nodeType, position):
nodes.append(self)
self.position = position
self.nodeType = nodeType
self.colour = CELL_COLOURS[nodeType]
grid[int(position.y)][int(position.x)] = nodeType
def getNeighbours(position):
global DIRECTIONS
neighbours = []
for key, direction in DIRECTIONS.items():
neighbourPosition = position + direction
neighbours.append(neighbourPosition)
return neighbours
def getKey(position):
key = str(int(position.x)) + "_" + str(int(position.y))
return key
def pathfind(startPosition, goalPosition):
global grid
frontier = queue.PriorityQueue()
frontier.put(PrioritizedItem(0, startPosition))
cameFrom = dict()
costSoFar = dict()
startPositionKey = getKey(startPosition)
cameFrom[startPositionKey] = None
costSoFar[startPositionKey] = 0
while not frontier.empty():
current = frontier.get().item
currentKey = getKey(current)
for neighbour in getNeighbours(current):
cell = grid[int(neighbour.y)][int(neighbour.x)]
neighbourKey = getKey(neighbour)
if cell == "goal":
return currentKey, cameFrom
if cell == "wall" or cell == "start":
continue
newCost = costSoFar[currentKey] + 0.01
if neighbourKey not in costSoFar or newCost < costSoFar[neighbourKey]:
costSoFar[neighbourKey] = newCost
distance = neighbour.distance_to(goalPosition)
priority = costSoFar[neighbourKey] + distance
frontier.put(PrioritizedItem(priority, neighbour))
cameFrom[neighbourKey] = current
grid[int(neighbour.y)][int(neighbour.x)] = "explored"
def drawCell(colour, position):
global CELL_WIDTH
pygame.draw.rect(screen,
colour,
(int(position.x * CELL_WIDTH),
int(position.y * CELL_WIDTH),
CELL_WIDTH,
CELL_WIDTH))
def drawGrid():
global grid, COLUMNS, ROWS, CELL_COLOURS
for column in range(COLUMNS):
for row in range(ROWS):
cell = grid[column][row]
colour = CELL_COLOURS[cell]
position = pygame.math.Vector2(row, column)
drawCell(colour, position)
def createWalls():
global grid, COLUMNS, ROWS
for column in range(COLUMNS):
grid[column][0] = "wall"
grid[column][ROWS - 1] = "wall"
for row in range(ROWS):
grid[0][row] = "wall"
grid[COLUMNS - 1][row] = "wall"
createWalls()
nodes = []
start = Node("start", pygame.math.Vector2(1, 1))
goal = Node("goal", pygame.math.Vector2(98, 98))
currentKey, cameFrom = pathfind(start.position, goal.position)
while cameFrom[currentKey]:
position = cameFrom[currentKey]
grid[int(position.y)][int(position.x)] = "path"
currentKey = getKey(position)
drawGrid()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#screen.fill((0, 0, 0))
pygame.display.update()
pygame.quit()