98 lines
3.0 KiB
Python
98 lines
3.0 KiB
Python
import pygame
|
|
import random
|
|
from bfs import bfs
|
|
from state import State
|
|
import time
|
|
|
|
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))
|
|
DIRT_IMG = pygame.image.load("dirt.jpg")
|
|
DIRT = pygame.transform.scale(DIRT_IMG, (50, 50))
|
|
GRASS_IMG = pygame.image.load("grass.png")
|
|
GRASS = pygame.transform.scale(GRASS_IMG, (50, 50))
|
|
SAND_IMG = pygame.image.load("sand.jpeg")
|
|
SAND = pygame.transform.scale(SAND_IMG, (50, 50))
|
|
COBBLE_IMG = pygame.image.load("cobble.jpeg")
|
|
COBBLE = pygame.transform.scale(COBBLE_IMG, (50, 50))
|
|
FPS = 10
|
|
|
|
class Agent:
|
|
def __init__(self, rect, direction):
|
|
self.rect = rect
|
|
self.direction = direction
|
|
|
|
def randomize_map(): # tworzenie mapy z losowymi polami
|
|
fields_list = [DIRT, GRASS, SAND, COBBLE]
|
|
field_array_1 = []
|
|
field_array_2 = []
|
|
for i in range(16):
|
|
for j in range(16):
|
|
field_array_2.append(random.choice(fields_list))
|
|
field_array_1.append(field_array_2)
|
|
field_array_2 = []
|
|
return field_array_1
|
|
|
|
|
|
def draw_window(agent, fields):
|
|
for i in range(16):
|
|
for j in range(16):
|
|
window.blit(fields[i][j], (i * 50, j * 50))
|
|
window.blit(AGENT, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta
|
|
pygame.display.update()
|
|
|
|
|
|
#def agent_movement(keys_pressed, agent): # sterowanie
|
|
# if keys_pressed[pygame.K_LEFT] and agent.x > 0:
|
|
# agent.x -= 50
|
|
# if keys_pressed[pygame.K_RIGHT] and agent.x < 750:
|
|
# agent.x += 50
|
|
# if keys_pressed[pygame.K_UP] and agent.y > 0:
|
|
# agent.y -= 50
|
|
# if keys_pressed[pygame.K_DOWN] and agent.y < 750:
|
|
# agent.y += 50
|
|
|
|
|
|
def main():
|
|
clock = pygame.time.Clock()
|
|
run = True
|
|
agent = Agent(pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta
|
|
fields = randomize_map()
|
|
while run:
|
|
clock.tick(FPS)
|
|
for event in pygame.event.get(): # przechwycanie zamknięcia okna
|
|
if event.type == pygame.QUIT:
|
|
run = False
|
|
#keys_pressed = pygame.key.get_pressed()
|
|
draw_window(agent, fields)
|
|
steps = bfs(State(None, None, 0, 0, 'E'), 250, 700)
|
|
for interm in steps:
|
|
if interm.action == 'LEFT':
|
|
agent.direction = (agent.direction - 1) % 4
|
|
if interm.action == 'RIGHT':
|
|
agent.direction = (agent.direction + 1) % 4
|
|
if interm.action == 'FORWARD':
|
|
if agent.direction == 0:
|
|
agent.rect.x += 50
|
|
elif agent.direction == 1:
|
|
agent.rect.y += 50
|
|
elif agent.direction == 2:
|
|
agent.rect.x -= 50
|
|
else:
|
|
agent.rect.y -= 50
|
|
draw_window(agent, fields)
|
|
time.sleep(1)
|
|
|
|
while True:
|
|
pass
|
|
|
|
pygame.quit()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|