diff --git a/astar.py b/astar.py index 8e73f5a..96258d7 100644 --- a/astar.py +++ b/astar.py @@ -5,10 +5,13 @@ def heurystyka(obiekty, a, b): heur = abs((b[0] - a[0])) + abs((b[1] - a[1])) if obiekty["plansza"][b[0], b[1]].jestDomem is True: heur += 2 + if obiekty["plansza"][b[0], b[1]].jestPrzeszkoda is True: + heur += 100 return heur def astar(obiekty, start, cel): + sasiedzi = [(0, 1), (0, -1), (1, 0), (-1, 0)] close_set = set() came_from = {} @@ -24,6 +27,7 @@ def astar(obiekty, start, cel): obiekty["plansza"][current[0], current[1]].setKolor((255, 0, 0)) data = [] while current in came_from: + data.append(current) current = came_from[current] return data[::-1] @@ -40,9 +44,9 @@ def astar(obiekty, start, cel): continue tentative_h_score = heurystyka(obiekty, sasiad, cel) + heurystyka(obiekty, current, sasiad) - if sasiad in close_set and tentative_h_score > hscore.get(current, 0): + if sasiad in [i[1] for i in oheap] and tentative_h_score > hscore.get(current, 0): continue - if tentative_h_score <= hscore.get(current, 0) or sasiad not in [i[1] for i in oheap]: + if sasiad not in close_set and sasiad not in [i[1] for i in oheap]: came_from[sasiad] = current hscore[sasiad] = tentative_h_score fscore[sasiad] = tentative_h_score + gscore[current] diff --git a/modele.py b/modele.py index a52d992..148db04 100644 --- a/modele.py +++ b/modele.py @@ -57,7 +57,7 @@ class Smieciarka(pygame.sprite.Sprite): self.w_dol() def astar_move(self, obiekty): - sciezka = astar.astar(obiekty, self.pozycja, (14, 0)) + sciezka = astar.astar(obiekty, self.pozycja, (random.randrange(15), random.randrange(15))) print(sciezka) for koord in sciezka: if koord[0] == self.x - 1 and koord[1] == self.y: