How to remove/replace text in pygame [closed]
Asked Answered
M

5

13

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..?

Multiplicand answered 6/5, 2012 at 3:28 Comment(0)
H
12

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))
Houselights answered 6/5, 2012 at 5:25 Comment(1)
To erase only the text area use: screen.fill(pygame.Color("black"), (0, 0, 110, 40))Recor
B
2

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))
Bromide answered 20/5, 2015 at 10:34 Comment(0)
W
1

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.

Where answered 22/4, 2013 at 9:52 Comment(0)
T
0

For me creating a black rectangle and only calling it when I have a specific task met worked: here is an example,

I go through a value in a variable:

for j in str(value):
    
    # Here I check if my target is met (if I clicked a button)

    if event.type == mousepress and event.button == leftclick:
        pygame.draw.rect(clicker, "black", pygame.Rect(650, 120, 350, 100))
        break
    
    # I then update the blit and font or text or whatever

    font = pygame.font.SysFont("Times New Roman", 35)
    numberdisplay = font.render('upgrade: ' + j, True, 'pink')
    clicker.blit(display,(650 - display.get_width() // 2, 150 - display.get_height() // 2))
Tony answered 21/5, 2023 at 20:58 Comment(0)
S
-1

You should fill your window before update text:

############################
screen.fill((255, 255, 255))
############################

screen.blit(font.render(textVar, True, (255, 255, 0)), (10, 10))
pygame.display.update()

If you would like to don't fill all Surface, you can create rect in this text:

textVar = ""

screen = pygame.display.set_mode((500, 500))
rect = pygame.Rect(0, 0, 0, 0)
font = pygame.font.Font(None, 36)

while True:
    ###################################################################
    pygame.draw.rect(screen, (255, 255, 255), rect) # filling this text
    rect = font.render(textVar, True, (255, 255, 0)) # get surface with text
    rect = screen.blit(rect, (10, 10)) # get rectangle of your text
    ###################################################################

    pygame.display.update() # Update screen; 
Spermatophyte answered 19/4 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.