How to remove/replace text in pygame

心不动则不痛 提交于 2019-11-29 07:30:28

You have to erase the old text first. Surfaces created by Font.render are ordinary surfaces. Once a Surface is blit, its contents become part of the destination surface, and you have to manipulate the destination surface to erase whatever was blit from the source surface.

One way to erase the destination surface is to blit a background surface onto it. The background surface is what the destination surface would look like without anything like text or sprites on it. Another way is to fill the surface with a solid color:

# 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))

There can be an other solution, even if it's not very different. The previous answer erases all the screen, but you can erase just your text. If it's written on an image, you will replace just a part of the image, by getting the text size and blitting the corresponding image part (pygame.surface.subsurface function). Or if it's not, you can just fill a part of the screen. In this case you will just erase your text.

Dazwenn

You could also overwrite your text.
Like this:

label = myfont.render("Text", 0, (255,255,0))
screen.blit(label, (100, 100))
if x: //Parameter you check before overwrite
    label = myfont.render("Text", 0, BACKGROUND_COLOR)
    screen.blit(label, (100, 100))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!