我在python中制作了一个控制台游戏,我想在打印故事时禁用控制台。它看起来像这样:
print("First line of story")
time.sleep(2)
print("Second line of story")
time.sleep(2)
等等......
所以我的问题是玩家在编写故事时可以输入和搞砸控制台。我可以以某种方式禁用输入吗?
答案 0 :(得分:0)
如果您使用的是Unix,则可以禁用这样的回显:
import sys
import termios
import time
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] &= ~termios.ECHO
termios.tcsetattr(fd, termios.TCSADRAIN, new)
print("First line of story")
time.sleep(2)
print("Second line of story")
time.sleep(2)
termios.tcsetattr(fd, termios.TCSADRAIN, old)
如果您不希望在上一次tcsetattr
通话后回复被抑制的输入,则可以将TCSADRAIN
替换为TCSAFLUSH
。
可以找到termios模块的文档here,这也是取自示例的地方。