2022-03-08 11:25:50 +01:00
|
|
|
import sys
|
|
|
|
import pygame
|
|
|
|
|
|
|
|
screen = []
|
|
|
|
objectArray = []
|
2022-03-24 22:53:16 +01:00
|
|
|
collisionsMap = []
|
2022-03-08 11:25:50 +01:00
|
|
|
|
2022-03-20 22:33:20 +01:00
|
|
|
|
|
|
|
class Position:
|
|
|
|
def __init__(self, x, y):
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
2022-03-24 22:53:16 +01:00
|
|
|
def get_moved(self, delta_x, delta_y):
|
|
|
|
return Position(self.x + delta_x, self.y + delta_y)
|
|
|
|
|
2022-03-20 22:33:20 +01:00
|
|
|
|
|
|
|
class Object:
|
|
|
|
def __init__(self, name, pos):
|
2022-03-08 11:25:50 +01:00
|
|
|
self.name = name
|
2022-03-20 22:33:20 +01:00
|
|
|
self.pos = pos
|
|
|
|
|
|
|
|
def draw(self, square):
|
2022-03-24 22:53:16 +01:00
|
|
|
leftTopX, leftTopY = 50 + self.pos.x * square, 10 + self.pos.y * square
|
|
|
|
pygame.draw.rect(screen, (0, 0, 0), (leftTopX, leftTopY, square, square))
|
|
|
|
|
|
|
|
|
|
|
|
def detect_collision(newPos):
|
|
|
|
return collisionsMap[newPos.x][newPos.y]
|
|
|
|
|
|
|
|
|
|
|
|
def position_in_grid(pos, gridLength):
|
|
|
|
return 0 <= pos.x < gridLength and 0 <= pos.y < gridLength
|
|
|
|
|
|
|
|
|
|
|
|
def movement_allowed(newPos, gridLength):
|
|
|
|
return position_in_grid(newPos, gridLength) and not detect_collision(newPos)
|
2022-03-20 22:33:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Agent(Object):
|
|
|
|
def __init__(self, name, pos):
|
|
|
|
super().__init__(name, pos)
|
|
|
|
|
|
|
|
def draw(self, square):
|
|
|
|
## RYSUJEMY AGENTA
|
2022-03-24 22:53:16 +01:00
|
|
|
circleX = 52 + self.pos.x * square
|
|
|
|
circleY = 12 + self.pos.y * square
|
|
|
|
|
2022-03-20 22:33:20 +01:00
|
|
|
truck = pygame.image.load("car.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
|
|
|
truck = pygame.transform.scale(truck, (square, square))
|
|
|
|
screen.blit(truck, (circleX, circleY))
|
|
|
|
|
2022-03-24 22:53:16 +01:00
|
|
|
def move(self, event, gridLength):
|
|
|
|
if event.key == pygame.K_LEFT:
|
|
|
|
newPos = self.pos.get_moved(-1, 0)
|
|
|
|
self.move_if_possible(newPos, gridLength)
|
|
|
|
if event.key == pygame.K_RIGHT:
|
|
|
|
newPos = self.pos.get_moved(1, 0)
|
|
|
|
self.move_if_possible(newPos, gridLength)
|
|
|
|
if event.key == pygame.K_UP:
|
|
|
|
newPos = self.pos.get_moved(0, -1)
|
|
|
|
self.move_if_possible(newPos, gridLength)
|
|
|
|
if event.key == pygame.K_DOWN:
|
|
|
|
newPos = self.pos.get_moved(0, 1)
|
|
|
|
self.move_if_possible(newPos, gridLength)
|
|
|
|
|
|
|
|
def move_if_possible(self, newPos, gridLength):
|
|
|
|
if movement_allowed(newPos, gridLength):
|
|
|
|
self.pos = newPos
|
|
|
|
|
2022-03-20 22:33:20 +01:00
|
|
|
|
|
|
|
class House(Object):
|
|
|
|
def __init__(self, name, pos):
|
|
|
|
super().__init__(name, pos)
|
2022-03-24 22:53:16 +01:00
|
|
|
self.trash_cans = {
|
|
|
|
"paper": False,
|
|
|
|
"glass": False,
|
|
|
|
"plastic": False,
|
|
|
|
"bio": False
|
|
|
|
}
|
2022-03-20 22:33:20 +01:00
|
|
|
|
|
|
|
def draw(self, square):
|
2022-03-24 22:53:16 +01:00
|
|
|
x = 52 + self.pos.x * square
|
|
|
|
y = 12 + self.pos.y * square
|
|
|
|
|
|
|
|
house = pygame.image.load("house.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
|
|
|
house = pygame.transform.scale(house, (square, square))
|
|
|
|
screen.blit(house, (x, y))
|
2022-03-20 22:33:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Junkyard(Object):
|
|
|
|
def __init__(self, name, pos):
|
|
|
|
super().__init__(name, pos)
|
2022-03-24 22:53:16 +01:00
|
|
|
self.heaps = {
|
|
|
|
"paper": True,
|
|
|
|
"glass": True,
|
|
|
|
"plastic": True,
|
|
|
|
"bio": True
|
|
|
|
}
|
2022-03-20 22:33:20 +01:00
|
|
|
|
|
|
|
def draw(self, square):
|
2022-03-24 22:53:16 +01:00
|
|
|
x = 52 + self.pos.x * square
|
|
|
|
y = 12 + self.pos.y * square
|
|
|
|
|
|
|
|
junkyard = pygame.image.load("junkyard.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
|
|
|
junkyard = pygame.transform.scale(junkyard, (square, square))
|
|
|
|
screen.blit(junkyard, (x, y))
|
|
|
|
|
|
|
|
|
|
|
|
class Hole(Object):
|
|
|
|
def __init__(self, name, pos):
|
|
|
|
super().__init__(name, pos)
|
|
|
|
|
|
|
|
def draw(self, square):
|
|
|
|
x = 52 + self.pos.x * square
|
|
|
|
y = 12 + self.pos.y * square
|
|
|
|
|
|
|
|
hole = pygame.image.load("hole.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
|
|
|
hole = pygame.transform.scale(hole, (square, square))
|
|
|
|
screen.blit(hole, (x, y))
|
|
|
|
|
2022-03-08 11:25:50 +01:00
|
|
|
|
|
|
|
def draw(square_num, objectArr):
|
2022-03-20 22:33:20 +01:00
|
|
|
# następne dwie linijki do odkomentowania, jak będzie wgrane zdjęcie do tła
|
|
|
|
# background = pygame.image.load("ścieżka do pliku").convert() #tu ścieżka do zdjęcia w tle
|
|
|
|
# screen.blit(background, (0, 0))
|
|
|
|
grid_color = (0, 0, 0) # kolor czarny
|
2022-03-08 11:25:50 +01:00
|
|
|
|
2022-03-24 22:53:16 +01:00
|
|
|
grid_size = 510 # rozmiar kraty
|
|
|
|
square = grid_size // square_num # rozmiar pojedyńczego kwadracika
|
2022-03-08 11:25:50 +01:00
|
|
|
|
|
|
|
a = 50
|
2022-03-20 22:33:20 +01:00
|
|
|
b = 10 # odległości kraty od krawędzi okna
|
|
|
|
|
|
|
|
for o in objectArr:
|
|
|
|
o.draw(square)
|
2022-03-08 11:25:50 +01:00
|
|
|
|
|
|
|
for i in range(square_num):
|
2022-03-20 22:33:20 +01:00
|
|
|
pygame.draw.line(screen, grid_color, (a + i * square, b), (a + i * square, b + grid_size), 2)
|
2022-03-08 11:25:50 +01:00
|
|
|
|
2022-03-20 22:33:20 +01:00
|
|
|
pygame.draw.line(screen, grid_color, (a, b + i * square), (a + grid_size, b + i * square), 2)
|
2022-03-08 11:25:50 +01:00
|
|
|
|
|
|
|
pygame.draw.line(screen, grid_color, (a, b + grid_size), (a + grid_size,
|
2022-03-20 22:33:20 +01:00
|
|
|
b + grid_size), 2)
|
2022-03-08 11:25:50 +01:00
|
|
|
pygame.draw.line(screen, grid_color, (a + grid_size, b),
|
|
|
|
(a + grid_size, b + grid_size), 2)
|
|
|
|
|
|
|
|
|
2022-03-08 14:30:25 +01:00
|
|
|
def kb_listen(objectArray, gridLength):
|
2022-03-24 22:53:16 +01:00
|
|
|
agent = objectArray[0]
|
2022-03-08 11:25:50 +01:00
|
|
|
for event in pygame.event.get():
|
2022-03-08 11:42:06 +01:00
|
|
|
if event.type == pygame.KEYDOWN:
|
2022-03-24 22:53:16 +01:00
|
|
|
agent.move(event, gridLength)
|
2022-03-08 11:25:50 +01:00
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-03-20 22:33:20 +01:00
|
|
|
pygame.init() # inicjalizacja modułów, na razie niepotrzebna
|
2022-03-24 22:53:16 +01:00
|
|
|
gridSize = 15
|
2022-03-20 22:33:20 +01:00
|
|
|
|
|
|
|
# Tworzymy nowego playera, czy tam agenta
|
2022-03-24 22:53:16 +01:00
|
|
|
agent = Agent("smieciarka", Position(0, 0))
|
2022-03-20 22:33:20 +01:00
|
|
|
junkyard = Junkyard("wysypisko", Position(10, 10))
|
2022-03-24 22:53:16 +01:00
|
|
|
houses = [House(f'dom-{i}', pos) for i, pos in enumerate([Position(x, y) for x, y in [
|
2022-03-20 22:33:20 +01:00
|
|
|
(7, 4), (3, 10), (8, 10), (4, 5), (1, 2), (10, 4), (14, 14), (6, 9)
|
2022-03-24 22:53:16 +01:00
|
|
|
]])]
|
|
|
|
holes = [Hole(f'dziura-{i}', pos) for i, pos in enumerate([Position(x, y) for x, y in [
|
|
|
|
(4, 9), (5, 11), (11, 7), (13, 8)
|
|
|
|
]])]
|
2022-03-08 11:25:50 +01:00
|
|
|
objectArray.append(agent)
|
2022-03-20 22:33:20 +01:00
|
|
|
objectArray.append(junkyard)
|
|
|
|
objectArray += houses
|
2022-03-24 22:53:16 +01:00
|
|
|
objectArray += holes
|
|
|
|
|
|
|
|
collisionsMap = [[False] * gridSize for _ in range(gridSize)]
|
|
|
|
for object in objectArray[1:]:
|
|
|
|
collisionsMap[object.pos.x][object.pos.y] = True
|
2022-03-08 11:25:50 +01:00
|
|
|
|
2022-03-24 22:53:16 +01:00
|
|
|
width = 610
|
2022-03-08 11:25:50 +01:00
|
|
|
height = 530
|
2022-03-20 22:33:20 +01:00
|
|
|
screen = pygame.display.set_mode((width, height)) # ustalanie rozmiarów okna
|
2022-03-08 11:25:50 +01:00
|
|
|
|
|
|
|
while 1:
|
2022-03-20 22:33:20 +01:00
|
|
|
c = (255, 255, 255) # tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie
|
2022-03-08 11:42:06 +01:00
|
|
|
screen.fill(c)
|
2022-03-24 22:53:16 +01:00
|
|
|
draw(gridSize, objectArray)
|
|
|
|
kb_listen(objectArray, gridSize)
|
2022-03-20 22:33:20 +01:00
|
|
|
pygame.display.update() # by krata pojawiła się w okienku - update powierzchni
|