Do not create the pygame.font.Font
object in every frame and do not render the text in every frame. Create the text Surface once at the begin of the program or in the constructor (__init__
) of a class. Just blit
the text Surface in every frame:
At initialization:
font = pygame.font.SysFont("Comic Sans MS", 180)
color = (0,60,20)
text_surface = font.render("Title", False, color)
Once per frame:
screen.blit(text_surface, (480,0))
If the text is dynamic, it cannot even be pre-rendered. However, the most time-consuming is to create the pygame.font
object. At the very least, you should avoid creating the font in every frame.
In a typical application you don't need all permutations of fonts and font sizes. You just need a couple of different font
objects. Create a number of fonts at the beginning of the application and use them when rendering the text. For Instance:
fontComic40 = pygame.font.SysFont("Comic Sans MS", 40)
fontComic180 = pygame.font.SysFont("Comic Sans MS", 180)
# [...]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…