3

我是使用 Pyglet 的新手,我编写了一个小程序,可以在屏幕上移动一个球。现在我很难建立 60 fps 的稳定帧速率。虽然 Pyglet 应该与我的显示器的 60Hz 刷新率同步,但 Pyglet 将我的 fps 设置为我刷新率的一半(例如,当 60Hz、30 fps 时)。我的代码中是否有问题导致这种情况?

import pyglet
import physicalobject
import random
from pyglet.window import mouse

pyglet.resource.path = ['./resources']
pyglet.resource.reindex()

ball_image = pyglet.resource.image("ball2.png")

#sets clock format
fps_display = pyglet.clock.ClockDisplay(format='%(fps).2f fps')

def center_image(image):
    image.anchor_x = image.width/2
    image.anchor_y = image.height/2

center_image(ball_image)
ball = physicalobject.PhysicalObject(img=ball_image, x = 400, y = 300)
ball.scale = .2
ball.velocity_x = random.randint(-4,4)*150
ball.velocity_y = random.randint(-4,4)*150

#Calls update function to set new ball position based on velocity
def update(dt):
     ball.update(dt)

@window.event
def on_mouse_drag(x, y, dx, dy, button, modifiers):
     ball.x = x
     ball.y = y
     ball.velocity_x = dx * 20
     ball.velocity_y = dy * 20

@window.event
def on_draw():
     window.clear()
     ball.draw()
     fps_display.draw()

def main():
     pyglet.clock.schedule_interval(update, 1/120.0)
     pyglet.app.run()

if __name__ == '__main__':
     main()
4

2 回答 2

1
import pyglet
from time import time, sleep

class Window(pyglet.window.Window):
    def __init__(self, refreshrate):
        super(Window, self).__init__(vsync = False)
        self.frames = 0
        self.framerate = pyglet.text.Label(text='Unknown', font_name='Verdana', font_size=8, x=10, y=10, color=(255,255,255,255))
        self.last = time()
        self.alive = 1
        self.refreshrate = refreshrate

    def on_draw(self):
        self.render()

    def render(self):
        self.clear()
        if time() - self.last >= 1:
            self.framerate.text = str(self.frames)
            self.frames = 0
            self.last = time()
        else:
            self.frames += 1
        self.framerate.draw()
        self.flip()

    def on_close(self):
        self.alive = 0

    def run(self):
        while self.alive:
            self.render()
            event = self.dispatch_events()
            sleep(1.0/self.refreshrate)

win = Window(23) # set the fps
win.run()

请注意缺少时钟功能。另外,尝试设置vsync = True和删除sleep(1.0/self.refreshrate),这会将刷新率锁定到您的显示器。

另外,请注意,我不使用pyglet.app.run()锁定渲染过程,self.dispatch_events()而是调用..它实际上并没有做任何事情,除了让图形“轮询”并继续前进,没有它.. pyglet 等待轮询发生,这pyglet.app.run()通常会。

于 2013-02-06T18:06:14.343 回答
1

Pyglet 在某些系统上根本无法正确处理它,您必须禁用应用程序窗口的 vsync 才能使其工作。这是一个示例脚本,您可以运行它来了解它的工作原理:

import pyglet

# Show FPS
fps = pyglet.clock.ClockDisplay()

# The game window
class Window(pyglet.window.Window):
    def __init__(self):
        super(Window, self).__init__(vsync = False)
        # Run "self.update" 128 frames a second and set FPS limit to 128.
        pyglet.clock.schedule_interval(self.update, 1.0/128.0)
        pyglet.clock.set_fps_limit(128)

    # You need the dt argument there to prevent errors,
    # it does nothing as far as I know.
    def update(self, dt): 
        pass

    def on_draw(self):
        pyglet.clock.tick() # Make sure you tick the clock!
        self.clear()
        fps.draw()

# Create a window and run
win = Window()
pyglet.app.run()
于 2012-08-30T14:38:06.790 回答