Show the frame rate on the screen in pygame

If you want to check the frame rate in your game, here is how you can do it. First you need to render some text, you gotta do your way, cause there is not a function in pygame for that.

Follow these steps:

  • define a font object
  • render some text through the font object
  • blit it on a Surface or on the screen surface
import pygame

''' Example to show fps with pygame 28.12.2021 - python 3.10 '''


def scale(x):
	scaled = pygame.transform.scale(x, (50,50))
	return scaled


def render_fps(background=""):
	''' shows the frame rate on the screen '''

	fps_text = font.render(str(int(clock.get_fps())),1,(255,255,255)) # get the clocl'fps
	if background != "":
		bck.fill((255,0,0))
		bck.blit(fps_text,(0,0))
		screen.blit(scale(bck),(0,0))
	else:
		screen.blit(scale(fps_text),(0,0))


def mainloop():
	''' Infinite event loop where thing happens every frame '''

	while True:
		# screen.fill(0)
		render_fps(1)# call the function to blit fps
		for event in pygame.event.get(): # close window
			if event.type == pygame.QUIT:
				pygame.quit()
		pygame.display.update() # update the display
		clock.tick(60) # max frame rate to save memory


# FONT INIT
size = 20
font = pygame.font.SysFont("Arial", size) # a font for the fps
bck = pygame.Surface((size,size))
bck.fill((255,0,0))

if __name__ == "__main__":
	# INIT
	pygame.init()
	screen = pygame.display.set_mode((600, 400)) # the screen surface
	clock = pygame.time.Clock()
	mainloop()

To make the code more interesting, I added a surface as background of the text and also scaled the text a little bit, just to show you that you can. That is why it’s a little blurred in the image aboce, cause it’s scaled


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.