From 687a630576e3a6a603afed7e899bc04688b48651 Mon Sep 17 00:00:00 2001 From: MlodyJ Date: Sat, 13 Apr 2024 11:53:13 +0200 Subject: [PATCH] feat: agent moves normally now, Added features to handle agent self-traffic and threads Fix: agent move and errors --- app.py | 43 ++++++++++++++++++++++++++----------------- classes/agent.py | 2 +- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app.py b/app.py index 40bb5c1..7c42b44 100644 --- a/app.py +++ b/app.py @@ -6,6 +6,8 @@ from pygame.locals import K_w, K_s, K_a, K_d from classes.cell import Cell from classes.agent import Agent from collections import deque +import threading +import time pygame.init() window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT)) pygame.display.set_caption("Game Window") @@ -45,17 +47,12 @@ def draw_grid(window, cells, agent): window.blit(scoreText, (0, 0)) window.blit(multiplierText, (0, 50)) - - initBoard() def bfs(start, target, cells): - - queue = deque([(start,[])]) visited = set() - while queue: current, path = queue.popleft() if current==target: @@ -85,13 +82,12 @@ def get_neighbors(cell, cells): return neighbors - - -#Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe +#Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe do funkcji znajdujacej najkrotsza sciezke start_cell = cells[5][5] -target_cell = cells[10][10] +target_cell = cells[3][3] shortest_path = bfs(start_cell, target_cell, cells) + if shortest_path: print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path]) else: @@ -106,7 +102,7 @@ def convert_to_coordinates(shortest_path): path = convert_to_coordinates(shortest_path) -#Wyjmuje pierwsze koordynaty do ruszenia agenta a potem usuwa je z listy +#Wyjmuje pierwsze koordynaty do ruszenia agenta a potem usuwa go z listy def pop_first_coordinates(coordinates): if coordinates: x, y = coordinates.pop(0) @@ -118,6 +114,25 @@ def pop_first_coordinates(coordinates): agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells) + + +#Funkcja pomocnicza dla watku bo chcemy zeby agent poruszal sie ale zeby to normalnie wygladalo +def sciezkaAgenta(): + x,y =pop_first_coordinates(path) + if x is not None and y is not None: + agent.moveto(x,y) + + +#osobny watek dla sciezki agenta zeby co *iles czasu* poruszal sie tam gdzie mowi path +def watekDlaSciezkiAgenta(): + while True: + sciezkaAgenta() + time.sleep(1) + +watek = threading.Thread(target=watekDlaSciezkiAgenta) +watek.daemon = True +watek.start() + running = True while running: for event in pygame.event.get(): @@ -144,17 +159,11 @@ while running: - if not agent.moved: - first_x, first_y = pop_first_coordinates(path) - if first_x is not None and first_y is not None: - agent.moveto(first_x,first_y) - - - window.fill((255, 0, 0)) draw_grid(window, cells, agent) agent.update(window) pygame.display.update() + time.sleep(0.1) pygame.quit() diff --git a/classes/agent.py b/classes/agent.py index 68538cc..65d1c7a 100644 --- a/classes/agent.py +++ b/classes/agent.py @@ -65,7 +65,7 @@ class Agent: self.multiplier = 1 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: + if 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()