在 Pygame 中显示 FPS

Show FPS in Pygame

我正在 python 和 pygame 的朋友一起做一个项目。我们尝试在我们的游戏中显示 FPS,但我们失败了。 fps 计数器始终为零。这是代码:

#Create Text
def Render_Text(what, color, where):
    font = pygame.font.Font('assets/Roboto.ttf', 30)
    text = font.render(what, 1, pygame.Color(color))
    window.blit(text, where)
#Show FPS
    Render_Text(str(int(clock.get_fps())), (255,0,0), (0,0))
    print("FPS:", int(clock.get_fps()))

get_fps() only gives a correct result, when you call tick()每帧一次:

clock = pygame.time.Clock()
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False          

    clock.tick()
    print(clock.get_fps())

    # [...]

请参阅 get_fps()

的文档

get_fps() compute the clock framerate

get_fps() -> float

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