This question already has an answer here:
- Countdown timer in Pygame 5 answers
i would like to spawn a 'boss' Sprite after a certain time has passed or x amount of mobs have spawned and how could i display the timer on the screen.
class for boss
class Boss(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(boss_img, (150, 200))
self.image.set_colorkey(Black)
self.rect = self.image.get_rect()
self.rect.center = ((Width / 2, -70))
self.speedy = 1
self.shoot_delay = 250
self.last_shot = pygame.time.get_ticks()
self.hp = 150
self.dead = False
def update(self):
if self.hp <= 25:
self.speedy = 3
if self.rect.top > Height + 10:
player.lives = 0
There are several ways to implement a timer in pygame. You can use the time that
pygame.Clock.tick
returns to increase or decrease a timer variable, calculate the time difference withpygame.time.get_ticks
or use a custom event in conjunction withpygame.time.set_timer
.Example 1 - delta time:
If you want to spawn exactly 1 sprite, you can add another variable like
boss_spawned = False
and change the timer only if the boss hasn't spawned:Or set the timer to exactly 0 after the spawn and only decrease the timer if it's
!= 0
.Example 2 - pygame.time.get_ticks (replace the main function above):
If you just want to count the kills or spawned mobs, you can increment a counter variable and then spawn the enemy boss when it exceeds some limit. The following example just counts the mouse clicks and spawns a block after 3 clicks.