IAmTurtle
IAmTurtle

Reputation: 1

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 :)

Upvotes: 0

Views: 606

Answers (1)

Rabbid76
Rabbid76

Reputation: 210878

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()

Upvotes: 1

Related Questions