looking to hold a pygame window open for a few seconds then close it

189 Views Asked by At

(very new to python)i have this code below and i am wondering how i can hold the window open for a set amount of time then close the window. can anybody help me? anything helps

i really have no idea how to do this and i cant find anything about it anywhere. any help are suggestions would be greatly appreciated

import pygame


window_x=600
window_y=600
background=(255,200,255)
pygame.init()
world=pygame.display.set_mode([window_x,window_y])
game_on=True
while game_on:
    world.fill(background)
    pygame.display.flip()


1

There are 1 best solutions below

0
On

This is a way you can do something like this without freezing your program with the time library.

import pygame
from pygame.locals import *

pygame.init()
window_x=600
window_y=600
background=(255,200,255)
FPS = pygame.time.Clock() # add a time tracker
time = 0 #start at 0

world=pygame.display.set_mode((window_x,window_y), 0, 32)

def main(frames):
    game_on=True
    while game_on:
        frames += 1 #every frame you add 1
        world.fill(background)
        for event in pygame.event.get():  #other ways to close the window
            if event.type == QUIT:
                pygame.quit()
                exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    exit()
        if frames == 60: #every 30 frames is 1 second, here after 2 seconds the window closes
            exit()
        pygame.display.flip()
        FPS.tick(30) #here Im setting that 1 second = 30 frames.
main(time)