From 85d981a1777df05cce7ad853975b2aef18c9a2b4 Mon Sep 17 00:00:00 2001 From: tonywesoly Date: Sun, 8 May 2022 16:43:43 +0200 Subject: [PATCH] =?UTF-8?q?Poczyszczenie=20kodu=20A=5Fstar=20i=20powi?= =?UTF-8?q?=C4=85zanych=20z=20nim=20funkcji?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astar.py => Astar.py | 45 ++++---------------------------------------- Environment.py | 8 ++++++-- Program.py | 2 +- 3 files changed, 11 insertions(+), 44 deletions(-) rename astar.py => Astar.py (79%) diff --git a/astar.py b/Astar.py similarity index 79% rename from astar.py rename to Astar.py index 0447a8d..f37379b 100644 --- a/astar.py +++ b/Astar.py @@ -1,4 +1,3 @@ -from ast import walk import math import pygame @@ -51,8 +50,8 @@ class Node: if self.walkable: return self.g_cost + self.h_cost else: - return 0 - # return math.inf + # return 0 + return math.inf class Pathfinding: @@ -108,8 +107,8 @@ class Pathfinding: self.path = path for neighbour in self.succ(current_node): - # if not neighbour.walkable or neighbour in explored: - if neighbour in explored: + if not neighbour.walkable or neighbour in explored: + # if neighbour in explored: continue new_movement_cost_to_neighbour = current_node.g_cost + self.get_distance(current_node,neighbour) if new_movement_cost_to_neighbour < neighbour.g_cost or not neighbour in fringe: @@ -150,39 +149,3 @@ class Pathfinding: pygame.draw.rect(window, color, block) - - - - -def cost(node): # funkcja kosztu : ile kosztuje przejechanie przez dane pole - cost = 0 - while node.parent is not None: # FIX!!!!!!!!!! - cost = cost + 1 + 1 - node = node.parent - return cost -# - - -def f(goal, node): # funkcja zwracająca sumę funkcji kosztu oraz heurestyki - return cost(node) + heuristic(goal, node) - - -def heuristic(goal, node): # funkcja heurestyki : oszacowuje koszt osiągnięcia stanu końcowego (droga) - return abs(node.x - goal[0]) + abs(node.y - goal[1]) - - -def print_moves(elem): # zwraca listę ruchów jakie należy wykonać by dotrzeć do punktu docelowego - moves_list = [] - while elem.parent is not None: - moves_list.append(elem.action) - elem = elem.parent - moves_list.reverse() - return moves_list - - -def succ(elem): # funkcja następnika, przypisuje jakie akcje są możliwe do wykonania na danym polu oraz jaki będzie stan (kierunek, położenie) po wykonaniu tej akcji - pass - - -def graphsearch(explored, fringe, goaltest, istate): # przeszukiwanie grafu wszerz - pass diff --git a/Environment.py b/Environment.py index 8107490..7567b59 100644 --- a/Environment.py +++ b/Environment.py @@ -10,7 +10,7 @@ from Truck import Truck from Global_variables import Global_variables as G_var from pygame.constants import * -from astar import Pathfinding, State +from Astar import Pathfinding, State class Environment: @@ -34,9 +34,13 @@ class Environment: for field in row: field.draw() self.grid.draw_grid() - self.use_astar() # w przyszlosci trzeba przeniesc funkcje w jakies logiczniejsze miejsce np funkcje update() self.astar.draw_path(self.window) pygame.display.flip() + + def update_all_elements(self,event): + self.use_astar() + self.update_truck(event) + def use_astar(self): start_state = State(1,self.truck.x,self.truck.y) diff --git a/Program.py b/Program.py index 03fc637..a582b73 100644 --- a/Program.py +++ b/Program.py @@ -17,5 +17,5 @@ class Program: for event in pygame.event.get(): # integrating with keyboard if event.type == QUIT: running = False - self.environment.update_truck(event) + self.environment.update_all_elements(event) self.environment.draw_all_elements()