有什么方法可以加快Python和Pygame的速度吗?

编程入门 行业动态 更新时间:2024-10-11 23:19:03
本文介绍了有什么方法可以加快Python和Pygame的速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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的速度吗?

本文发布于:2023-10-26 17:30:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   速度   方法   Python   Pygame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!