Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove/replace text in pygame

Tags:

text

pygame

blit

I'm fairly new to pygame and ive hit my first stump which I cannot find an answer for..

After blitting text, then changing the string for the same variable, the game instead of replacing the original text with the new, overlaps the two texts..?

like image 843
Krazyhornet Avatar asked May 06 '12 03:05

Krazyhornet


1 Answers

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))
like image 90
0eggxactly Avatar answered Oct 06 '22 01:10

0eggxactly