added basic collision detection (bullts/enemies)

This commit is contained in:
kleph 2009-09-07 22:25:46 +02:00
parent 4be9f64baa
commit 676d173010

View file

@ -179,7 +179,10 @@ class Enemy(Object):
def aim_player(self): def aim_player(self):
""" compute speed vector to aim at current player position """ """ compute speed vector to aim at current player position """
def die(self):
""" make the enemy dying """
globals.enemies_list.remove(self)
def init_gfx(): def init_gfx():
"""graphics initialisation""" """graphics initialisation"""
@ -281,6 +284,14 @@ def transition_fade_to_white():
""" lighten every pixels on screen """ """ lighten every pixels on screen """
pass 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 # Main
## ##
@ -297,7 +308,7 @@ player = Player(['player1_1.bmp', 'player1_2.bmp'], (50, 50), (0, 0))
globals.player = player globals.player = player
# background # 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() background_rect = globals.background.get_rect()
globals.screen.blit(globals.background, background_rect) globals.screen.blit(globals.background, background_rect)
pygame.display.flip() pygame.display.flip()
@ -314,6 +325,11 @@ while not globals.die:
globals.moving_objects.update() globals.moving_objects.update()
globals.bullets_list.update() globals.bullets_list.update()
globals.enemies_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. # add player (if it's not already in the group). ugly, but ok for now.
globals.moving_objects.add(player) globals.moving_objects.add(player)