added basic map with agent and randomized fields

This commit is contained in:
Tomasz Torchalski 2023-03-16 23:53:18 +01:00
commit 0a18d1a656
7 changed files with 73 additions and 0 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

BIN
cobble.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

BIN
dirt.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

BIN
garbage-truck-nbg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 KiB

BIN
grass.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

70
main.py Normal file
View File

@ -0,0 +1,70 @@
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()

BIN
sand.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB