如何删除在 pygame 中使用 event.unicode 的字符串中添加的元素?

how to remove elements added in a string with event.unicode in pygame?

我的程序中有一个函数,玩家可以输入他的名字,然后该名字会出现在屏幕上。但是有没有办法,例如当玩家按下退格键时,添加到字符串中的最后一个字母被删除?

就是这个函数,我想我不需要再写关于玩家名字的其余代码了:

def enter_player_name(self):
    player_name_screen = True
    while player_name_screen:
        win.blit(playerNameImg[self.namecount//1], (self.x_0, self.y_0))
        self.namecount += 1
        if (self.namecount + 1) >= 8:
            self.namecount = 0
        for event in pg.event.get():
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_RETURN:
                    print(self.name)
                    player_name_screen = False
                    self.display = GAME_SCREEN
                else:
                    self.name += event.unicode
                    self.text_name = game_font.render(self.name, True, BLANC)
        win.blit(self.text_name, (500, 500))
        pg.display.update()

感谢您的帮助!

按下退格键时删除文本的最后一个字符 (self.name = self.name[:-1]) 并呈现新文本:

if event.key == pg.K_RETURN:
    print(self.name)
    player_name_screen = False
    self.display = GAME_SCREEN

elif event.key == pg.K_BACKSPACE:
    self.name = self.name[:-1]
    self.text_name = game_font.render(self.name, True, BLANC)

else:
    self.name += event.unicode
    self.text_name = game_font.render(self.name, True, BLANC)