python - Pygame Flashing Sprite After Damage -
i continuing work using pygame , working on player sprite, specifically, when takes damage. when player takes damage enemy, want player sprite blink 2-3 times giving player second or 2 move taking damage. have health bar (3 hearts) , set every time there collision between enemy , player sprites, remove 1. using kill() function ( know wrong since removes sprite). how can sprite flash second or two. or advice appreciated. thank you.
enemy_hit_list = pygame.sprite.spritecollide(self, self.level.enemy_list, false) if enemy_hit_list: self.health -= 1 self.kill()
you have specialize class adding attribute there show flashing state. "kill" method remvoes sprites groups - can't know snippet above if are, , effect. if using group draw player (self), removing there not best thing do.
you could, example, have "hit_countdown" attribute on sprite, , use it's update method change it's image accordingly, , measure time go normal:
class player(sprite): def __init__(self, ...): ... self.hit_countdown = 0 ... def update(self, ...): if self.hit_coundown: if not hasattr(self, original_image): self.original_image = self.image if self.hit_countdown % 2: self.image = none # (or other suitable pre-loaded image) else: self.image = self.original_image self.hit_countdown = max(0, self.hit_countdown - 1) super(player, self).update(...) # , on hit code above: ... if enemy_hit_list: self.health -= 1 self.hit_countdown = 6
Comments
Post a Comment