How to Set Pygame FPS – Easy Game Clock Quick Tips

You can access the full course here: INTRO TO PYGAME


Frames per second are an integral part of game development, and mastering Pygame FPS is critical to mastering the framework. As such, in this tutorial we’ll focus on what is commonly called the Game Clock.

The Game Clock is crucial in games as it is used for tracking time within the game, regulating the framerate, and keeping specific game events in check. In the context of Pygame for Python, the Game Clock underscores the foundational structure of game development. We will delve into the nuances of the Game Clock and its role in controlling frames per second (FPS).

Whether you’re a novice exploring the realm of game development or an experienced developer brushing up your Python and Pygame skills, this tutorial is designed for you. Familiarity with Python programming language is expected to get the most out of this guide.

Project Files – Pygame FPS

Everything you require to follow this tutorial on Pygame FPS, from codes to scripts are made available to you. You can download the necessary project files from the link provided.

Download Project Files Here

Game Clock

In this lesson, we will focus on the concept of a Game Clock and Pygame FPS. In the context of Pygame, a clock is not used for telling you the time of day, instead, it is used to track time within the game.  This could be the time elapsed since the start of the game, the time taken to perform a single task, or the time since the last frame, to set a consistent FPS (frames per second). Note that this concept isn’t exclusive to Pygame FPS – many other game frameworks require similar implementations.

What is FPS?

The FPS is essentially the number of frames that are rendered to the player’s screen each second. As rendering is part of our game loop, this means the FPS is tied directly to the number of times our game loop runs every second. As programs run extremely quickly, our game loop may run too many times per second, so to limit this, we set the maximum Pygame FPS of our game.

A higher framerate will result in smoother gameplay, however, it will come at the cost of higher CPU usage, as we are doing more operations per second. Lowering the framerate will lower the CPU usage, but may result in more stuttering, especially noticeable at lower framerates. This is because we have fewer turns on our game loop to work with, meaning we have to move objects in large increments each time, and can perform checks less often than higher framerates.

Creating a Game Clock in Pygame

To implement a game clock for a specific Pygame FPS, we first need to create a variable called clock which will hold a pygame.time.Clock object. This should be done above our game loop so that it is ready to use when we need it.

clock = pygame.time.Clock()

Using our clock variable we can set the framerate for our Pygame FPS by passing our target framerate as a value in the tick function. This will pause the program until the next tick, so we will place this at the end of the while loop.

while True:


We will use a value here of 60 for 60 FPS. The only other clock methods you are likely to use are the get_time and get_fps methods. The get_time method simply gets the time of the clock at the last frame, and the get_fps method gets the current FPS of the game. We will not be using these in this lesson, but they are helpful methods to be aware of.


And there we have it – a deep dive into the Game Clock and its importance in setting the Pygame FPS. We have explored its role in managing FPS to have a balanced, resource-efficient gameplay experience. The ‘game loop’ is at the heart of our game, running innumerable times per second to render frames, and controlling it efficiently is elemental to your game’s performance. We hope this tutorial has boosted your understanding of how a Game Clock functions within a game.

As your knowledge of Pygame deepens, you can explore other interesting features to create more complex and engaging games. You can also experiment more with Pygame FPS and how different values affect the entire look and feel of your game. The world of game development is vast, and there is always something new to learn. Do not forget, practice makes a coder perfect. Keep experimenting and happy coding!


Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Unreal, Python, Godot and more.


Hi everyone and welcome to part three of our game components tutorials. At the end of part two, we had created our basic game loop. It’s not doing anything super complex, but at least we have the template set and we can start introducing some more components in this tutorial. We’ll focus specifically on the clock. Uh, when I say clock in this context, I don’t mean the kind that hangs on your wall and tells you the time of day. I’m talking more specifically about a game clock and Pygame FPS. So we’ll introduce the concept of a game clock and what it’s used for in a program and then we’ll add a pie game, game clock to our game.

Okay, so for Star Wars, what is a clock again, from a game standpoint? Well, a game clock is used to track time within a game soap. This could be the time elapsed since the beginning of the game. It could be the amount of time taken to perform a single task, but more often than not, it’s used to set the number of frames per second or f ps for short. Now, if you’ve done any kind of serious gaming, this may well be a figure that you are familiar with.

Very often there’s two that gamers will focus on. One is ping, which is more or less the speed of your internet connection, and the other is the F ps or the number of frames per second.

