我对 pygame 还很陌生,我遇到了我无法找到答案的第一个树桩。
在 blitting 文本之后,然后更改相同变量的字符串,游戏不是用新文本替换原始文本,而是将两个文本重叠..?
最佳答案
您必须先删除旧文本。由 Font.render
创建的曲面是普通曲面。曾经Surface
是 blit,它的内容成为目标表面的一部分,您必须操纵目标表面以删除源表面上 blit 的任何内容。
删除目标表面的一种方法是将背景表面 blit 到它上面。背景表面是目标表面没有任何类似文本或 Sprite 的样子。另一种方法是用纯色填充表面:
# pygame initialization goes here
screen = pygame.display.get_surface()
font = pygame.font.Font(None, 40)
font_surface = font.render("original", True, pygame.Color("white"));
screen.blit(surface, (0, 0))
screen.fill(pygame.Color("black")) # erases the entire screen surface
font_surface = font.render("edited", True, pygame.Color("white"));
screen.blit(surface, (0, 0))
关于text - 如何删除/替换pygame中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467863/