我正在尝试制作一款基于文本的冒险游戏,我希望能够让文本自行删除。 例如,游戏将使用
print()
语句打印文本,然后在 5 秒后将其删除或对玩家隐藏。
我尝试查找其他人关于此问题的问题,但找不到任何信息。我不知道要使用什么命令或与之相关的任何内容,请帮忙。
import time
import sys
def print_and_replace(text, replacement_text, delay=5):
print(text, end='', flush=True)
time.sleep(delay)
sys.stdout.write('\r' + ' ' * len(text))
sys.stdout.write('\r')
sys.stdout.write(replacement_text)
sys.stdout.flush()
print_and_replace("This will be replaced in 5 seconds...", "This is the new text!", 5)