Example #1
0
class PlayerGui():
    def __init__(self, player):
        self.player = player
        self.ammo_counter = DrawText(self.player,
                                     (self.player.game.screen_rect.midtop),
                                     self.player.all_sprites_container)
        self.health_bar = HealthBar(self, (self.player.stats.health, 25),
                                    (50, 50), c.BLUE,
                                    self.player.all_sprites_container)

    def update(self):
        self.health_bar.update(self.player.stats.health)
        self.ammo_counter.update(
            str(self.player.gun.clip) + "/" + str(self.player.gun.ammo_held))
Example #2
0
class Enemies(pg.sprite.DirtySprite):
    """Basic enemy class."""
    def __init__(self, game, player, pos, size, *groups):
        super().__init__(*groups)
        self.game = game
        self.image = pg.Surface(size).convert()
        self.image.fill(c.SILVER)
        self.rect = self.image.get_rect()
        self.rect.topleft = pos
        self.player = player
        self.health_bar = None

    def update(self, room, dt):
        self.check_if_alive(room, dt)

    def check_if_alive(self, room, dt):
        if self.health > 0:
            for health_bar in room.health_bar_container:
                health_bar.move_health_bar(dt)
            self.handle_bullet_hit(room)
        else:
            self.kill()
            room.health_bar_container.update(self.health)

    def handle_bullet_hit(self, room):
        bullet_hit = pg.sprite.spritecollide(self, room.bullet_container, True)
        for bullet in bullet_hit:
            self.health -= bullet.gun.output_damage
            bullet.kill()
            self.handle_health_bar(room)

    def handle_health_bar(self, room):
        if self.health > 0:
            self.health_bar = HealthBar(
                self, (self.health, 10),
                (self.rect.centerx, self.rect.center[1] - 30), c.RED)
            self.health_bar.rect.centerx = self.rect.centerx
            self.health_bar.add_to_group(room.health_bar_container)
            self.game.all_sprites.add(self.health_bar)
            self.health_bar.update(self.health)