fixed agent rotation

This commit is contained in:
Tomasz Torchalski 2023-06-16 01:07:04 +02:00 committed by Mateusz Szlachetka
parent 1c26edad6c
commit 2318e6ba50

40
main.py
View File

@ -23,11 +23,15 @@ FIELDWIDTH = 50
BASE_IMG = pygame.image.load("Tiles/Base.jpg") BASE_IMG = pygame.image.load("Tiles/Base.jpg")
BASE = pygame.transform.scale(BASE_IMG, (50, 50)) BASE = pygame.transform.scale(BASE_IMG, (50, 50))
def draw_window(agent, fields, flip): def draw_window(agent, fields, flip, turn):
if flip: if flip:
direction = pygame.transform.flip(AGENT, True, False) direction = pygame.transform.flip(AGENT, True, False)
if turn:
direction = pygame.transform.rotate(AGENT, -90)
else: else:
direction = pygame.transform.flip(AGENT, False, False) direction = pygame.transform.flip(AGENT, False, False)
if turn:
direction = pygame.transform.rotate(AGENT, 90)
for i in range(16): for i in range(16):
for j in range(16): for j in range(16):
window.blit(fields[i][j], (i * 50, j * 50)) window.blit(fields[i][j], (i * 50, j * 50))
@ -49,36 +53,54 @@ def main():
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
run = False run = False
draw_window(agent, fields, False) # false = kierunek east (domyslny), true = west draw_window(agent, fields, False, False) # false = kierunek east (domyslny), true = west
x, y = agent.next_destination() x, y = agent.next_destination()
if x == agent.rect.x and y == agent.rect.y: if x == agent.rect.x and y == agent.rect.y:
print('out of jobs') print('out of jobs')
break break
if low_space == 1: if low_space == 1:
x, y = 0, 0 x, y = 0, 0
steps = astar(State(None, None, agent.rect.x, agent.rect.y, agent.orientation, priority_array[agent.rect.x//50][agent.rect.y//50], heuristicfn(agent.rect.x, agent.rect.y, x, y)), x, y, priority_array) steps = astar(State(None, None, agent.rect.x, agent.rect.y, agent.orientation,
priority_array[agent.rect.x//50][agent.rect.y//50],
heuristicfn(agent.rect.x, agent.rect.y, x, y)), x, y, priority_array)
for interm in steps: for interm in steps:
if interm.action == 'LEFT': if interm.action == 'LEFT':
agent.turn_left() agent.turn_left()
draw_window(agent, fields, True) if agent.orientation == 0:
draw_window(agent, fields, False, False)
elif agent.orientation == 2:
draw_window(agent, fields, True, False)
elif agent.orientation == 1:
draw_window(agent, fields, True, True)
else:
draw_window(agent, fields, False, True)
elif interm.action == 'RIGHT': elif interm.action == 'RIGHT':
agent.turn_right() agent.turn_right()
draw_window(agent, fields, False) if agent.orientation == 0:
draw_window(agent, fields, False, False)
elif agent.orientation == 2:
draw_window(agent, fields, True, False)
elif agent.orientation == 1:
draw_window(agent, fields, True, True)
else:
draw_window(agent, fields, False, True)
elif interm.action == 'FORWARD': elif interm.action == 'FORWARD':
agent.forward() agent.forward()
if agent.orientation == 0: if agent.orientation == 0:
draw_window(agent, fields, False) draw_window(agent, fields, False, False)
elif agent.orientation == 2: elif agent.orientation == 2:
draw_window(agent, fields, True) draw_window(agent, fields, True, False)
elif agent.orientation == 1:
draw_window(agent, fields, True, True)
else: else:
draw_window(agent, fields, False) draw_window(agent, fields, False, True)
time.sleep(0.3) time.sleep(0.3)
if (agent.rect.x // 50 != 0) or (agent.rect.y // 50 != 0): if (agent.rect.x // 50 != 0) or (agent.rect.y // 50 != 0):
garbage_type = loadmodel.classify(imgpath_array[agent.rect.x // 50][agent.rect.y // 50]) garbage_type = loadmodel.classify(imgpath_array[agent.rect.x // 50][agent.rect.y // 50])
low_space = agent.collect(garbage_type) low_space = agent.collect(garbage_type)
fields[agent.rect.x//50][agent.rect.y//50] = BASE fields[agent.rect.x//50][agent.rect.y//50] = BASE
priority_array[agent.rect.x//50][agent.rect.y//50] = 1 priority_array[agent.rect.x//50][agent.rect.y//50] = 100
time.sleep(0.5) time.sleep(0.5)
pygame.quit() pygame.quit()