2015-10-03 77 views
1

這是我的代碼,我是一個初學者,我一直在四處查看我已經看過至少5-10個關於此的帖子,他們不是真的幫助我,因爲我無法理解代碼。我試圖檢測某個鍵被按下(Python)

在我的代碼我嘗試一個真正的基本出發屏幕遊戲和我想要做的是如何檢測E或在啓動畫面時按下S,

print (" _____________________________________________ ") 
print ("|            |") 
print ("|            |") 
print ("|   The Chronicles     |") 
print ("|    Of Game      |") 
print ("|            |") 
print ("|            |") 
print ("|            |") 
print ("|            |") 
print ("|_____________________________________________|") 
print ("|            |") 
print ("|  START    EXIT    |") 
print ("|  (S)    (E)    |") 
print (" \___________________________________________/ ") 
+1

這是依賴於操作系統的,所以說您所使用的操作系統。 –

回答

1

我用殘培,如果你想使用,下載PY-殘培:

https://github.com/joeyespo/py-getch

的命令來安裝:蟒蛇的setup.py安裝

示例代碼:

from getch import getch, pause 

# game loop 
while(True): 
    key = getch() 
    if(key == 'S'): 
     print('key S pressed...') 
    elif(key == 'E'): 
     print('key E pressed...') 

key pressed

+0

感謝我即將嘗試這一個 – Ryan

+0

我試過這個,它的工作原理。但是,它打破了終端。所以我必須重新設置才能恢復一切。而且,使用try時它不會捕獲'KeyboardInterrupt'外。 – muammar