How to wait some time in pygame?

For animation / cooldowns, etc: If you want to ‘wait’, but still have code running you use: pygame.time.get_ticks

class Unit():
    def __init__(self):
        self.last = pygame.time.get_ticks()
        self.cooldown = 300    

    def fire(self):
        # fire gun, only if cooldown has been 0.3 seconds since last
        now = pygame.time.get_ticks()
        if now - self.last >= self.cooldown:
            self.last = now
            spawn_bullet()

Leave a Comment