Search code examples
pythontimerpygamestartup

Python/Pygame How to make something disappear off of screen after a set amount of time?


I was wondering if there was any sort of code to make something disappear from the screen after a set time? Something like an intro to your game?


Solution

  • You used the tag so use a threading.Timer object to call a function after some time:

    class Menu:
        def __init__(self):
            self.active = True
    
            # start a timer for the menu
            Timer(3, self.reset).start()
    
        def update(self, scr):
            if self.active:
                surf = pygame.Surface((100, 100))
                surf.fill((255, 255, 255))
                scr.blit(surf, (270, 190))
    
        def reset(self):
            self.active = False
    
    menu = Menu()
    
    while True:
        pygame.display.update()
        queue = pygame.event.get()
        scr.fill((0, 0, 0))
    
        for event in queue:
            if event.type == QUIT:
                pygame.quit(); sys.exit()
    
        menu.update(scr)