3

我使用 python 的 pyglet 包编写了一个简单的图像显示。在我的Linux 笔记本电脑上,代码按我的预期运行,每秒显示恒定的 60 帧。

然而,在我的 Windows 7 桌面(@Xi的 GeForce GTX 550 Ti 相当新)上,帧速率非常非常低(约 10 FPS 或更低)。但是,我不认为这是硬件限制,因为鼠标拖动事件会大大加快帧速率(60 FPS 或更高)。

为什么当我不拖动鼠标时,我在 Windows 上的帧速率如此之低,而当我拖动鼠标时却如此之快?

这是我用来产生这种行为的简化代码:

import pyglet
from pyglet.window import mouse

image_1 = pyglet.resource.image('1.png')
image_2 = pyglet.resource.image('2.png')

fps_display = pyglet.clock.ClockDisplay()
image_x, image_y = 0, 0
frame = 0

window = pyglet.window.Window(image_1.width, image_2.height)

@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
    global image_x, image_y
    if buttons == mouse.LEFT:
        image_x += dx
        image_y += dy

@window.event
def on_draw():
    global frame
    frame += 1
    window.clear()
    if frame%2 == 0:
        image = image_1
    else:
        image = image_2
    image.blit(x=image_x, y=image_y,
               height=image.height,
               width=image.width)
    fps_display.draw()

if __name__ == '__main__':
    pyglet.app.run()

'1.png' 和 '2.png' 具有相同的像素尺寸,它们只是不同的图像,所以我可以看到帧翻转。我正在使用 python 2.7.2 和 pyglet 版本 1.2dev。我很乐意添加任何有用的附加信息。

4

1 回答 1

1

自从我做任何 pyglet 以来已经有一段时间了,但是回顾一些旧代码,我发现这一切似乎都使用了 pyglet 时钟设置

 clock.schedule_interval(self.update,1.0/75.0)   
 clock.set_fps_limit(75)

在子类 pyglet Window 中控制更新速率(其中 update 是一种窗口方法,它通过时间步参数推进游戏世界,并使窗口无效)。我认为 pyglet 中没有任何东西可以特别保证定期的“ticker”更新率。

于 2012-05-09T23:27:27.770 回答