Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait some time in pygame?

While I've been using time.wait in my code since I began learning Python and Pygame, I've been wondering if there are any other ways to do it and what are the advantages and disadvantages of each approach. For example, Pygame also has a pygame.time.wait. What's the difference between python's wait and pygame's wait functions? Which one is better? And are there other ways to wait some time besides using these two functions?

like image 633
bzrr Avatar asked Sep 16 '13 23:09

bzrr


People also ask

How do I add wait time in Pygame?

For Pygame, however, using pygame. time. delay() will pause for a given number of milliseconds based on the CPU clock for more accuracy (as opposed to pygame. time.

Can you use time sleep in Pygame?

In Pygame, you have to use pygame. time. wait() instead of python's time. sleep() .

What does Pygame quit () do?

quit() is a function that closes pygame (python still running) While pygame. QUIT just checks if pygame is running or not. I put pygame. quit() function after the while loop to make sure that pygame is not running after the while loop.


1 Answers

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()
like image 177
ninMonkey Avatar answered Sep 23 '22 20:09

ninMonkey