2

在 linux 机器上使用 pygame,不断加载新图像并显示它们会减慢程序的速度。

输入图像 (400x300) 采用 PPM 格式,以保持文件大小恒定 (360K) - 不影响 IO 并避免任何解压缩延迟。

它以每秒 50 帧开始,然后在大约 2 分钟后达到每秒 25 帧左右。

import pygame    
pygame.init()    
clock = pygame.time.Clock()
screen = pygame.display.set_mode((800, 600),pygame.FULLSCREEN)   
frame=1

while 1:

    image = pygame.image.load(str(frame)+".ppm")    

    screen.blit(image,(0,0))
    pygame.display.flip()

    clock.tick(240)

    frame=frame+1
    if(frame%10==0):
        print(clock.get_fps())

可以做些什么来保持帧速率更加一致?

很可能它与对需要进行垃圾收集的图像的旧引用有关。也许不吧。

无论如何,是否可以在不创建新对象并触发垃圾收集器或任何减慢系统速度的情况下连续加载图像?

4

2 回答 2

1

经过数周的思考,我想我终于弄清楚了你的问题是什么。出于某种原因,计算机必须记住image. 在闪烁的行之后,放

del image

我不完全确定,但它可能会起作用。

于 2012-12-30T22:34:23.103 回答
0
import pygame    
pygame.init()    
clock = pygame.time.Clock()
screen = pygame.display.set_mode((800, 600),pygame.FULLSCREEN)   
frame=1

global image
image = pygame.image.load(str(frame)+".ppm")
#this image can be used again and again
#you can also give ////del image//// but it will load from the first so it takes time but using again and again doesn't do so
while 1:
     screen.blit(image,(0,0))
     pygame.display.flip()

     clock.tick(240)

     frame=frame+1
     if(frame%10==0):
        print(clock.get_fps()):
于 2020-05-06T18:06:16.593 回答