100 lines
3.0 KiB
Python
100 lines
3.0 KiB
Python
import pygame
|
|
import random
|
|
from bfs import bfs
|
|
from state import State
|
|
import time
|
|
from garbage_truck import GarbageTruck
|
|
|
|
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
|
|
FIELDCOUNT = 16
|
|
FIELDWIDTH = 50
|
|
|
|
class Agent:
|
|
def __init__(self, rect, direction):
|
|
self.rect = rect
|
|
self.direction = direction
|
|
|
|
|
|
def randomize_map(): # tworzenie mapy z losowymi polami
|
|
field_array_1 = []
|
|
field_array_2 = []
|
|
field_priority = []
|
|
for i in range(16):
|
|
temp_priority = []
|
|
for j in range(16):
|
|
if i in (0, 1) and j in (0, 1):
|
|
field_array_2.append(GRASS)
|
|
temp_priority.append(1)
|
|
else:
|
|
prob = random.uniform(0, 100)
|
|
if 0 <= prob <= 10:
|
|
field_array_2.append(COBBLE)
|
|
temp_priority.append(3)
|
|
if 10 < prob <= 20:
|
|
field_array_2.append(SAND)
|
|
temp_priority.append(2)
|
|
else:
|
|
field_array_2.append(GRASS)
|
|
temp_priority.append(1)
|
|
field_array_1.append(field_array_2)
|
|
field_array_2 = []
|
|
field_priority.append(temp_priority)
|
|
return field_array_1, field_priority
|
|
|
|
|
|
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 main():
|
|
clock = pygame.time.Clock()
|
|
run = True
|
|
agent = GarbageTruck(0, 0, pygame.Rect(0, 0, 50, 50), 0) # tworzenie pola dla agenta
|
|
fields, priority_array = randomize_map()
|
|
print(priority_array)
|
|
print(priority_array[4][8])
|
|
while run:
|
|
clock.tick(FPS)
|
|
for event in pygame.event.get():
|
|
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'), 100, 50, fields)
|
|
for interm in steps:
|
|
if interm.action == 'LEFT':
|
|
agent.turn_left()
|
|
elif interm.action == 'RIGHT':
|
|
agent.turn_right()
|
|
elif interm.action == 'FORWARD':
|
|
agent.forward()
|
|
draw_window(agent, fields)
|
|
time.sleep(0.5)
|
|
|
|
while True:
|
|
pass
|
|
|
|
pygame.quit()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|