-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (115 loc) · 4.52 KB
/
main.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
import pygame
from node import *
WIDTH, HEIGHT = 820, 570
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("PathFinder A*")
grid: list[list[Node]] = []
ROWS, COLS = 15, 10
#nodes:
# contains a list of all the active node
# contains the target node
nodes: dict[str:list[Node], str:Node] = {"active": [], "target": None}
path: list[Node] = []
def creteGrid() -> None:
for x in range(ROWS):
col = []
for y in range(COLS):
col.append(Node(x, y))
grid.append(col)
for row in grid:
for col in row:
col.setNeighbors(grid)
startx, starty = 1, 8
#set the start
grid[startx][starty].state = "active"
grid[startx][starty].path = [grid[startx][starty]]
nodes["active"].append(grid[startx][starty])
#set the end
grid[ROWS-1][COLS-1].state = "target"
nodes["target"] = grid[ROWS-1][COLS-1]
#create walls
for y in range(2, 10):
grid[7][y].state = "wall"
creteGrid()
for x in grid:
for n in x:
n.updateNeighbors()
def drawGrid() -> None:
NODE_SIZE: int = 50
n = 5
for row in grid:
for node in row:
if node.state == None:
pygame.draw.rect(WINDOW, (200, 200, 200), pygame.Rect(node.x * (NODE_SIZE + n), node.y * (NODE_SIZE + n), NODE_SIZE, NODE_SIZE))
elif node.state == "active":
pygame.draw.rect(WINDOW, (255, 255, 0), pygame.Rect(node.x * (NODE_SIZE + n), node.y * (NODE_SIZE + n), NODE_SIZE, NODE_SIZE))
elif node.state == "target":
pygame.draw.rect(WINDOW, (255, 0, 0), pygame.Rect(node.x * (NODE_SIZE + n), node.y * (NODE_SIZE + n), NODE_SIZE, NODE_SIZE))
elif node.state == "used":
pygame.draw.rect(WINDOW, (200, 0, 200), pygame.Rect(node.x * (NODE_SIZE + n), node.y * (NODE_SIZE + n), NODE_SIZE, NODE_SIZE))
elif node.state == "wall":
pygame.draw.rect(WINDOW, (0, 0, 255), pygame.Rect(node.x * (NODE_SIZE + n), node.y * (NODE_SIZE + n), NODE_SIZE, NODE_SIZE))
elif node.state == "path":
pygame.draw.rect(WINDOW, (0, 100, 0), pygame.Rect(node.x * (NODE_SIZE + n), node.y * (NODE_SIZE + n), NODE_SIZE, NODE_SIZE))
def update():
completed = isCompleted()
if completed:
for n in path:
n.state = "path"
else:
nodes["active"].append(activateNode(nodes["active"], nodes["target"]))
def isCompleted() -> bool:
DIRECTIONS = {(0, 1), (1, 0), (0, -1), (-1, 0)}
for d in DIRECTIONS:
x = nodes["target"].x + d[0]
y = nodes["target"].y + d[1]
#check if the node exist in the grid
if x >= 0 and y >= 0 and x < ROWS and y < COLS:
if grid[x][y].state == "active" or grid[x][y].state == "path":
grid[x][y].state = "path"
path.extend(grid[x][y].path)
return True
#this is the A* algorithm
def activateNode(activeNodes: list[Node], targetNode: Node) -> Node:
updateActiveNodes()
possibleNode: Node = None
possibleFValue: int = None
startNode: Node = None
for n in activeNodes:
n.updateNeighbors()
for neighbor in n.neighbors:
if possibleNode == None:
possibleNode = neighbor
possibleFValue = neighbor.f(n, targetNode)
startNode = n
else:
if neighbor.f(n, targetNode) < possibleFValue:
possibleNode = neighbor
possibleFValue = neighbor.f(n, targetNode)
startNode = n
possibleNode.state = "active"
possibleNode.path = startNode.path.copy()
possibleNode.path.append(startNode)
return possibleNode
#remove the non active node
def updateActiveNodes() -> None:
for n in nodes["active"]:
if n.state != "active":
nodes["active"].remove(n)
def draw():
drawGrid()
pygame.display.update()
def main():
run = True
clock = pygame.time.Clock()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
update()
draw()
pygame.quit()
if __name__ == "__main__":
main()