pygame:如何让我的游戏真正达到 运行 60fps?
pygame: how do I get my game to actually run at 60fps?
在我的主循环中我有:
clock.tick_busy_loop(60)
pygame.display.set_caption("fps: " + str(clock.get_fps()))
但是,读数显示游戏正在以 62.5fps 的速度读取。
然后我尝试输入 clock.tick_busy_loop(57.5)
,这给了我 58.82...fps 的读数。当我设置 clock.tick_busy_loop(59)
时,我再次获得 62.5fps。看起来在 58.8fps 和 62.5fps 之间有一个我无法在这里克服的阈值。如何让我的游戏真正达到 60fps 运行?我主要在寻找这种控制,因为我执行的事件取决于音乐时间。
所以我根据上面的评论制作了一个简单的演示,它使用系统时间模块而不是 pygame.time 模块。你可以忽略 OpenGL 的东西,因为我只是想在屏幕上渲染一些简单的东西。最重要的部分是每帧结束时的时序代码,我在代码中已经注释了。
import pygame
import sys
import time
from OpenGL.GL import *
from OpenGL.GLU import *
title = "FPS Timer Demo"
target_fps = 60
(width, height) = (300, 200)
flags = pygame.DOUBLEBUF|pygame.OPENGL
screen = pygame.display.set_mode((width, height), flags)
rotation = 0
square_size = 50
prev_time = time.time()
while True:
#Handle the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#Do computations and render stuff on screen
rotation += 1
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, width, 0, height, -1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslate(width/2.0, height/2.0, 0)
glRotate(rotation, 0, 0, 1)
glTranslate(-square_size/2.0, -square_size/2.0, 0)
glBegin(GL_QUADS)
glVertex(0, 0, 0)
glVertex(50, 0, 0)
glVertex(50, 50, 0)
glVertex(0, 50, 0)
glEnd()
pygame.display.flip()
#Timing code at the END!
curr_time = time.time()#so now we have time after processing
diff = curr_time - prev_time#frame took this much time to process and render
delay = max(1.0/target_fps - diff, 0)#if we finished early, wait the remaining time to desired fps, else wait 0 ms!
time.sleep(delay)
fps = 1.0/(delay + diff)#fps is based on total time ("processing" diff time + "wasted" delay time)
prev_time = curr_time
pygame.display.set_caption("{0}: {1:.2f}".format(title, fps))
在我的主循环中我有:
clock.tick_busy_loop(60)
pygame.display.set_caption("fps: " + str(clock.get_fps()))
但是,读数显示游戏正在以 62.5fps 的速度读取。
然后我尝试输入 clock.tick_busy_loop(57.5)
,这给了我 58.82...fps 的读数。当我设置 clock.tick_busy_loop(59)
时,我再次获得 62.5fps。看起来在 58.8fps 和 62.5fps 之间有一个我无法在这里克服的阈值。如何让我的游戏真正达到 60fps 运行?我主要在寻找这种控制,因为我执行的事件取决于音乐时间。
所以我根据上面的评论制作了一个简单的演示,它使用系统时间模块而不是 pygame.time 模块。你可以忽略 OpenGL 的东西,因为我只是想在屏幕上渲染一些简单的东西。最重要的部分是每帧结束时的时序代码,我在代码中已经注释了。
import pygame
import sys
import time
from OpenGL.GL import *
from OpenGL.GLU import *
title = "FPS Timer Demo"
target_fps = 60
(width, height) = (300, 200)
flags = pygame.DOUBLEBUF|pygame.OPENGL
screen = pygame.display.set_mode((width, height), flags)
rotation = 0
square_size = 50
prev_time = time.time()
while True:
#Handle the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#Do computations and render stuff on screen
rotation += 1
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, width, 0, height, -1, 1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslate(width/2.0, height/2.0, 0)
glRotate(rotation, 0, 0, 1)
glTranslate(-square_size/2.0, -square_size/2.0, 0)
glBegin(GL_QUADS)
glVertex(0, 0, 0)
glVertex(50, 0, 0)
glVertex(50, 50, 0)
glVertex(0, 50, 0)
glEnd()
pygame.display.flip()
#Timing code at the END!
curr_time = time.time()#so now we have time after processing
diff = curr_time - prev_time#frame took this much time to process and render
delay = max(1.0/target_fps - diff, 0)#if we finished early, wait the remaining time to desired fps, else wait 0 ms!
time.sleep(delay)
fps = 1.0/(delay + diff)#fps is based on total time ("processing" diff time + "wasted" delay time)
prev_time = curr_time
pygame.display.set_caption("{0}: {1:.2f}".format(title, fps))