我在Pygame中编写了一个简单的自上而下的rpg,我发现它的运行速度很慢....尽管我不希望python或pygame与使用C/C ++或C/C ++等编译语言制作的游戏的FPS相匹配像Java这样的事件字节编译对象,但是pygame的当前FPS仍然是15.我尝试渲染16色位图,而不是PNG或24位图,这稍微提高了速度,然后在无奈中,我将所有内容都切换为黑白单色位图,使FPS达到35.但是还不止于此.现在,根据我读过的大多数游戏开发书籍,要使用户对游戏图形完全满意,二维游戏的FPS至少应为40,那么有没有办法提高pygame的速度?
I am writing a simple top down rpg in Pygame, and I have found that it is quite slow.... Although I am not expecting python or pygame to match the FPS of games made with compiled languages like C/C++ or event Byte Compiled ones like Java, But still the current FPS of pygame is like 15. I tried rendering 16-color Bitmaps instead of PNGs or 24 Bitmaps, which slightly boosted the speed, then in desperation , I switched everything to black and white monochrome bitmaps and that made the FPS go to 35. But not more. Now according to most Game Development books I have read, for a user to be completely satisfied with game graphics, the FPS of a 2d game should at least be 40, so is there ANY way of boosting the speed of pygame?
推荐答案将Psyco用于python2:
Use Psyco, for python2:
import psyco psyco.full()此外,启用双缓冲.例如:
Also, enable doublebuffering. For example:
from pygame.locals import * flags = FULLSCREEN | DOUBLEBUF screen = pygame.display.set_mode(resolution, flags, bpp)如果不需要,也可以关闭Alpha:
You could also turn off alpha if you don't need it:
screen.set_alpha(None)不要每次都翻转整个屏幕,而是要跟踪已更改的区域并仅对其进行更新.例如,大致如下所示(主循环):
Instead of flipping the entire screen every time, keep track of the changed areas and only update those. For example, something roughly like this (main loop):
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)大多数(全部?)绘图函数返回一个矩形.
Most (all?) drawing functions return a rect.
您还可以仅设置一些允许的事件,以更快地处理事件:
You can also set only some allowed events, for more speedy event handling:
pygame.event.set_allowed([QUIT, KEYDOWN, KEYUP])此外,我不会为手动创建缓冲区而烦恼,也不会使用HWACCEL标志,因为在某些设置中遇到了问题.
Also, I would not bother with creating a buffer manually and would not use the HWACCEL flag, as I've experienced problems with it on some setups.
使用此工具,我已经在一个小型2d平台上获得了相当不错的FPS和平滑度.
Using this, I've achieved reasonably good FPS and smoothness for a small 2d-platformer.
更多推荐
有什么方法可以加快Python和Pygame的速度吗?
发布评论