'How to remove a text in pygame? [duplicate]
I am creating a game in pygame which has some buttons and on every button pressed a text is displayed on screen on same position (co-ordinates) but the text is overlapping each other. How can I solve this problem?
Solution 1:[1]
If you have provided x and y coordinates to display your text like in this link: https://www.geeksforgeeks.org/python-display-text-to-pygame-window/
Then the thing you can do is make a variable that saves the coordinates where your text is displayed.
When you display your first message on the screen, simply add some value in that variable (in the y-axis if your message is displayed vertically) and the second message that will display will be going to be at the bottom of the previous message.
size = [800,600]
screen = pygame.display.set_mode(size)
black = pygame.color.Color('#000000')
font = pygame.font.Font('freesansbold.ttf', 40)
text1 = font.render('YOUR MESSAGE1', False, black)
text2 = font.render('MESSAGE2', False, black)
#Center
coordinates = (size[0]//2, size[1]//2)
window.fill(white)
window.blit(text, coordinates)
coordiantes = (coordiantes[0] ,coordiantes[1] +5 )#This 5 is the extra addition to y axis
window.blit(text, coordinates) #this will print below 5 pixel from previous message
You can also make a function or class
def setCoordinates(coordinates):
coordinates = (coordiantes[0] ,coordiantes[1] +5)
return coordinates
Use this function as
coordinates = (100,200)
coordinates = setCoordinates(coordinates)
print(coordinates)
Output
(100,205)
Again calling the function gives
coordinates = setCoordinates(coordinates)
print(coordinates)
Output
(100,210)
Hopefully, You get what you want from this post :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muhammad Umar Anzar |
