obiekty+kolizja
This commit is contained in:
parent
95a3ac8755
commit
83d0918595
125
board
125
board
@ -3,6 +3,7 @@ import pygame
|
|||||||
|
|
||||||
screen = []
|
screen = []
|
||||||
objectArray = []
|
objectArray = []
|
||||||
|
collisionsMap = []
|
||||||
|
|
||||||
|
|
||||||
class Position:
|
class Position:
|
||||||
@ -10,6 +11,9 @@ class Position:
|
|||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
|
|
||||||
|
def get_moved(self, delta_x, delta_y):
|
||||||
|
return Position(self.x + delta_x, self.y + delta_y)
|
||||||
|
|
||||||
|
|
||||||
class Object:
|
class Object:
|
||||||
def __init__(self, name, pos):
|
def __init__(self, name, pos):
|
||||||
@ -17,7 +21,20 @@ class Object:
|
|||||||
self.pos = pos
|
self.pos = pos
|
||||||
|
|
||||||
def draw(self, square):
|
def draw(self, square):
|
||||||
pass
|
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)
|
||||||
|
|
||||||
|
|
||||||
class Agent(Object):
|
class Agent(Object):
|
||||||
@ -26,32 +43,82 @@ class Agent(Object):
|
|||||||
|
|
||||||
def draw(self, square):
|
def draw(self, square):
|
||||||
## RYSUJEMY AGENTA
|
## RYSUJEMY AGENTA
|
||||||
# agent_color = (255, 0, 0)
|
circleX = 52 + self.pos.x * square
|
||||||
circleX = self.pos.x * square + square + square / 2 # dodane jedno +square, by śmieciara nie wychodziła poza kratę
|
circleY = 12 + self.pos.y * square
|
||||||
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.image.load("car.png").convert_alpha() # tu ścieżka do zdjęcia w tle
|
||||||
truck = pygame.transform.scale(truck, (square, square))
|
truck = pygame.transform.scale(truck, (square, square))
|
||||||
screen.blit(truck, (circleX, circleY))
|
screen.blit(truck, (circleX, circleY))
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
class House(Object):
|
class House(Object):
|
||||||
def __init__(self, name, pos):
|
def __init__(self, name, pos):
|
||||||
super().__init__(name, pos)
|
super().__init__(name, pos)
|
||||||
|
self.trash_cans = {
|
||||||
|
"paper": False,
|
||||||
|
"glass": False,
|
||||||
|
"plastic": False,
|
||||||
|
"bio": False
|
||||||
|
}
|
||||||
|
|
||||||
def draw(self, square):
|
def draw(self, square):
|
||||||
leftTopX, leftTopY = 50 + self.pos.x * square, 10 + self.pos.y * square
|
x = 52 + self.pos.x * square
|
||||||
pygame.draw.rect(screen, (0, 255, 0), (leftTopX, leftTopY, square, 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))
|
||||||
|
|
||||||
|
|
||||||
class Junkyard(Object):
|
class Junkyard(Object):
|
||||||
def __init__(self, name, pos):
|
def __init__(self, name, pos):
|
||||||
super().__init__(name, pos)
|
super().__init__(name, pos)
|
||||||
|
self.heaps = {
|
||||||
|
"paper": True,
|
||||||
|
"glass": True,
|
||||||
|
"plastic": True,
|
||||||
|
"bio": True
|
||||||
|
}
|
||||||
|
|
||||||
def draw(self, square):
|
def draw(self, square):
|
||||||
leftTopX, leftTopY = 50 + self.pos.x * square, 10 + self.pos.y * square
|
x = 52 + self.pos.x * square
|
||||||
pygame.draw.rect(screen, (255, 0, 0), (leftTopX, leftTopY, square, 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))
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
@ -59,8 +126,8 @@ def draw(square_num, objectArr):
|
|||||||
# screen.blit(background, (0, 0))
|
# screen.blit(background, (0, 0))
|
||||||
grid_color = (0, 0, 0) # kolor czarny
|
grid_color = (0, 0, 0) # kolor czarny
|
||||||
|
|
||||||
grid_size = 500 # rozmiar kraty
|
grid_size = 510 # rozmiar kraty
|
||||||
square = grid_size / square_num # rozmiar pojedyńczego kwadracika
|
square = grid_size // square_num # rozmiar pojedyńczego kwadracika
|
||||||
|
|
||||||
a = 50
|
a = 50
|
||||||
b = 10 # odległości kraty od krawędzi okna
|
b = 10 # odległości kraty od krawędzi okna
|
||||||
@ -78,45 +145,45 @@ def draw(square_num, objectArr):
|
|||||||
pygame.draw.line(screen, grid_color, (a + grid_size, b),
|
pygame.draw.line(screen, grid_color, (a + grid_size, b),
|
||||||
(a + grid_size, b + grid_size), 2)
|
(a + grid_size, b + grid_size), 2)
|
||||||
|
|
||||||
## tutaj rysujemy agenta i inne obiekty juz na gotowej mapie
|
|
||||||
|
|
||||||
|
|
||||||
def kb_listen(objectArray, gridLength):
|
def kb_listen(objectArray, gridLength):
|
||||||
|
agent = objectArray[0]
|
||||||
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].pos.x > 0:
|
agent.move(event, gridLength)
|
||||||
objectArray[0].pos.x = objectArray[0].pos.x - 1
|
|
||||||
if event.key == pygame.K_RIGHT and objectArray[0].pos.x < gridLength - 1:
|
|
||||||
objectArray[0].pos.x = objectArray[0].pos.x + 1
|
|
||||||
if event.key == pygame.K_UP and objectArray[0].pos.y > 1:
|
|
||||||
objectArray[0].pos.y = objectArray[0].pos.y - 1
|
|
||||||
if event.key == pygame.K_DOWN and objectArray[0].pos.y < gridLength:
|
|
||||||
objectArray[0].pos.y = objectArray[0].pos.y + 1
|
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
pygame.init() # inicjalizacja modułów, na razie niepotrzebna
|
pygame.init() # inicjalizacja modułów, na razie niepotrzebna
|
||||||
|
gridSize = 15
|
||||||
|
|
||||||
# Tworzymy nowego playera, czy tam agenta
|
# Tworzymy nowego playera, czy tam agenta
|
||||||
agent = Agent("smieciarka", Position(5, 7))
|
agent = Agent("smieciarka", Position(0, 0))
|
||||||
junkyard = Junkyard("wysypisko", Position(10, 10))
|
junkyard = Junkyard("wysypisko", Position(10, 10))
|
||||||
housePositions = [Position(x, y) for x, y in [
|
houses = [House(f'dom-{i}', pos) for i, pos in enumerate([Position(x, y) for x, y in [
|
||||||
(7, 4), (3, 10), (8, 10), (4, 5), (1, 2), (10, 4), (14, 14), (6, 9)
|
(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)]
|
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)
|
||||||
|
]])]
|
||||||
objectArray.append(agent)
|
objectArray.append(agent)
|
||||||
objectArray.append(junkyard)
|
objectArray.append(junkyard)
|
||||||
objectArray += houses
|
objectArray += houses
|
||||||
|
objectArray += holes
|
||||||
|
|
||||||
width = 600
|
collisionsMap = [[False] * gridSize for _ in range(gridSize)]
|
||||||
|
for object in objectArray[1:]:
|
||||||
|
collisionsMap[object.pos.x][object.pos.y] = True
|
||||||
|
|
||||||
|
width = 610
|
||||||
height = 530
|
height = 530
|
||||||
screen = pygame.display.set_mode((width, height)) # ustalanie rozmiarów okna
|
screen = pygame.display.set_mode((width, height)) # ustalanie rozmiarów okna
|
||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
c = (255, 255, 255) # tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie
|
c = (255, 255, 255) # tymczasowy kolor tła - do usunięcia, jak już będzie zdjęcie
|
||||||
screen.fill(c)
|
screen.fill(c)
|
||||||
draw(15, objectArray)
|
draw(gridSize, objectArray)
|
||||||
kb_listen(objectArray, 15)
|
kb_listen(objectArray, gridSize)
|
||||||
pygame.display.update() # by krata pojawiła się w okienku - update powierzchni
|
pygame.display.update() # by krata pojawiła się w okienku - update powierzchni
|
||||||
|
BIN
junkyard.png
Normal file
BIN
junkyard.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 MiB |
Loading…
Reference in New Issue
Block a user