Dragonsy
Dragonsy

Reputation: 21

Why does my window keep flashing in pygame?

I made a window in pygame, tried to make the window white and even though I only call pygame.display.update() once it keeps flashing between white and black.

This is my code:

import pygame

running = True
pygame.init()

White = (255, 255, 255)

while running == True:
    screen = pygame.display.set_mode((1400, 800))
    screen.fill(White)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.display.update()

I tried changing it to pygame.display.flip() but it still stayed the same. I also tried to put pygame.display.update() out of the loop but then the screen just stayed black.

Upvotes: 2

Views: 69

Answers (1)

Rabbid76
Rabbid76

Reputation: 210938

You need to create the Pygame window (pygame.display.set_mode) once before the application loop instead of constantly creating a new window in the application loop:

import pygame

running = True
pygame.init()

screen = pygame.display.set_mode((1400, 800))
White = (255, 255, 255)

while running == True:
    screen.fill(White)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.display.update()

Upvotes: 2

Related Questions