Cookle Plex
Cookle Plex

Reputation: 23

pygame.display.flip() seems to not be working?

I've been working on a project, and when I tested it this happened. After a little testing I soon realized it was something wrong in the pygame display flip part of the code. I really don't see what's wrong here, so I hope one of you do.

import pygame
pygame.init()
screen = pygame.display.set_mode((200, 200))
img = pygame.image.load("test.png")
while 1:
    screen.fill((0, 0, 0))
    screen.blit(img, (0, 0))
    pygame.display.flip()
    pygame.time.delay(10)

Now, the result is a blank white screen that's 200 by 200. I hope someone sees what's wrong here. Also, my png doesn't matter here because I get the same result with just the fill(black), so I hope someone knows.

Upvotes: 2

Views: 2458

Answers (1)

The4thIceman
The4thIceman

Reputation: 3889

Couple things here:

1) I would recommend using the pygame clock instead of time.delay. Using the clock sets the frames per second to run the code, where the time.delay just sits and waits for the delay.

2) Pygame is event driven, so you need to be checking for events, even if you don't have any yet. otherwise it is interpreted as an infinite loop and locks up. for a more detailed explanation: click here

3) I would offer a way out of the game loop with a flag that can be set to false, that way the program can naturally terminate

import pygame

pygame.init()
screen = pygame.display.set_mode((200, 200))
img = pygame.image.load("test.png")
clock = pygame.time.Clock()
game_running = True
while game_running:
    # evaluate the pygame event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False  # or anything else to quit the main loop

    screen.fill((0, 0, 0))
    screen.blit(img, (0, 0))
    pygame.display.flip()
    clock.tick(60)

Upvotes: 4

Related Questions