From 676d173010ea78cf43d8e27d5910eb60981878f4 Mon Sep 17 00:00:00 2001 From: kleph Date: Mon, 7 Sep 2009 22:25:46 +0200 Subject: [PATCH] added basic collision detection (bullts/enemies) --- pyshoot.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pyshoot.py b/pyshoot.py index 0e6e3a8..a7f9ee3 100755 --- a/pyshoot.py +++ b/pyshoot.py @@ -179,7 +179,10 @@ 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() @@ -314,6 +325,11 @@ while not globals.die: globals.moving_objects.update() 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)