From 96b42fb6c027fcae30d43f292828d8eb8fd69148 Mon Sep 17 00:00:00 2001 From: Marcin Dobrowolski Date: Sun, 26 Apr 2020 14:14:35 +0200 Subject: [PATCH] 2nd part AStar implementation --- main.py | 2 + src/__pycache__/tile.cpython-38.pyc | Bin 869 -> 869 bytes src/__pycache__/waiter.cpython-38.pyc | Bin 2786 -> 3598 bytes src/tile.py | 6 +- src/waiter.py | 111 +++++++++++++++++--------- 5 files changed, 80 insertions(+), 39 deletions(-) diff --git a/main.py b/main.py index 016ffe0..89e16cc 100644 --- a/main.py +++ b/main.py @@ -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() diff --git a/src/__pycache__/tile.cpython-38.pyc b/src/__pycache__/tile.cpython-38.pyc index dc6801d1d4bdbc715d8ff75e7eac477507d9d358..9e713a27cdc556699693f3809a36ce4a3033194a 100644 GIT binary patch delta 173 zcmaFL_LPk`l$V!_0SG(`mc~8W$UA|N=N4yiNn%lnbAEBjt;s7Hl^BI4pJ8lfWSShn z6rjuxRCB)zg?0rQ*a$;}=s7i|%ff7tWT+9L_ YI2bu3z#@K{j9?ncV6Z;n$^Oh@0DiV2ga7~l delta 173 zcmaFL_LPk`l$V!_0SLH0EsA@&k#_|U=cq}Mlg+JFj$}9WPfHc01*EnssI20 diff --git a/src/__pycache__/waiter.cpython-38.pyc b/src/__pycache__/waiter.cpython-38.pyc index 437831fcc786b661f65c99c034d89d754abd5895..3cbbe93b6fe1b72e3c17767f820c98e0d04bb26f 100644 GIT binary patch literal 3598 zcmai1&2QX96rZujUVC?whBj&wgsM;#!AezAReXpFRh61Tt3rrqtAYi%%Z{7%?q`l|F$cl8nWu7DXHP&!!;-l&R6#2VqrM`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vL)-zL|T`u49#f8H4 zw>XxKnz=qENh5ds!61lKZuNr*w|1=4mQ2Hb(5j|!>i4h4Nt#=Oc%Z+c&#NZIwB$w-!tDxl)p%}%0yB#KJtJdzvNubPlroE;$vQi<^?u31XY0EA~bTO4KEB7hh z2TOk1xd?i42MrTP_`D^#6g4p`C==C$DU9_6+WKCJq@TbA$?hl1Mm+9S>5V$Mq@_!y}P1FDY diff --git a/src/tile.py b/src/tile.py index 56273a9..a4c0d88 100644 --- a/src/tile.py +++ b/src/tile.py @@ -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 diff --git a/src/waiter.py b/src/waiter.py index f0fa5f8..9230a1d 100644 --- a/src/waiter.py +++ b/src/waiter.py @@ -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