Question

After encountering a number of problems with the rendering of text in PyGame, I wonder, if there are any efficient ways to deal with it.

First approach:

store the texts already rendered in a classes 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.

then in the event handling, I just blit the variable:

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

Note: myfont is a pygame.font.SysFont(...)

Result: Framerate dropped from >29 (limit is 30) to roughly 20 to 22.

Second approach:

store just the text strings in a classes variables and render before blitting.

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

and then render just before the blitting:

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

Result: Comparable drop of Framerate

Third approach:

using GIMP, writing the text there and then store it in PNG. Result: No noticable change in FPS

So the question remains: Is there any way to efficiently handle text in PyGame or is it always the better solution to make it pictures? Did I just not understand a basic rule of text handling in PyGame? Did I overlook something?

How do you handle texts in your Py-Games?

Thanks in advance for all opinions and suggestions ;)

Patric

Was it helpful?

Solution

You can cache the text render. Try this demo: Render anti-aliased text on transparent surface in pygame

It will only re-render if dirty. ( changing .text or .aa ) by using properties.

Edit: You maybe be interested in the newer GUI module http://program.sambull.org/sgc/ for more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top