added dying state and animation to the ennemy

This commit is contained in:
kleph 2009-10-08 01:10:24 +02:00
parent 676d173010
commit 81d94ab8af

View file

@ -26,6 +26,7 @@ class Globals:
self.bullets_list = pygame.sprite.RenderUpdates()
self.bulletfactory = BulletFactory()
self.enemies_list = pygame.sprite.RenderUpdates()
self.enemies_active_list = pygame.sprite.RenderUpdates()
self.player = None
@ -39,6 +40,7 @@ class Object(pygame.sprite.Sprite):
pygame.sprite.Sprite.__init__(self)
self.speed = speed
self.frames = frames
self.frames_index = 0
self.image = None
self.load()
self.rect = self.image.get_rect().move(pos)
@ -51,7 +53,7 @@ class Object(pygame.sprite.Sprite):
image.set_colorkey(pygame.Color(255,255,255, 0))
self.frames[index] = image
index += 1
self.image = self.frames[0]
self.image = self.frames[self.frames_index]
def update(self):
self.rect.move_ip(self.speed)
@ -168,12 +170,20 @@ class Enemy(Object):
def __init__(self, frames, type, pos):
Object.__init__(self, frames, pos, (-1, 0))
self.counter = 0
self.threshold = 10
self.threshold = 2
self.dying = False
def update(self):
if self.counter == self.threshold:
self.rect.move_ip(self.speed)
self.count = 0
if self.dying:
self.frames_index += 1
if self.frames_index == 6:
globals.enemies_list.remove(self)
return
self.image = self.frames[self.frames_index]
else:
self.rect.move_ip(self.speed)
self.counter = 0
else:
self.counter += 1
@ -182,7 +192,12 @@ class Enemy(Object):
def die(self):
""" make the enemy dying """
globals.enemies_list.remove(self)
self.dying = True
self.threshold = 5
self.frames_index = 2
self.image = self.frames[self.frames_index]
globals.enemies_active_list.remove(self)
def init_gfx():
"""graphics initialisation"""
@ -236,7 +251,9 @@ def process_key(event, player):
elif event.key == K_x:
player.start_shoot()
elif event.key == K_e:
globals.enemies_list.add(Enemy(['enemy1_1.bmp', 'enemy1_2.bmp'], 0, (500, 200)))
e = Enemy(['enemy1_1.bmp', 'enemy1_2.bmp', 'enemy1_3.bmp', 'enemy1_4.bmp', 'enemy1_5.bmp', 'enemy1_6.bmp'], 0, (500, 200))
globals.enemies_list.add(e)
globals.enemies_active_list.add(e)
elif event.key == K_SPACE:
player.change_bullet_type()
@ -327,7 +344,7 @@ while not globals.die:
globals.enemies_list.update()
# collisions
ret = pygame.sprite.groupcollide(globals.bullets_list, globals.enemies_list, False, False)
ret = pygame.sprite.groupcollide(globals.bullets_list, globals.enemies_active_list, False, False)
handle_enemies_collisions(ret)