In this tutorial we will show you how to implement, retrieve and display the current FPS (frames per second) for your Pygame application.
Role of FPS in Pygame
By default your Pygame program has no “fixed fps”. What this means is that your program has no limit on the number of times it updates/renders it’s objects and events. This can cause problems as the Game Loop will be executing at different speeds on different devices.
A faster computer may execute a single “frame” or “iteration” within 0.0001 seconds, whereas a slower computer can only manage 0.01. Hence we set a fixed number of iterations/frames that will occur every second. This makes it easier on game developers as well (too high or too low FPS need to be handled a bit differently).
Now that we understand what FPS is, let’s take a look at how to implement a “fixed fps” in our pygame application.
Setting an FPS Limit
First you need to create a Clock object as shown below.
clock = pygame.time.Clock()
Now in the Game Loop for your Pygame program, you need to “set” the FPS for your program. This is done using the tick() method on the clock object.
while True: ... ... clock.tick(60)
clock.tick() function takes the FPS limit as a parameter. Do note however, that this is the “max possible” FPS value that we are defining. The FPS will never go above this value, but it is possible to go below this.
It will not go below this limit by choice however. This only happens when the processing power of your device is not enough to complete 60 iterations/frames within a single second. You can fix this by upgrading your device, optimizing your application or setting a lower FPS limit.
Retrieve the FPS Value
If all you want is to see the current FPS, then just use the get_fps() method on the Clock object.
import pygame pygame.init() display = pygame.display.set_mode((400, 300)) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() print(clock.get_fps()) pygame.display.update() clock.tick(60)
Try increasing the FPS limit just to see how high your FPS can go!
Also, if your FPS is showing up as “0”, then it’s probably because you never set a limit for it using
tick(). You need to do that, otherwise the
get_fps() method won’t work properly.
Displaying the FPS in Pygame
If you want to take things a step further and have your FPS rendered to the screen, then you can make use of the below FPS Class that we created. It’s a combination of basic Pygame concepts that you should already be familiar with.
import pygame import sys pygame.init() display = pygame.display.set_mode((400, 300)) BLACK = (255, 255, 255) class FPS: def __init__(self): self.clock = pygame.time.Clock() self.font = pygame.font.SysFont("Verdana", 20) self.text = self.font.render(str(self.clock.get_fps()), True, BLACK) def render(self, display): self.text = self.font.render(str(round(self.clock.get_fps(),2)), True, BLACK) display.blit(self.text, (200, 150)) fps = FPS() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() display.fill((0, 0, 0)) fps.render(display) pygame.display.update() fps.clock.tick(60)
Here is the output:
If you want a full explanation on how we did this, check out this short 5-min video tutorial of ours on YouTube.
This marks the end of the How to display FPS in Pygame Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.