-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathfinder.py
More file actions
292 lines (249 loc) · 11.1 KB
/
Pathfinder.py
File metadata and controls
292 lines (249 loc) · 11.1 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import pygame
from pygame.locals import *
walls = []
Pixel_group = pygame.sprite.Group()
##font = pygame.font.Font('Constantia', 36)
Clock = pygame.time.Clock()
class Vector2:
def __init__(self,x,y):
self.x = x
self.y = y
class FinderTree:
def __init__(self, root: 'Node', pixels):
self.pixels = pixels
root.children = self.getChildren(root)
self.root = root
self.frontier = []
self.explored = []
def findPath(self,to):
self.to = to
current = self.root
self.explored.append(self.root)
self.explore(current)
new_pixel = Pixel((current.pos.x, current.pos.y), 'caracter')
Pixel_group.add(new_pixel)
while(len(self.frontier) > 0):
##print('loopou')
pygame.time.delay(10)
screen.fill((255, 255, 255))
Pixel_group.draw(screen)
pygame.display.flip()
current = self.getSmallestCostNode()
##print(f"{current.pos.x},{current.pos.y}")
self.explore(current)
self.explored.append(current)
self.frontier.remove(current)
new_pixel = Pixel((current.pos.x, current.pos.y), 'explored')
Pixel_group.add(new_pixel)
if(current.pos.x == to.x and current.pos.y == to.y):
return self.searchPath(current)
def explore(self, node: 'Node'):
##print("explore")
testChildren = self.getChildren(node)
for child in testChildren:
##print(f"{child.pos.x};{child.pos.y} Test")
if(not Pixel.TesteColide(child.pos.x,child.pos.y)):
b = True
for node in self.explored:
##print(F"Compare {node.pos.x};{node.pos.y} and {child.pos.x}; {child.pos.y}")
if(node.pos.x == child.pos.x and node.pos.y == child.pos.y):
##print('ingual')
b = False
for node in self.frontier:
##print(F"Compare {node.pos.x};{node.pos.y} and {child.pos.x}; {child.pos.y}")
if(node.pos.x == child.pos.x and node.pos.y == child.pos.y):
##print('ingual')
b = False
if(b):
##print(f"{child.pos.x};{child.pos.y} appended")
self.frontier.append(child)
new_pixel = Pixel((child.pos.x, child.pos.y), 'frontier')
Pixel_group.add(new_pixel)
##print("EndExplore")
return
def searchPath(self,node:'Node'):
while(node.parent != None):
Pixel_group.draw(screen)
pygame.display.flip()
new_pixel = Pixel((node.pos.x, node.pos.y), 'path')
Pixel_group.add(new_pixel)
print(node.gcost)
node = node.parent
def getChildren(self,node:'Node'):
children = []
##print('StartGetCHildren')
child = Node(Vector2(node.pos.x +squareSize, node.pos.y) ,node)
children.append(child )
child = Node(Vector2(node.pos.x -squareSize, node.pos.y) ,node)
children.append(child )
child = Node(Vector2(node.pos.x, node.pos.y + squareSize) ,node)
children.append(child )
child = Node(Vector2(node.pos.x, node.pos.y - squareSize) ,node)
children.append(child )
##print("EndGetChildren")
return children
def getSmallestCostNode(self):
##print('getsmallestcostnode')
current = self.frontier[0]
currentcost = current.getCost(self.to)
for node in self.frontier:
nodecost = node.getCost(self.to)
if(nodecost < currentcost):
currentcost = nodecost
current = node
##print(f"smallestcost = {currentcost}")
##print('endgetsmallestcostnoed')
return current
"""'''''''''"""
"""
def drawpath(self):
steps = []
for step in steps:
Pixel_group.draw(Pixel(self.pixels.postoScreen(step.pos.x),self.pixels.postoScreen(step.pos.y)))
draw_text(f'{steps.index(step) +1}')
"""
class Node:
def __init__(self,pos:Vector2, parent:'Node' = None):
print(type(parent))
self.pos = pos
self.parent = parent
if(parent != None):
if(parent.gcost != None):
self.gcost = parent.gcost + 1
else:
self.gcost = 0
def getCost(self,to:'Vector2' = None ) -> int:
if(to == None):
return 0
cost = round( (((self.pos.x - to.x)**2 + (self.pos.y - to.y)**2)**(1/2))/squareSize)
##print(f"hcost = {cost}; gcost = {self.gcost}")
return cost + self.gcost
pygame.init()
screen_width = 1000
screen_height = 1000
screen = pygame.display.set_mode((screen_width,screen_height))
pixel_color = (0,0,0)
player_color = (255,0,0)
screen.fill((255,255,255))
running = True
start = Vector2(0,0)
end = Vector2(0,0)
squareSize = 40
drawing = False
candrawFrom = True
candrawTo = True
startPos:Vector2
finalPos:Vector2
class Pixel(pygame.sprite.Sprite):
def __init__(self, position, group):
pygame.sprite.Sprite.__init__(self)
self.group = group
if self.group == "wall":
self.image = pygame.Surface((squareSize, squareSize))
self.image.fill(pixel_color)
self.rect = self.image.get_rect(topleft=position)
elif self.group == "caracter":
self.image = pygame.Surface((squareSize, squareSize))
self.image.fill(player_color)
self.rect = self.image.get_rect(topleft=position)
elif self.group == "end":
self.image = pygame.Surface((squareSize, squareSize))
self.image.fill((0,255,0))
self.rect = self.image.get_rect(topleft=position)
elif self.group == "frontier":
self.image = pygame.Surface((squareSize, squareSize))
self.image.fill((255, 165, 0))
self.rect = self.image.get_rect(topleft=position)
elif self.group == "explored":
self.image = pygame.Surface((squareSize, squareSize))
self.image.fill((255, 0, 0))
self.rect = self.image.get_rect(topleft=position)
elif self.group == "path":
self.image = pygame.Surface((squareSize, squareSize))
self.image.fill((0, 255, 255))
self.rect = self.image.get_rect(topleft=position)
def screenpixeltoPos(self,x,y):
x = round(x/squareSize)*squareSize
y = round(y/squareSize)*squareSize
return(x,y)
def screendottoPos(x):
x = round(x/squareSize)*squareSize
return x
def postoScreen(x):
return x*squareSize
def TesteColide(x,y):
x = round(x/squareSize)*squareSize
y = round(y/squareSize)*squareSize
for pixel in Pixel_group:
if pixel.group == "wall":
if pixel.rect.collidepoint(x,y):
print(f"{x},{y}")
return True
return False
def add_borders():
for x in range(0, screen_width, squareSize):
top_pixel = Pixel((x, 0), 'wall')
bottom_pixel = Pixel((x, screen_height - squareSize), 'wall')
Pixel_group.add(top_pixel, bottom_pixel)
for y in range(0, screen_height, squareSize):
left_pixel = Pixel((0, y), 'wall')
right_pixel = Pixel((screen_width - squareSize, y), 'wall')
Pixel_group.add(left_pixel, right_pixel)
##nove função que recebe posição em vector de 0 a 25 e desenha um quadrado nessa posição
## def funcção leandro colisaõ (recebe uma posição em pontos do mapa (de 0 a 25) e retorna se existe um quadrado la ou não )
## recebe vector2 como inpu
pixel:Pixel
Pixel.add_borders()
while running:
Clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
drawing = True
x = Pixel.screendottoPos(event.pos[0] - squareSize / 2)
y = Pixel.screendottoPos(event.pos[1] - squareSize / 2)
new_pixel = Pixel((x, y), 'wall')
Pixel_group.add(new_pixel)
elif event.button == 2 and candrawFrom:
candrawFrom = False
drawing = True
x = Pixel.screendottoPos(event.pos[0] - squareSize / 2)
y = Pixel.screendottoPos(event.pos[1] - squareSize / 2)
new_pixel = Pixel((x, y), 'caracter')
startPos = Vector2(x,y)
pixel = new_pixel
Pixel_group.add(new_pixel)
elif event.button == 3 and candrawTo:
candrawTo = False
drawing = True
x = Pixel.screendottoPos(event.pos[0] - squareSize / 2)
y = Pixel.screendottoPos(event.pos[1] - squareSize / 2)
new_pixel = Pixel((x, y), 'end')
endPos = Vector2(x,y)
Pixel_group.add(new_pixel)
elif event.type == MOUSEBUTTONUP:
if event.button in {1, 2, 3}:
drawing = False
elif event.type == MOUSEMOTION and drawing and event.buttons[0] == 1:
x = Pixel.screendottoPos(event.pos[0] - squareSize / 2)
y = Pixel.screendottoPos(event.pos[1] - squareSize / 2)
new_pixel = Pixel((x, y), 'wall')
Pixel_group.add(new_pixel)
elif event.type == KEYDOWN:
if event.key == K_SPACE:
x = Pixel.screendottoPos(pygame.mouse.get_pos()[0] - squareSize / 2)
y = Pixel.screendottoPos(pygame.mouse.get_pos()[1] - squareSize / 2)
if(not candrawFrom and not candrawTo):
startPos = pixel.screenpixeltoPos(startPos.x,startPos.y)
startPos = Vector2(startPos[0],startPos[1])
Arvore = FinderTree(Node(startPos),pixel)
Arvore.findPath(endPos)
# Desenhando na tela
if event.key == K_f:
Pixel.TesteColide(Pixel.screendottoPos(pygame.mouse.get_pos()[0]),Pixel.screendottoPos(pygame.mouse.get_pos()[1]))
screen.fill((255, 255, 255))
Pixel_group.draw(screen)
pygame.display.flip()
pygame.quit()