对python2使用Psyco:
import psyco
psyco.full()
另外,启用双缓冲。例如:
from pygame.locals import *
flags = FULLSCREEN | DOUBLEBUF
screen = pygame.display.set_mode(resolution, flags, bpp)
如果不需要,也可以关闭Alpha:
screen.set_alpha(None)
不必每次都翻转整个屏幕,而是要跟踪已更改的区域并仅对其进行更新。例如,大致如下所示(主循环):
events = pygame.events.get()
for event in events:
# deal with events
pygame.event.pump()
my_sprites.do_stuff_every_loop()
rects = my_sprites.draw()
activerects = rects + oldrects
activerects = filter(bool, activerects)
pygame.display.update(activerects)
oldrects = rects[:]
for rect in rects:
screen.blit(bgimg, rect, rect)
您还可以仅设置一些允许的事件,以更快地处理事件:
pygame.event.set_allowed([QUIT, KEYDOWN, KEYUP])
另外,我不会手动创建缓冲区,也不会使用HWACCEL标志,因为在某些设置中遇到了问题。
使用此工具,我已经为小型2d平台实现了相当不错的FPS和平滑度。