0

I've got a huge issue with pygame. Movement is always a bit jerky, even if the code is as simple as can be. Try running this simple example which moves a rect by 1 pixel at 60 FPS:

import sys
import pygame
pygame.init()

display = pygame.display.set_mode((640, 480), pygame.FULLSCREEN)
clock = pygame.time.Clock()
FPS = 60

def motion_test():

    rect = pygame.rect.Rect((0, 240), (40, 40))

    while 1:

        # clear display
        display.fill((0, 0, 0))

        # check quit
        for e in pygame.event.get():
            if (e.type == pygame.QUIT or
                e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE):
                pygame.quit()
                sys.exit()

        # move the rect by 1 pixel
        rect.x += 1
        if rect.x >= 640:
            rect.x = 0

        # draw the rect and flip the display
        pygame.draw.rect(display, (255, 0, 0), rect)
        pygame.display.flip()

        # tick the clock
        clock.tick(FPS)


if __name__ == "__main__":
    motion_test()

No matter on what hardware I test this, the rect is moving jerky. Every like half a second or so, it "jumps" a bit. It's just not smooth motion.

Please, this makes me mad... does anybody know what might cause this? Is it pygame itself, or Python's garbage collection, or the OS, or just what? Any ideas? Do you see the same running the example?

(Win7 64bit, Pygame 1.9.1, Python 2.7.3)

**EDIT:

I have found out that the pygame clock object is not very accurate when limitting it to 60 FPS. I have now replaced the pygame clock with the pyglet clock, and now it's much better. Still not perfect, but better.

Don Polettone
  • 165
  • 1
  • 14
  • It works perfectly on my machine. Perhaps it's your hardware? – Anthony Nov 09 '15 at 02:58
  • You mean the rect is moving a100% smoothly on your screen? – Don Polettone Nov 11 '15 at 23:12
  • Yes. It could also be an issue with the fullscreen being a different size than the given size, try removing `pygame.FULLSCREEN`, although it is unlikely that it will change anything. – Anthony Nov 12 '15 at 04:02
  • the rect jitters in windowed mode, too... I just don't know what to do anymore. I tried to replace the pygame clock with pyglet, but it's not really better either. Further hints? – Don Polettone Nov 13 '15 at 01:54
  • I don't know. The only thing I can think of is that the problem is not a problem with the code, because it works perfectly on mine, but maybe a problem with your hardware. Try using resource monitor, to see what is happening when you run the program. – Anthony Nov 13 '15 at 02:46

0 Answers0