Justin Farr
Justin Farr

Reputation: 183

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?

Upvotes: 0

Views: 1976

Answers (2)

Malik Brahimi
Malik Brahimi

Reputation: 16711

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)

Upvotes: 0

Filip Dupanović
Filip Dupanović

Reputation: 33670

You should use pygame.time.Clock to track time as it passes.

Upvotes: 1

Related Questions