feat: agent moves normally now, Added features to handle agent self-traffic and threads

Fix: agent move and errors
This commit is contained in:
MlodyJ 2024-04-13 11:53:13 +02:00
parent fee1eddb8a
commit 687a630576
2 changed files with 27 additions and 18 deletions

43
app.py
View File

@ -6,6 +6,8 @@ from pygame.locals import K_w, K_s, K_a, K_d
from classes.cell import Cell from classes.cell import Cell
from classes.agent import Agent from classes.agent import Agent
from collections import deque from collections import deque
import threading
import time
pygame.init() pygame.init()
window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT)) window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT))
pygame.display.set_caption("Game Window") pygame.display.set_caption("Game Window")
@ -45,17 +47,12 @@ def draw_grid(window, cells, agent):
window.blit(scoreText, (0, 0)) window.blit(scoreText, (0, 0))
window.blit(multiplierText, (0, 50)) window.blit(multiplierText, (0, 50))
initBoard() initBoard()
def bfs(start, target, cells): def bfs(start, target, cells):
queue = deque([(start,[])]) queue = deque([(start,[])])
visited = set() visited = set()
while queue: while queue:
current, path = queue.popleft() current, path = queue.popleft()
if current==target: if current==target:
@ -85,13 +82,12 @@ def get_neighbors(cell, cells):
return neighbors return neighbors
#Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe do funkcji znajdujacej najkrotsza sciezke
#Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe
start_cell = cells[5][5] start_cell = cells[5][5]
target_cell = cells[10][10] target_cell = cells[3][3]
shortest_path = bfs(start_cell, target_cell, cells) shortest_path = bfs(start_cell, target_cell, cells)
if shortest_path: if shortest_path:
print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path]) print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path])
else: else:
@ -106,7 +102,7 @@ def convert_to_coordinates(shortest_path):
path = 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): def pop_first_coordinates(coordinates):
if coordinates: if coordinates:
x, y = coordinates.pop(0) 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) 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 running = True
while running: while running:
for event in pygame.event.get(): 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)) window.fill((255, 0, 0))
draw_grid(window, cells, agent) draw_grid(window, cells, agent)
agent.update(window) agent.update(window)
pygame.display.update() pygame.display.update()
time.sleep(0.1)
pygame.quit() pygame.quit()

View File

@ -65,7 +65,7 @@ class Agent:
self.multiplier = 1 self.multiplier = 1
def moveto(self,x,y): 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.current_cell = self.cells[x][y]
self.moved=True self.moved=True
self.last_move_time=pygame.time.get_ticks() self.last_move_time=pygame.time.get_ticks()