1

So, I am making a game engine in python using pygame. I am trying to get and display fps using pygame.time.Clock().get_fps(), but it is only saying 0.0 . Here is the basic code:

import pygame
from pygame.locals import *
screen = pygame.display.set_mode()

while 1:
  print(pygame.time.Clock().get_fps())
  pygame.time.Clock().tick(60)

I just need anything that exactly shows current fps.

2 Answers2

2

See get_fps()

Compute your game's framerate (in frames per second). It is computed by averaging the last ten calls to Clock.tick().

So there are 2 reasons why your code does not work. You create a new instance of pygame.time.Clock() every time you call it, and the FPS cannot be averaged. But an even bigger problem is that you call tick from another instance.
You must create one Clock object before the application loop, but not in the application loop, and call tick and get_fps from that object. Also handle the events in the application loop (PyGame window not responding after a few seconds):

import pygame
from pygame.locals import *
screen = pygame.display.set_mode()
clock = pygame.time.Clock()
while 1:
  pygame.event.pump()
  print(clock.get_fps())
  clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
1

A better way would be to create your clock at the top of your code. Then within the game loop at the end you can enact the .tick function.

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode()
clock = pygame.time.Clock()

while True:
  pygame.display.update()
  clock.tick(60)
AmeriNam20
  • 103
  • 6
  • What is the difference with my answer? Is there anything new that was not included in my answer? – Rabbid76 Dec 09 '22 at 11:14
  • Our answers are pretty much the same. In a standard pygame our game loop should be continuous. Either by say while True or While run. As our game loop is true and we haven't ended our game there is no need for "pygame.event.pump()" because our game loop is constantly running – AmeriNam20 Dec 10 '22 at 01:35
  • No. On some systems your loop will stop responding. You must call `pygame.event.pump()`. See [Pygame window not responding after a few seconds](https://stackoverflow.com/questions/20165492/pygame-window-not-responding-after-a-few-seconds) – Rabbid76 Dec 10 '22 at 07:44