dodanie kolizji ze skałami
This commit is contained in:
parent
ea957fac37
commit
98f8669a3c
Binary file not shown.
Binary file not shown.
21
agent.py
21
agent.py
@ -34,7 +34,9 @@ class Agent(pygame.sprite.Sprite):
|
||||
self.movement()
|
||||
|
||||
self.rect.x += self.x_change
|
||||
self.collide_blocks('x')
|
||||
self.rect.y += self.y_change
|
||||
self.collide_blocks('y')
|
||||
|
||||
self.x_change = 0
|
||||
self.y_change = 0
|
||||
@ -48,4 +50,21 @@ class Agent(pygame.sprite.Sprite):
|
||||
if keys[pygame.K_UP] and self.rect.y > 0:
|
||||
self.y_change -= TILE_SIZE
|
||||
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
|
||||
|
5
main.py
5
main.py
@ -24,7 +24,7 @@ class Game:
|
||||
|
||||
def new(self): # tworzy się nowa sesja grania
|
||||
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.archer_ork = Archer_ork(self,10,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ę
|
||||
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):
|
||||
for event in pygame.event.get():
|
||||
|
@ -5,7 +5,7 @@ class Rocks(pygame.sprite.Sprite):
|
||||
|
||||
def __init__(self,game,x,y):
|
||||
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)
|
||||
|
||||
self.x = x * TILE_SIZE
|
||||
|
Loading…
Reference in New Issue
Block a user