fix: agent moving astar (still (x,y))

This commit is contained in:
s481862 2024-05-09 11:50:45 +02:00
parent d130f2360b
commit 713a1e6679
2 changed files with 18 additions and 2 deletions

4
app.py
View File

@ -89,6 +89,9 @@ def watekDlaSciezkiAgenta():
agent.rotate_right() agent.rotate_right()
if element == "forward": if element == "forward":
agent.move_direction() agent.move_direction()
elif isinstance(element, tuple): # Check if it's a tuple indicating movement coordinates
x, y = element
agent.moveto(x, y)
time.sleep(1) time.sleep(1)
running = True running = True
@ -132,7 +135,6 @@ while running:
watek = threading.Thread(target=watekDlaSciezkiAgenta) watek = threading.Thread(target=watekDlaSciezkiAgenta)
watek.daemon = True watek.daemon = True
watek.start() watek.start()
if pygame.key.get_pressed()[pygame.K_e]: if pygame.key.get_pressed()[pygame.K_e]:

View File

@ -20,6 +20,7 @@ class Agent:
self.directionPOM = 0 self.directionPOM = 0
self.xPOM = 5 self.xPOM = 5
self.yPOM = 5 self.yPOM = 5
self.g_scores = {}
self.textures = [ self.textures = [
pygame.image.load("sprites/BartenderNew64.png").convert_alpha(), pygame.image.load("sprites/BartenderNew64.png").convert_alpha(),
@ -262,6 +263,15 @@ class Agent:
return [] return []
#Algorytm astar #Algorytm astar
def moveto(self,x,y):
if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.X < prefs.GRID_SIZE-1 and not self.cells[x][y].blocking_movement:
self.current_cell = self.cells[x][y]
self.moved=True
self.last_move_time=pygame.time.get_ticks()
print("Agent moved to x,y: ",x,y)
else:
print("Agent cannot move to this direction")
def get_cost(self, cell): def get_cost(self, cell):
x, y = cell[0], cell[1] x, y = cell[0], cell[1]
if x == 0 or x == len(self.cells) - 1 or y == 0 or y == len(self.cells[0]) - 1: if x == 0 or x == len(self.cells) - 1 or y == 0 or y == len(self.cells[0]) - 1:
@ -318,10 +328,14 @@ class Agent:
heapq.heappush(open_list, (f_score, neighbor_coords)) heapq.heappush(open_list, (f_score, neighbor_coords))
return [], float('inf') # If no path found, return an empty path and infinite cost return [], float('inf')