Inteligentna_smieciarka/main.py

94 lines
2.8 KiB
Python
Raw Normal View History

import pygame
import random
2023-04-19 23:51:24 +02:00
from bfs import bfs
from state import State
import time
2023-04-21 14:31:17 +02:00
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
2023-04-21 14:31:17 +02:00
FIELDCOUNT = 16
FIELDWIDTH = 50
2023-04-19 23:51:24 +02:00
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))
2023-04-19 23:51:24 +02:00
window.blit(AGENT, (agent.rect.x, agent.rect.y)) # wyswietlanie agenta
pygame.display.update()
2023-04-19 23:51:24 +02:00
#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
2023-04-21 14:31:17 +02:00
agent = GarbageTruck(0, 0, 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
2023-04-19 23:51:24 +02:00
#keys_pressed = pygame.key.get_pressed()
2023-04-19 23:53:36 +02:00
draw_window(agent, fields)
2023-04-21 14:31:17 +02:00
steps = bfs(State(None, None, 0, 0, 'E'), 450, 600)
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-04-19 23:51:24 +02:00
if interm.action == 'RIGHT':
2023-04-21 14:31:17 +02:00
agent.turn_right()
2023-04-19 23:51:24 +02:00
if interm.action == 'FORWARD':
2023-04-21 14:31:17 +02:00
agent.forward()
2023-04-19 23:51:24 +02:00
draw_window(agent, fields)
2023-04-21 14:31:17 +02:00
time.sleep(0.5)
2023-04-19 23:51:24 +02:00
while True:
pass
pygame.quit()
2023-04-19 23:51:24 +02:00
if __name__ == "__main__":
main()