Skip to content Skip to sidebar Skip to footer

Pygame: Display.update() Does Not Update Until After Clock Delay

I am using pygame's time.Clock to run my game at a lower FPS and I noticed that my input seemed to take one extra frame to take effect. I did some debugging and realized that it wa

Solution 1:

I was able to fix this glitch in one program by calling pygame.event.pump() (which does not clear the event queue, so no event-handling is lost) between the pygame.display.update() call and the clock.tick(FPS) call. Basically, my main game loop ended with:

pygame.display.update()
pygame.event.pump()
clock_object.tick(FPS_CONSTANT)

Solution 2:

I have found a way to eliminate the one-frame delay I was experiencing, although I still can't explain why pygame.display.update() waits for the clock delay to update the screen. I figured I would post my solution here in case anyone else has the same issue.

I noticed that the display will update after pygame.event.get() is run, so I set my loop to check for events at a faster frame rate. I still only want to run my game logic and update the screen at a lower frequency, so I just count how many times the loop has executed. Notice the variables frequency, FPS, and skip_frames in my new example below:

import pygame, sys
from pygame.localsimport *

pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
colour = 1
key_press = False
frequency = 0.5# How often the game logic is run
FPS = 30# How often the user input is checked
skip_frames = FPS/frequency
count_frames = 0whileTrue:
    # *** Check for user input every loop ***for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            key_press = True# *** Only run game logic at given frequency ***if count_frames >= skip_frames:
        count_frames -= skip_frames

        if key_press:
            screen.fill((0,255,0))
            print("A key was pressed.")
            key_press = Falseelse:
            screen.fill((colour*255, colour*255, colour*255))
            colour = not colour

        pygame.display.update()

    count_frames += 1
    clock.tick(FPS)

This is a bit of a work-around, but it gives the result I was looking for. The screen will flash at the given frequency, and it will change to green the very next flash after any key is pressed. If anyone can figure out why pygame.display.update() waits for the clock delay to actually update the screen, I would be very interested to know.

Solution 3:

pygame.time.Clock.ticksource calls SDL_Delay which lets the program sleep for a given time.

By passing 0.5 as the frame rate to clock.tick, you're telling pygame to slow down so that only a half frame is processed per second. That actually means the game will only be updated every two seconds and it will be unresponsive during the rest of the time.

If you want to implement a timer in pygame, use one of the solutions here (pygame.time.get_ticks, set_timer, or use the delta time).

Post a Comment for "Pygame: Display.update() Does Not Update Until After Clock Delay"