feat: agent moves normally now, Added features to handle agent self-traffic and threads
Fix: agent move and errors
This commit is contained in:
parent
fee1eddb8a
commit
687a630576
43
app.py
43
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()
|
||||
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user