Skip to content
Advertisement

How to set framerate in Pygame?

So in response to my question (How to continuously move an image smoothly in Pygame?) I was told to set my framerate in order to do my animation. But no matter where I place it, clock.tick(60) does nothing. I do clock = pygame.time.Clock() BTW. So how do I do this? I have researched and found this (pygame clock.tick() vs framerate in game main loop) but I don’t really understand this.

My main game function;

JavaScript

This is my main game loop;

JavaScript

The main game loop will not set the framerate because the main_game function has a loop inside it. This is only in a loop because when I want to restart the function I can. I just added this in to show how the function is run.

Advertisement

Answer

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.

e.g.:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement