added basic collision detection (bullts/enemies)
This commit is contained in:
parent
4be9f64baa
commit
676d173010
1 changed files with 18 additions and 2 deletions
18
pyshoot.py
18
pyshoot.py
|
@ -180,6 +180,9 @@ class Enemy(Object):
|
|||
def aim_player(self):
|
||||
""" compute speed vector to aim at current player position """
|
||||
|
||||
def die(self):
|
||||
""" make the enemy dying """
|
||||
globals.enemies_list.remove(self)
|
||||
|
||||
def init_gfx():
|
||||
"""graphics initialisation"""
|
||||
|
@ -281,6 +284,14 @@ def transition_fade_to_white():
|
|||
""" lighten every pixels on screen """
|
||||
pass
|
||||
|
||||
def handle_enemies_collisions(collisions):
|
||||
""" take the returned dictionnary from pygame.sprite.groupcollide()"""
|
||||
for bullet, enemies in collisions.iteritems():
|
||||
if enemies != []:
|
||||
for e in enemies:
|
||||
e.die()
|
||||
globals.bullets_list.remove(bullet)
|
||||
|
||||
##
|
||||
# Main
|
||||
##
|
||||
|
@ -297,7 +308,7 @@ player = Player(['player1_1.bmp', 'player1_2.bmp'], (50, 50), (0, 0))
|
|||
globals.player = player
|
||||
|
||||
# background
|
||||
globals.background = pygame.image.load(os.path.join('data', 'background.bmp')).convert()
|
||||
globals.background = pygame.image.load(os.path.join('data', 'background1.bmp')).convert()
|
||||
background_rect = globals.background.get_rect()
|
||||
globals.screen.blit(globals.background, background_rect)
|
||||
pygame.display.flip()
|
||||
|
@ -315,6 +326,11 @@ while not globals.die:
|
|||
globals.bullets_list.update()
|
||||
globals.enemies_list.update()
|
||||
|
||||
# collisions
|
||||
ret = pygame.sprite.groupcollide(globals.bullets_list, globals.enemies_list, False, False)
|
||||
handle_enemies_collisions(ret)
|
||||
|
||||
|
||||
# add player (if it's not already in the group). ugly, but ok for now.
|
||||
globals.moving_objects.add(player)
|
||||
|
||||
|
|
Loading…
Reference in a new issue