Skip to main content
 首页 » 编程设计

python之在 python 中使用 pyglet,如果我拖动鼠标,为什么我的帧速率会加快

2025年05月04日9thcjp

我使用 python 的 pyglet 包写了一个简单的图像显示。在我的 Linux laptop ,代码按照我的预期工作,每秒显示恒定的 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。我很乐意添加任何有用的附加信息。

请您参考如下方法:

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

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

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