Search code examples
pythonpygamepygame-clockpygame-tick

Pygame clock tick stuttering


This is an issue that has been bugging me for a few weeks now. Whenever I have a pygame clock variable, so for example: clock = pygame.time.clock and I limit the fps using: clock.tick(fps) the game will occasionally stutter. I have a simple example below - a window with a cube that moves from side to side.

import pygame

screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
width, height = screen.get_size()

rect = pygame.Rect(0, height // 2 - 50, 100, 100)

delta_x = 5

clock = pygame.time.Clock()

running = True
while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = False

    screen.fill((0, 0, 0))

    if rect.left < 0 or rect.right > width:
        delta_x *= -1

    pygame.draw.rect(screen, (255, 255, 255), rect)
    rect.x += delta_x

    pygame.display.flip()

    clock.tick(60)

Video: https://www.youtube.com/watch?v=6spFoKIqVQY&ab_channel=NotAHackusator
Does anyone know how to fix this? Thanks in advance.


Solution

  • Setting clock.tick to 144 from 60 makes the stutter go away from my end. I'm not not sure if you want it to be limited to 60 and why though.