简体   繁体   中英

Pygame window closes instantly

My pygame window closes without any errors right after I run the code
Here's my code:

import pygame pygame.init() # initialises pygame

win = pygame.display.set_mode((500, 500)) # width, height

pygame.display.set_caption("Project_01")

If anyone can help, thank you in advance:)

See Why is my PyGame application not running at all? Your application will exit right after the window is created. You have to implement an application loop. You must implement an application loop to prevent the window from closing immediately:

import pygame 

pygame.init() # initialises pygame
win = pygame.display.set_mode((500, 500)) # width, height
pygame.display.set_caption("Project_01")

clock = pygame.time.Clock()
run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    win.fill((0, 0, 0))

    # draw scene
    # [...]

    pygame.display.flip()

pygame.quit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM