188 lines
6.9 KiB
Python
188 lines
6.9 KiB
Python
import pygame
|
|
agent = pygame.image.load("sprites/agent.png")
|
|
agentWithGarbage = pygame.image.load("sprites/agentWithGarbage.png")
|
|
grass = pygame.image.load("sprites/grass.png")
|
|
tree = pygame.image.load("sprites/tree.png") # cost 0
|
|
house = pygame.image.load("sprites/house.png") # cost 0
|
|
road = pygame.image.load("sprites/road.png") # cost 1
|
|
brick = pygame.image.load("sprites/brick.png") # cost 2
|
|
sand = pygame.image.load("sprites/sand.png") # cost 3
|
|
hole = pygame.image.load("sprites/hole.png") # cost 9
|
|
speed_bump = pygame.image.load("sprites/speedbump.png") # cost 8
|
|
glass_bin = pygame.image.load("sprites/szklo.png")
|
|
paper_bin = pygame.image.load("sprites/papier.png")
|
|
plastic_bin = pygame.image.load("sprites/plastik.png")
|
|
other_bin = pygame.image.load("sprites/zmieszane.png")
|
|
a=0
|
|
b=1
|
|
|
|
def set_world(path):
|
|
array = []
|
|
with open(path) as f:
|
|
content = f.read().splitlines()
|
|
for line in content:
|
|
array.append(line)
|
|
return array
|
|
|
|
|
|
class WorldCell(pygame.Rect):
|
|
def __init__(self, x, y, rect_size, terrain, img):
|
|
self.rect_size = rect_size
|
|
self.terrain = terrain
|
|
self.img = pygame.transform.scale(img, (40, 40))
|
|
super(WorldCell, self).__init__(x, y, self.rect_size, self.rect_size)
|
|
|
|
def draw(self, window):
|
|
pygame.draw.rect(window, (0, 0, 0), self)
|
|
window.blit(self.img, self)
|
|
|
|
def get_terrain(self) -> int:
|
|
return self.terrain
|
|
|
|
|
|
class World:
|
|
def __init__(self, world_map, cell_size):
|
|
self.cell_size = cell_size
|
|
self.y_size = len(world_map)
|
|
self.x_size = len(world_map[0])
|
|
self.world_map = world_map
|
|
self.world_cells = [[0 for x in range(self.x_size)] for y in range(self.y_size)]
|
|
|
|
def draw(self, window):
|
|
for i in range(self.y_size):
|
|
for j in range(self.x_size):
|
|
img = ''
|
|
terrain = int(self.world_map[i][j]) # zczytywanie z planszy do terrain
|
|
if terrain == 0:
|
|
if ((i * j) + j + i) % 2 == 0:
|
|
img = pygame.transform.scale(tree, (40, 40))
|
|
else:
|
|
img = pygame.transform.scale(house, (40, 40))
|
|
if terrain == 1:
|
|
img = pygame.transform.scale(road, (40, 40))
|
|
if terrain == 2:
|
|
img = pygame.transform.scale(brick, (40, 40))
|
|
if terrain == 3:
|
|
img = pygame.transform.scale(sand, (40, 40))
|
|
if terrain == 4:
|
|
img = pygame.transform.scale(glass_bin, (40, 40))
|
|
if terrain == 5:
|
|
img = pygame.transform.scale(paper_bin, (40, 40))
|
|
if terrain == 6:
|
|
img = pygame.transform.scale(plastic_bin, (40, 40))
|
|
if terrain == 7:
|
|
img = pygame.transform.scale(other_bin, (40, 40))
|
|
if terrain == 9:
|
|
img = pygame.transform.scale(hole, (40, 40))
|
|
if terrain == 8:
|
|
img = pygame.transform.scale(speed_bump, (40, 40))
|
|
|
|
self.world_cells[i][j] = WorldCell(j * self.cell_size, i * self.cell_size, self.cell_size, terrain, img)
|
|
self.world_cells[i][j].draw(window)
|
|
|
|
def get_world_cell_size(self):
|
|
return self.cell_size
|
|
|
|
|
|
|
|
|
|
class Agent(pygame.sprite.Sprite):
|
|
def __init__(self, size, world):
|
|
pygame.sprite.Sprite.__init__(self)
|
|
self.image = pygame.transform.scale(pygame.image.load("sprites/agent.png"), (size, size))
|
|
self.rect = self.image.get_rect()
|
|
self.rect.center = (60, 20) # agent position on start
|
|
self.size = size
|
|
self.directions = ["E", "S", "W", "N"]
|
|
self.world = world
|
|
self.a = 1
|
|
self.b = 0
|
|
|
|
def change_image(self):
|
|
self.image = pygame.transform.scale(pygame.image.load("sprites/agentWithGarbage.png"), (40, 40))
|
|
def change_image2(self):
|
|
self.image = pygame.transform.scale(pygame.image.load("sprites/agent.png"), (40, 40))
|
|
def get_directions(self):
|
|
return self.directions
|
|
|
|
def check_collisions(self, x, y):
|
|
x_parsed = int((x - 20) / self.world.cell_size)
|
|
y_parsed = int((y - 20) / self.world.cell_size)
|
|
if x_parsed < 0:
|
|
return False
|
|
if y_parsed < 0:
|
|
return False
|
|
if x_parsed >= self.world.x_size:
|
|
return False
|
|
if y_parsed >= self.world.y_size:
|
|
return False
|
|
if not self.world.world_cells[y_parsed][x_parsed].terrain:
|
|
return False
|
|
return True
|
|
|
|
def move(self):
|
|
if self.directions[0] == "N":
|
|
x = self.rect.center[0]
|
|
y = self.rect.center[1] - self.size
|
|
if self.check_collisions(x, y):
|
|
self.rect.center = [x, y]
|
|
self.b = self.b - 1
|
|
if self.directions[0] == "S":
|
|
x = self.rect.center[0]
|
|
y = self.rect.center[1] + self.size
|
|
if self.check_collisions(x, y):
|
|
self.rect.center = [x, y]
|
|
self.b = self.b + 1
|
|
if self.directions[0] == "W":
|
|
x = self.rect.center[0] - self.size
|
|
y = self.rect.center[1]
|
|
if self.check_collisions(x, y):
|
|
self.rect.center = [x, y]
|
|
self.a = self.a- 1
|
|
if self.directions[0] == "E":
|
|
x = self.rect.center[0] + self.size
|
|
y = self.rect.center[1]
|
|
if self.check_collisions(x, y):
|
|
self.rect.center = [x, y]
|
|
self.a = self.a + 1
|
|
|
|
def get_next_move(self):
|
|
if self.directions[0] == "N":
|
|
x = self.rect.center[0]
|
|
y = self.rect.center[1] - self.size
|
|
self.a = self.a - 1
|
|
if self.check_collisions(x, y):
|
|
return x, y
|
|
if self.directions[0] == "S":
|
|
x = self.rect.center[0]
|
|
y = self.rect.center[1] + self.size
|
|
if self.check_collisions(x, y):
|
|
return x, y
|
|
if self.directions[0] == "W":
|
|
x = self.rect.center[0] - self.size
|
|
y = self.rect.center[1]
|
|
if self.check_collisions(x, y):
|
|
return x, y
|
|
if self.directions[0] == "E":
|
|
x = self.rect.center[0] + self.size
|
|
y = self.rect.center[1]
|
|
if self.check_collisions(x, y):
|
|
return x, y
|
|
|
|
def get_first(self):
|
|
return self.a
|
|
def get_second(self):
|
|
return self.b
|
|
|
|
|
|
def turn_right(self):
|
|
self.image = pygame.transform.rotate(self.image, -90)
|
|
self.directions = self.directions[1:] + [self.directions[0]]
|
|
|
|
def turn_left(self):
|
|
self.image = pygame.transform.rotate(self.image, 90)
|
|
self.directions = [self.directions[3]] + self.directions[:3]
|
|
|
|
|
|
|