So what this number means is essentially it’s the number of frames that are rendered each second. From a game loop standpoint, this is really the amount of times that loop runs per second. So if you remember within a game loop, we have the three phases, the input phase, the update phase, and the render phase. Well, programs run really, really quickly, so this could happen many, many times per second. By assessing the F ps, we determine exactly how many times these checks will happen each second.

So assess these numbers higher or lower will have an effect on the performance. For example, if we set the FPSs to be quite high, we’ll get smoother gameplay because we’re just doing more things per second. So we don’t have to make these large jumps in save movement or updating or inputs or anything. So we will get smoother gameplay, but of course we are doing more things per second, so that’s going to demand more from the cpu. We will see a pretty steep decrease in performance if we’re trying to do a lot of other CPU intensive processes as well.

And sometimes this just doesn’t really work and the game crashes because of it. Conversely, a lower frame rate will lead to more stuttering because we have to move in larger increments or we have to perform fewer checks per second. So the game will just look a lot less smooth. However, it’s a lot more CPU friendly because we are demanding fewer actions per second from the cpu. And so depending on how new or old or sophisticated your system is, this might be something that we have to take into consideration. So games are always trying to balance the FPSs out.

Obviously we want it as high as possible because that gives a smoother better experience, but then some CPUs just can’t handle it. Okay, so we know roughly speaking what a clock is. If we still don’t know, it will become a little clearer once we implement it in our code. So let’s head over there and get started.

All right, so this is where we left off last time. We had just created our basic game loop here, not doing anything terribly exciting, but at least we have kind of annotated it to tell you exactly what each line of code is doing. You’ll also know I have an archive dot pi file. What I’m going to do from here on out is I’ll take all the code that we write at the end of one tutorial and stick it into archive dot pi.

So this will be a really long file with all of the code by the end. That way we have a complete archive of all of the steps that we took along the way. And of course I’ll label it appropriately. Okay, great. So we do still want all of this code in main dot pi, but again, some of the other code that we write in each section we won’t want, so that I’ll stick in archive dot pi. Cool.

So because we are going to be using our clock within our game loop, it’s a good idea to create it before the game loop, but of course after the pie game. in it, because the clock is a pie game idea. So we’ll create a variable just called clock and this will be a pie game dot time dot clock object like so. So this belongs in the time module, that’s why we have to do pie game dot time and it is an object of type clock.

Okay, so the perhaps most important thing that we’ll do with a clock throughout the game is to call clock dot tick. You’ll also note that the parameter there is a frame rate which defaults to an inch instant value of zero. This is where we can set the frame rate and clock dot tick will essentially force the clock onto the next tick.

Okay, so the frame rate just kind of slows things down or speeds things up to set the appropriate frame rate. Typically, actually we wouldn’t put it here. We would put this call in our wire loop at the very, very end of that loop. Okay, so we don’t want it within the fall loop, but we do want it at the very end of our wild loop. And then we set the f p s. Typically somewhere around 60 is a pretty good measure.

The more advanced systems might go up to one 20, but again, that’s kind of only the state of the art, really powerful CPUs that can handle that for advanced games. Now, other than clock dot tick, perhaps the only two methods that we would really be interested in from a pie game clock might be the clock dot get time, which gets the, uh, previous clock’s time and the FPSs, which gets the, uh, clock’s current FPSs. So I’ll just demonstrate what those look like it is.

Previous time might be equal to clock dot get time, like so. Okay. And then the fps is equal to the clock. Dot get fps like so. And you know what I, just to demonstrate these, I’m just gonna print them out for you. I guess actually what we’ll do is we’ll just do that and then print out previous time and then we’ll print out the FPSs and this, you don’t have to run along with me, I just wanna show you exactly what this looks like. We have our wire loop started, so let’s go ahead and run this.

We’ll just print out a bunch of numbers. Yeah, there we go. So we can see that the current time is going up and f p s is somewhere around 60 and uh, it can fluctuate a little bit depending on, uh, kind of what’s going on in the CPU. But, uh, otherwise, yeah, that’s, uh, exactly what’s happening there.

So because this can’t be difficult to stop running, that’s why I encourage you to, hopefully you didn’t stop running this as well. Uh, this was more just for me to demonstrate to you. Okay. So that is it for this tutorial. That is a brief introduction into the clock. And in the next tutorial we’ll introduce something a little bit more interesting so that we actually have something to run and see, uh, on screen there. Okay? So thanks for watching. We’ll see you in the next one.

Interested in continuing?  Check out our all-access plan which includes 250+ courses, guided curriculums, new courses monthly, access to expert course mentors, and more!