如何删除 pygame 中的文本?

How to remove a text in pygame?

我正在 pygame 中创建一个游戏,其中有一些按钮,在按下每个按钮时,文本会显示在屏幕上的相同位置(坐标),但文本相互重叠。 我该如何解决这个问题?

您需要在屏幕上填充背景图片或仅使用白色来擦除旧文本,然后再编写新文本。

如果您提供了 x 和 y 坐标来显示您的文本,如下所示 link:https://www.geeksforgeeks.org/python-display-text-to-pygame-window/

那么你可以做的就是制作一个变量来保存显示文本的坐标。

当您在屏幕上显示第一条消息时,只需在该变量[=42]中添加一些值 =](如果您的消息是垂直显示的,则在 y 轴上)并且 second message display 将位于 b 上一条消息的底部

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

你也可以做一个函数或者class

def setCoordinates(coordinates):
    coordinates = (coordiantes[0] ,coordiantes[1] +5)
    return coordinates

将此函数用作

coordinates = (100,200)
coordinates = setCoordinates(coordinates) 
print(coordinates)

输出

(100,205)

再次调用函数给出

coordinates = setCoordinates(coordinates) 
print(coordinates)

输出

(100,210)

希望你能从中得到你想要的post :)