2016-11-15 61 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,這也是該示例的來源。