dodanie kolizji ze skałami

This commit is contained in:
Weranda 2023-03-30 20:10:29 +02:00
parent ea957fac37
commit 98f8669a3c
5 changed files with 23 additions and 5 deletions

Binary file not shown.

View File

@ -34,7 +34,9 @@ class Agent(pygame.sprite.Sprite):
self.movement() self.movement()
self.rect.x += self.x_change self.rect.x += self.x_change
self.collide_blocks('x')
self.rect.y += self.y_change self.rect.y += self.y_change
self.collide_blocks('y')
self.x_change = 0 self.x_change = 0
self.y_change = 0 self.y_change = 0
@ -49,3 +51,20 @@ class Agent(pygame.sprite.Sprite):
self.y_change -= TILE_SIZE self.y_change -= TILE_SIZE
if keys[pygame.K_DOWN] and self.rect.y < 832 - 64: if keys[pygame.K_DOWN] and self.rect.y < 832 - 64:
self.y_change += TILE_SIZE self.y_change += TILE_SIZE
def collide_blocks(self, direction):
if direction == "x":
hits = pygame.sprite.spritecollide(self,self.game.rock_sprites, False)
if hits:
if self.x_change > 0:
self.rect.x = hits[0].rect.left - self.rect.width
if self.x_change < 0:
self.rect.x = hits[0].rect.right
if direction == "y":
hits = pygame.sprite.spritecollide(self,self.game.rock_sprites, False)
if hits:
if self.y_change > 0:
self.rect.y = hits[0].rect.top - self.rect.height
if self.y_change < 0:
self.rect.y = hits[0].rect.bottom

View File

@ -24,7 +24,7 @@ class Game:
def new(self): # tworzy się nowa sesja grania def new(self): # tworzy się nowa sesja grania
self.all_sprites = pygame.sprite.LayeredUpdates() self.all_sprites = pygame.sprite.LayeredUpdates()
self.rock_sprites = pygame.sprite.Group() self.rock_sprites = pygame.sprite.LayeredUpdates()
self.agent = Agent(self,1,1) self.agent = Agent(self,1,1)
self.archer_ork = Archer_ork(self,10,10) self.archer_ork = Archer_ork(self,10,10)
self.infantry_ork = Infantry_ork(self,5,10) self.infantry_ork = Infantry_ork(self,5,10)
@ -35,8 +35,7 @@ class Game:
def update(self): # update postaci na mapie, związane z poruszaniem się def update(self): # update postaci na mapie, związane z poruszaniem się
self.all_sprites.update() self.all_sprites.update()
pygame.sprite.spritecollide(self.agent, self.rock_sprites, True)
#ograniczyć ruch agenta gdy jest kolizja ze skałami
def events(self): def events(self):
for event in pygame.event.get(): for event in pygame.event.get():

View File

@ -5,7 +5,7 @@ class Rocks(pygame.sprite.Sprite):
def __init__(self,game,x,y): def __init__(self,game,x,y):
self.game = game self.game = game
self.groups = self.game.rock_sprites self.groups = self.game.all_sprites, self.game.rock_sprites
pygame.sprite.Sprite.__init__(self, self.groups) pygame.sprite.Sprite.__init__(self, self.groups)
self.x = x * TILE_SIZE self.x = x * TILE_SIZE