2nd part AStar implementation

This commit is contained in:
Marcin Dobrowolski 2020-04-26 14:14:35 +02:00
parent cdbc599d14
commit 96b42fb6c0
5 changed files with 80 additions and 39 deletions

View File

@ -14,6 +14,7 @@ if __name__ == "__main__":
# init functions
graphics.drawBackground(waiter.matrix)
graphics.update(waiter.X, waiter.Y)
print(waiter.findPath((1, 10)))
while True:
for event in pygame.event.get():
@ -29,6 +30,7 @@ if __name__ == "__main__":
break
graphics.clear(waiter.X, waiter.Y)
waiter.update(event, graphics)
graphics.update(waiter.X, waiter.Y)
pygame.display.flip()

Binary file not shown.

View File

@ -18,9 +18,9 @@ class Tile:
# Atrybuty niezbedne dla A*
self.position = (x, y)
self.parent = None
self.totalCost = 0 # Koszt totalny czyli dystans do wierzcholka startowego + heurystyka
self.startCost = 0 # Dystans do wierzcholka startowego
# Dystant do wierzcholka koncowego oszacowany za pomoca funkcji heurystyki
self.totalCost = 0 # Koszt totalny czyli dystans do wierzcholka startowego + heurystyka F
self.startCost = 0 # Dystans do wierzcholka startowego G
# Dystant do wierzcholka koncowego oszacowany za pomoca funkcji heurystyki H
self.heuristic = 0
# Operator porownywania pol

View File

@ -1,6 +1,7 @@
import pygame
from .matrix import Matrix
from .tile import Tile
# WAITER
@ -12,6 +13,7 @@ class Waiter(pygame.sprite.Sprite):
self.frame = 0
self.matrix = Matrix(graphics=graphics)
self.direction = 'E'
self.tile = Tile(self, self.X, self.Y)
# Borders
def move(self, x, y, graphics):
@ -53,61 +55,98 @@ class Waiter(pygame.sprite.Sprite):
# AStar
def findPath(self, goal):
#Stworzenie startowego i koncowego wierzcholka
# Stworzenie startowego i koncowego wierzcholka
startNode = self.matrix.matrix[self.X][self.Y]
goalNode = self.matrix.matrix[goal[0]][goal[1]]
#Inicjalizacja list
# Inicjalizacja list
openList = []
closedList = []
openList.append(startNode)
while len(openList) > 0:
openList.sort(key=tile.totalCost)
openList.sort(key=getTotalCost)
currentNode = openList.pop(0)
closedList.append(currentNode)
print("\nOpenList")
for tile in openList:
print(tile.position, end=" ")
print("\nClosed List")
for tile in closedList:
print(tile.position, end=' ')
# Tutaj odbywac sie bedzie budowanie sciezki gdy algorytm osiagnie cel
if currentNode == goalNode:
pass
#Tutaj odbywac sie bedzie budowanie sciezki gdy algorytm osiagnie cel
path = []
current = currentNode
while current is not None:
path.append(current.position)
current = current.parent
return path[::-1]
children = []
if currentNode.X >= 0:
if currentNode.Y + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.X][currentNode.Y + 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X][currentNode.Y + 1])
if currentNode.Y - 1 >= 0:
if self.matrix.matrix[currentNode.X][currentNode.Y - 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X][currentNode.Y - 1])
if currentNode.position[0] >= 0:
if currentNode.position[1] + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.position[0]][currentNode.position[1] + 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0]][currentNode.position[1] + 1])
if currentNode.position[1] - 1 >= 0:
if self.matrix.matrix[currentNode.position[0]][currentNode.position[1] - 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0]][currentNode.position[1] - 1])
if currentNode.X + 1 < len(self.matrix.matrix):
if currentNode.Y + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.X + 1][currentNode.Y + 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X + 1][currentNode.Y + 1])
if currentNode.Y - 1 >= 0:
if self.matrix.matrix[currentNode.X + 1][currentNode.Y - 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X + 1][currentNode.Y - 1])
if currentNode.Y >= 0:
if self.matrix.matrix[currentNode.X + 1][currentNode.Y].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X + 1][currentNode.Y])
if currentNode.position[0] + 1 < len(self.matrix.matrix):
if currentNode.position[1] + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1] + 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1] + 1])
if currentNode.position[1] - 1 >= 0:
if self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1] - 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1] - 1])
if currentNode.position[1] >= 0:
if self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1]].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] + 1][currentNode.position[1]])
if currentNode.X - 1 >= 0:
if currentNode.Y + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.X - 1][currentNode.Y + 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X - 1][currentNode.Y + 1])
if currentNode.Y - 1 >= 0:
if self.matrix.matrix[currentNode.X - 1][currentNode.Y - 1].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X - 1][currentNode.Y - 1])
if currentNode.Y >= 0:
if self.matrix.matrix[currentNode.X - 1][currentNode.Y].walk_through == 1:
children.append(self.matrix.matrix[currentNode.X - 1][currentNode.Y])
if currentNode.position[0] - 1 >= 0:
if currentNode.position[1] + 1 < len(self.matrix.matrix[0]):
if self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1] + 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1] + 1])
if currentNode.position[1] - 1 >= 0:
if self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1] - 1].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1] - 1])
if currentNode.position[1] >= 0:
if self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1]].walk_through == 1:
children.append(
self.matrix.matrix[currentNode.position[0] - 1][currentNode.position[1]])
for child in children:
if child in closedList:
continue
#child.parent = currentNode
for closedChild in closedList:
if child == closedChild:
continue
child.startCost = currentNode.startCost + 1
child.heuristic = ((child.position[0] - goalNode.position[0]) ** 2) + (
(child.position[1] - goalNode.position[1]) ** 2)
child.totalCost = child.startCost + child.heuristic
for openNode in openList:
if child == openNode and child.startCost >= openNode.startCost:
continue
#child.parent = currentNode
openList.append(child)
def getTotalCost(tile):
return tile.totalCost