【问题标题】:Efficiently handle text in PyGame在 PyGame 中有效地处理文本
【发布时间】:2013-05-23 13:34:07
【问题描述】:

在遇到PyGame中文本渲染的一些问题后,我想知道有没有什么有效的方法来处理它。

第一种方法:

将已经呈现的文本存储在 self.variables 类中:

class SomeClass():
    def __init__(self):
        self.text1 = myfont.render("Text 1",0,self.colour)
        self.text2 = myfont.render("Text 2",0,self.colour)
        #etc.

然后在事件处理中,我只是对变量进行 blit:

while running:
screen.blit(SomeClass().text1,(x, y))

注意:myfont 是一个 pygame.font.SysFont(...)

结果:帧率从 >29(限制为 30)下降到大约 20 到 22。

第二种方法:

仅将文本字符串存储在类变量中并在传输之前渲染。

class SomeClass():
    def __init__(self):
        self.text1 = "Text 1 string"
        self.text2 = "Text 2 string"

然后在 blitting 之前渲染:

while running:
    #more code
    screen.blit(SomeClass().myfont.render(SomeClass().text1,0,SomeClass().colour)

结果:帧率下降

第三种方法:

使用 GIMP,在此处写入文本,然后将其存储在 PNG 中。 结果:FPS 没有明显变化

所以问题仍然存在:有什么方法可以有效地处理 PyGame 中的文本,还是让它成为图片总是更好的解决方案?我只是不了解 PyGame 中文本处理的基本规则吗?我是不是忽略了什么?

您如何处理 Py-Games 中的文本?

提前感谢所有意见和建议;)

帕特里克

【问题讨论】:

    标签: text pygame blit


    【解决方案1】:

    您可以缓存文本渲染。试试这个演示:Render anti-aliased text on transparent surface in pygame

    只有脏了才会重新渲染。 (通过使用属性更改 .text.aa )。

    编辑:您可能对更新的 GUI 模块 http://program.sambull.org/sgc/ 感兴趣。

    【讨论】:

    • 谢谢!这是一个非常有趣的方法。我会试一试。
    • 现在想知道其他的东西...“打印”命令不是也消耗大量CPU吗...?在我删除打印 FPS 的命令后,游戏的运行速度突然变得更快了。
    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 2014-04-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多