Pygame FPS is only 0.0

Question:

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.

Asked By: Hudson Rocke

||

Answers:

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)
Answered By: Rabbid76

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)
Answered By: AmeriNam20