如何停止python input()函数以记住以前按下的键?

2024-10-01 15:37:27 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我正在写一个键盘练习游戏,你需要按特定的键才能赢。最后,玩家使用input()函数输入他们的名字(用于记分牌)

import keyboard

...
print(key_that_needs_to_be_pressed)
keyboard.wait(key)
if keyboard.is_pressed(key):
    ...

print('Game Over\nScore= ' + str(score))
user=input('Player:')
with open('scores.txt', 'a') as f:
    f.write(user+': ' + str(correct) + ' points\n')

输出为:

r
t
space
f
d
b
d
w
Game Over
Score= 8
Player: rt fdbdw

当用户需要输入自己的名字时,有没有办法去掉rt-fdbdw部分 我尝试使用os.system('cls')并模拟在input()部分之前按backspace,但都不起作用

编辑:

我已尝试刷新输入流like this,但没有效果。另一个建议是this,但我使用windows


Tags: keygameinput键盘this名字overplayer

热门问题