Search code examples
pythonpygameframe-rate

How can I blit the frame rate on my screen with pygame?


I know that I can print the fps in the shell using print(clock.get_fps()), but I want to blit the fps on the screen while I'm playing.

I tried to use

fps_text = game_font.render(clock.get_fps(), True, (255, 255, 0))
win.blit(fps_text, (1000, 100))

(font is already initialized, I use it somewhere else in my program)

I get the error TypeError: text must be a unicode or bytesfor the first line of the code. Is there a way to do what I'm trying to do ? Thanks for the answers


Solution

  • clock.get_fps() returns a float. The first argument of 'render' needs to be a str.

    Do this instead: game_font.render(str(clock.get_fps()), True, (255, 255, 0))