2016-11-15 76 views
0

我在python中制作了一个控制台游戏,我想在打印故事时禁用控制台。它看起来像这样:Python在程序中禁用控制台?

print("First line of story") 
time.sleep(2) 
print("Second line of story") 
time.sleep(2) 

等等......

所以我的问题是,虽然它写的故事,玩家可以通过控制台输入和陷入困境。可以以某种方式禁用打字吗?

回答

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通话后回荡,则可以用TCSAFLUSH替代过去TCSADRAIN

termios模块的文档可以找到here,这也是该示例的来源。