Search code examples
pygamepygame-clock

Pygame: pygame.time.Clock, or time.sleep


I have a partner in this Pygame project. So it's for fun, and it's Snake. He says to use time.sleep instead of pygame.clock since it is 'better'. Is his choice better, or is mine better?


Solution

  • Use pygame.time.Clock to control the frames per second and thus the game speed.

    The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. See pygame.time.Clock.tick():

    This method should be called once per frame.

    That means that the loop:

    clock = pygame.time.Clock()
    run = True
    while run:
       clock.tick(60)
    

    runs 60 times per second.

    For comparison, time.sleep just waits a constant amount of time. If you want to get a constant frame rate you need to use pygame.time.Clock.tick.