2023-03-16 23:53:18 +01:00
|
|
|
import pygame
|
2023-05-26 19:08:22 +02:00
|
|
|
from treelearn import treelearn
|
2023-05-26 10:52:02 +02:00
|
|
|
|
|
|
|
|
2023-05-04 20:44:35 +02:00
|
|
|
from astar import astar
|
2023-04-19 23:51:24 +02:00
|
|
|
from state import State
|
|
|
|
import time
|
2023-04-21 14:31:17 +02:00
|
|
|
from garbage_truck import GarbageTruck
|
2023-05-04 20:44:35 +02:00
|
|
|
from heuristicfn import heuristicfn
|
2023-05-26 19:08:22 +02:00
|
|
|
from map import randomize_map
|
2023-03-16 23:53:18 +01:00
|
|
|
|
|
|
|
pygame.init()
|
|
|
|
WIDTH, HEIGHT = 800, 800
|
|
|
|
window = pygame.display.set_mode((WIDTH, HEIGHT))
|
|
|
|
pygame.display.set_caption("Intelligent Garbage Collector")
|
|
|
|
AGENT_IMG = pygame.image.load("garbage-truck-nbg.png")
|
|
|
|
AGENT = pygame.transform.scale(AGENT_IMG, (50, 50))
|
|
|
|
FPS = 10
|
2023-04-21 14:31:17 +02:00
|
|
|
FIELDCOUNT = 16
|
|
|
|
FIELDWIDTH = 50
|
2023-03-16 23:53:18 +01:00
|
|
|
|
2023-05-26 19:08:22 +02:00
|
|
|
GRASS_IMG = pygame.image.load("grass.png")
|
|
|
|
GRASS = pygame.transform.scale(GRASS_IMG, (50, 50))
|
2023-05-05 12:20:29 +02:00
|
|
|
def draw_window(agent, fields, flip):
|
|
|
|
if flip:
|
|
|
|
direction = pygame.transform.flip(AGENT, True, False)
|
|
|
|
else:
|
|
|
|
direction = pygame.transform.flip(AGENT, False, False)
|
2023-03-16 23:53:18 +01:00
|
|
|
for i in range(16):
|
|
|
|
for j in range(16):
|
2023-05-04 18:21:09 +02:00
|
|
|
window.blit(fields[i][j], (i * 50, j * 50))
|
2023-05-05 12:20:29 +02:00
|
|
|
window.blit(direction, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta
|
2023-03-16 23:53:18 +01:00
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-05-26 19:08:22 +02:00
|
|
|
clf = treelearn()
|
2023-03-16 23:53:18 +01:00
|
|
|
clock = pygame.time.Clock()
|
|
|
|
run = True
|
2023-05-26 19:08:22 +02:00
|
|
|
fields, priority_array, request_list = randomize_map()
|
|
|
|
agent = GarbageTruck(0, 0, pygame.Rect(0, 0, 50, 50), 0, request_list, clf) # tworzenie pola dla agenta
|
2023-03-16 23:53:18 +01:00
|
|
|
while run:
|
|
|
|
clock.tick(FPS)
|
2023-05-04 18:21:09 +02:00
|
|
|
for event in pygame.event.get():
|
2023-03-16 23:53:18 +01:00
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
run = False
|
2023-05-05 12:20:29 +02:00
|
|
|
draw_window(agent, fields, False) # false = kierunek east (domyslny), true = west
|
2023-05-26 19:08:22 +02:00
|
|
|
x, y = agent.next_destination()
|
|
|
|
if x == agent.rect.x and y == agent.rect.y:
|
|
|
|
print('out of jobs')
|
|
|
|
break
|
2023-05-26 20:14:13 +02:00
|
|
|
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)
|
2023-04-19 23:51:24 +02:00
|
|
|
for interm in steps:
|
|
|
|
if interm.action == 'LEFT':
|
2023-04-21 14:31:17 +02:00
|
|
|
agent.turn_left()
|
2023-05-05 12:20:29 +02:00
|
|
|
draw_window(agent, fields, True)
|
2023-05-03 12:07:13 +02:00
|
|
|
elif interm.action == 'RIGHT':
|
2023-04-21 14:31:17 +02:00
|
|
|
agent.turn_right()
|
2023-05-05 12:20:29 +02:00
|
|
|
draw_window(agent, fields, False)
|
2023-05-03 12:07:13 +02:00
|
|
|
elif interm.action == 'FORWARD':
|
2023-04-21 14:31:17 +02:00
|
|
|
agent.forward()
|
2023-05-05 12:20:29 +02:00
|
|
|
if agent.orientation == 0:
|
|
|
|
draw_window(agent, fields, False)
|
|
|
|
elif agent.orientation == 2:
|
|
|
|
draw_window(agent, fields, True)
|
|
|
|
else:
|
|
|
|
draw_window(agent, fields, False)
|
2023-05-26 19:08:22 +02:00
|
|
|
time.sleep(0.3)
|
|
|
|
agent.collect()
|
|
|
|
fields[agent.rect.x//50][agent.rect.y//50] = GRASS
|
2023-05-26 20:14:13 +02:00
|
|
|
priority_array[agent.rect.x//50][agent.rect.y//50] = 1
|
2023-05-26 19:08:22 +02:00
|
|
|
time.sleep(0.5)
|
2023-04-19 23:51:24 +02:00
|
|
|
|
2023-03-16 23:53:18 +01:00
|
|
|
|
|
|
|
pygame.quit()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|