Wyświetlanie domów i wysypiska
This commit is contained in:
parent
d7edccf091
commit
95a3ac8755
88
board
88
board
@ -4,11 +4,54 @@ import pygame
|
|||||||
screen = []
|
screen = []
|
||||||
objectArray = []
|
objectArray = []
|
||||||
|
|
||||||
class Agent:
|
|
||||||
def __init__(self, name, xPos, yPos):
|
class Position:
|
||||||
|
def __init__(self, x, y):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
|
||||||
|
class Object:
|
||||||
|
def __init__(self, name, pos):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.xPos = xPos
|
self.pos = pos
|
||||||
self.yPos = yPos
|
|
||||||
|
def draw(self, square):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Agent(Object):
|
||||||
|
def __init__(self, name, pos):
|
||||||
|
super().__init__(name, pos)
|
||||||
|
|
||||||
|
def draw(self, square):
|
||||||
|
## RYSUJEMY AGENTA
|
||||||
|
# agent_color = (255, 0, 0)
|
||||||
|
circleX = self.pos.x * square + square + square / 2 # dodane jedno +square, by śmieciara nie wychodziła poza kratę
|
||||||
|
circleY = self.pos.y * square - square / 2
|
||||||
|
# radius = 10
|
||||||
|
# pygame.draw.circle(screen, agent_color, (a + circleX, b + circleY), radius)
|
||||||
|
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))
|
||||||
|
|
||||||
|
|
||||||
|
class House(Object):
|
||||||
|
def __init__(self, name, pos):
|
||||||
|
super().__init__(name, pos)
|
||||||
|
|
||||||
|
def draw(self, square):
|
||||||
|
leftTopX, leftTopY = 50 + self.pos.x * square, 10 + self.pos.y * square
|
||||||
|
pygame.draw.rect(screen, (0, 255, 0), (leftTopX, leftTopY, square, square))
|
||||||
|
|
||||||
|
|
||||||
|
class Junkyard(Object):
|
||||||
|
def __init__(self, name, pos):
|
||||||
|
super().__init__(name, pos)
|
||||||
|
|
||||||
|
def draw(self, square):
|
||||||
|
leftTopX, leftTopY = 50 + self.pos.x * square, 10 + self.pos.y * square
|
||||||
|
pygame.draw.rect(screen, (255, 0, 0), (leftTopX, leftTopY, square, square))
|
||||||
|
|
||||||
def draw(square_num, objectArr):
|
def draw(square_num, objectArr):
|
||||||
# następne dwie linijki do odkomentowania, jak będzie wgrane zdjęcie do tła
|
# następne dwie linijki do odkomentowania, jak będzie wgrane zdjęcie do tła
|
||||||
@ -22,6 +65,9 @@ def draw(square_num, objectArr):
|
|||||||
a = 50
|
a = 50
|
||||||
b = 10 # odległości kraty od krawędzi okna
|
b = 10 # odległości kraty od krawędzi okna
|
||||||
|
|
||||||
|
for o in objectArr:
|
||||||
|
o.draw(square)
|
||||||
|
|
||||||
for i in range(square_num):
|
for i in range(square_num):
|
||||||
pygame.draw.line(screen, grid_color, (a + i * square, b), (a + i * square, b + grid_size), 2)
|
pygame.draw.line(screen, grid_color, (a + i * square, b), (a + i * square, b + grid_size), 2)
|
||||||
|
|
||||||
@ -34,27 +80,18 @@ def draw(square_num, objectArr):
|
|||||||
|
|
||||||
## tutaj rysujemy agenta i inne obiekty juz na gotowej mapie
|
## tutaj rysujemy agenta i inne obiekty juz na gotowej mapie
|
||||||
|
|
||||||
## RYSUJEMY AGENTA
|
|
||||||
#agent_color = (255, 0, 0)
|
|
||||||
circleX = objectArr[0].xPos * square + square + square/ 2 #dodane jedno +square, by śmieciara nie wychodziła poza kratę
|
|
||||||
circleY = objectArr[0].yPos * square - square / 2
|
|
||||||
#radius = 10
|
|
||||||
#pygame.draw.circle(screen, agent_color, (a + circleX, b + circleY), radius)
|
|
||||||
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))
|
|
||||||
|
|
||||||
def kb_listen(objectArray, gridLength):
|
def kb_listen(objectArray, gridLength):
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_LEFT and objectArray[0].xPos > 0:
|
if event.key == pygame.K_LEFT and objectArray[0].pos.x > 0:
|
||||||
objectArray[0].xPos = objectArray[0].xPos - 1
|
objectArray[0].pos.x = objectArray[0].pos.x - 1
|
||||||
if event.key == pygame.K_RIGHT and objectArray[0].xPos < gridLength - 1:
|
if event.key == pygame.K_RIGHT and objectArray[0].pos.x < gridLength - 1:
|
||||||
objectArray[0].xPos = objectArray[0].xPos + 1
|
objectArray[0].pos.x = objectArray[0].pos.x + 1
|
||||||
if event.key == pygame.K_UP and objectArray[0].yPos > 1:
|
if event.key == pygame.K_UP and objectArray[0].pos.y > 1:
|
||||||
objectArray[0].yPos = objectArray[0].yPos - 1
|
objectArray[0].pos.y = objectArray[0].pos.y - 1
|
||||||
if event.key == pygame.K_DOWN and objectArray[0].yPos < gridLength:
|
if event.key == pygame.K_DOWN and objectArray[0].pos.y < gridLength:
|
||||||
objectArray[0].yPos = objectArray[0].yPos + 1
|
objectArray[0].pos.y = objectArray[0].pos.y + 1
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
@ -63,8 +100,15 @@ if __name__ == '__main__':
|
|||||||
pygame.init() # inicjalizacja modułów, na razie niepotrzebna
|
pygame.init() # inicjalizacja modułów, na razie niepotrzebna
|
||||||
|
|
||||||
# Tworzymy nowego playera, czy tam agenta
|
# Tworzymy nowego playera, czy tam agenta
|
||||||
agent = Agent("smieciarka", 5, 7)
|
agent = Agent("smieciarka", Position(5, 7))
|
||||||
|
junkyard = Junkyard("wysypisko", Position(10, 10))
|
||||||
|
housePositions = [Position(x, y) for x, y in [
|
||||||
|
(7, 4), (3, 10), (8, 10), (4, 5), (1, 2), (10, 4), (14, 14), (6, 9)
|
||||||
|
]]
|
||||||
|
houses = [House(f'dom-{i}', pos) for i, pos in enumerate(housePositions)]
|
||||||
objectArray.append(agent)
|
objectArray.append(agent)
|
||||||
|
objectArray.append(junkyard)
|
||||||
|
objectArray += houses
|
||||||
|
|
||||||
width = 600
|
width = 600
|
||||||
height = 530
|
height = 530
|
||||||
|
Loading…
Reference in New Issue
Block a user