71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
import pygame
|
|
import random
|
|
|
|
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
|
|
|
|
|
|
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.x, agent.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 = pygame.Rect(0, 0, 50, 50) # 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)
|
|
agent_movement(keys_pressed, agent)
|
|
|
|
pygame.quit()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|