prevent enemies from shooting themselves and added basic player

collision
This commit is contained in:
kleph 2010-02-23 00:25:02 +01:00
parent bb4ff64e60
commit 8acaaed74b

View file

@ -24,11 +24,13 @@ class Globals:
self.die = False self.die = False
self.moving_objects = pygame.sprite.RenderUpdates() self.moving_objects = pygame.sprite.RenderUpdates()
self.bullets_list = pygame.sprite.RenderUpdates() self.bullets_list = pygame.sprite.RenderUpdates()
self.bullets_list_player = pygame.sprite.RenderUpdates()
self.bullet_factory = BulletFactory() self.bullet_factory = BulletFactory()
self.enemies_list = pygame.sprite.RenderUpdates() self.enemies_list = pygame.sprite.RenderUpdates()
self.enemies_active_list = pygame.sprite.RenderUpdates() self.enemies_active_list = pygame.sprite.RenderUpdates()
self.enemy_factory = EnemyFactory() self.enemy_factory = EnemyFactory()
self.player = None self.player = None
self.players_list = pygame.sprite.RenderUpdates()
class Event_Spawn: class Event_Spawn:
def __init__(self, enemy_type, pos): def __init__(self, enemy_type, pos):
@ -146,7 +148,7 @@ class Player(Object):
self.shoot_wavepos = 0 self.shoot_wavepos = 0
self.shooting = False self.shooting = False
globals.bullets_list.add(bullet) globals.bullets_list_player.add(bullet)
def start_move(self, vector): def start_move(self, vector):
""" initiate moving sequence """ """ initiate moving sequence """
@ -390,6 +392,12 @@ def handle_enemies_collisions(collisions):
if enemies != []: if enemies != []:
for e in enemies: for e in enemies:
e.die() e.die()
globals.bullets_list_player.remove(bullet)
def handle_player_collisions(collisions):
""" take the returned dictionnary from pygame.sprite.groupcollide()"""
for bullet, enemies in collisions.iteritems():
# show some player reaction ;-)
globals.bullets_list.remove(bullet) globals.bullets_list.remove(bullet)
## ##
@ -409,6 +417,7 @@ level = Level()
# create_player # create_player
player = Player(['player1_1.bmp', 'player1_2.bmp'], (50, 50), (0, 0)) player = Player(['player1_1.bmp', 'player1_2.bmp'], (50, 50), (0, 0))
globals.player = player globals.player = player
globals.players_list.add(player)
# background # background
globals.background = pygame.image.load(os.path.join('data', 'background1.bmp')).convert() globals.background = pygame.image.load(os.path.join('data', 'background1.bmp')).convert()
@ -422,16 +431,20 @@ while not globals.die:
# erase previously moved objects # erase previously moved objects
globals.moving_objects.clear(globals.screen, globals.background) globals.moving_objects.clear(globals.screen, globals.background)
globals.bullets_list.clear(globals.screen, globals.background) globals.bullets_list.clear(globals.screen, globals.background)
globals.bullets_list_player.clear(globals.screen, globals.background)
globals.enemies_list.clear(globals.screen, globals.background) globals.enemies_list.clear(globals.screen, globals.background)
# update game logic # update game logic
globals.moving_objects.update() globals.moving_objects.update()
globals.bullets_list.update() globals.bullets_list.update()
globals.bullets_list_player.update()
globals.enemies_list.update() globals.enemies_list.update()
# collisions # collisions
ret = pygame.sprite.groupcollide(globals.bullets_list, globals.enemies_active_list, False, False) ret = pygame.sprite.groupcollide(globals.bullets_list_player, globals.enemies_active_list, False, False)
handle_enemies_collisions(ret) handle_enemies_collisions(ret)
ret = pygame.sprite.groupcollide(globals.bullets_list, globals.players_list, False, False)
handle_player_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.
@ -440,6 +453,7 @@ while not globals.die:
# drawing # drawing
rectlist = globals.moving_objects.draw(globals.screen) rectlist = globals.moving_objects.draw(globals.screen)
rectlist.extend(globals.bullets_list.draw(globals.screen)) rectlist.extend(globals.bullets_list.draw(globals.screen))
rectlist.extend(globals.bullets_list_player.draw(globals.screen))
rectlist.extend(globals.enemies_list.draw(globals.screen)) rectlist.extend(globals.enemies_list.draw(globals.screen))
pygame.display.update(rectlist) pygame.display.update(rectlist)