-1

When I try to move Surface in Pygame, it seems to stutter in a weird way. Even when I use dt, the movement speed is not constant. Even thought FPS oscilate around 59-61, the Surface sometimes moves just a few, pixels and sometimes makes a big jump ahead. Theres no visible lag, or pause between the frames. I've seen this problem occur in many Pygame projects.

I haven't found any working fix to this problem, except using higher framerate rates.

import pygame

# setup
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

# start position of rect
x = 0
dt = 0

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # move react
    x += 200 * dt

    # refresh
    screen.fill("black")
    # draw rect on Surface
    pygame.draw.rect(screen, "white", (x, 200, 50, 50))
    # flip
    pygame.display.flip()

    dt = clock.tick(60) / 1000
New contributor
Master Machine68 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

2

The stuttering could be due to a specific combination of computer and monitor. It may be more noticeable to you than others depending on your CPU, graphics card, and the display refresh rate of your monitor. So others may not see it at all when running the same code.

That being said, try including the following in your display init. This tends to smooth things out.

screen = pygame.display.set_mode((800, 600), pygame.SCALED, vsync=1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.