0

私のメインループには次のものがあります:

clock.tick_busy_loop(60)
pygame.display.set_caption("fps: " + str(clock.get_fps()))

ただし、読み取り値によると、ゲームは 62.5 fps で読み取っています。次に を入力しようとしたところclock.tick_busy_loop(57.5)、58.82...fps が表示されました。設定するclock.tick_busy_loop(59)と、再び62.5fpsになります。58.8fps と 62.5fps の間に、ここでは乗り越えられないしきい値があるようです。ゲームを実際に 60fps で実行するにはどうすればよいですか? 音楽のタイミングに依存するイベントを実行するので、主にこの種のコントロールを探しています。

4

1 に答える 1

1

そこで、上記のコメントに基づいて、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))
于 2016-10-17T02:26:27.920 に答える