什麼是實現鍵盤處理的好方法?在任何一種語言,當我寫一個鍵盤交互程序(如俄羅斯方塊遊戲),我最終有一些代碼,看起來像這樣:有效的鍵盤輸入處理
for event in pygame.event.get():
if event.type == KEYDOWN:
if False: pass #make everything an elif
elif rotating: pass
elif event.key == K_q:
elif event.key == K_e:
elif event.key == K_LEFT:
curpiece.shift(-1, 0)
shadowpiece = curpiece.clone(); setupshadow(shadowpiece)
elif event.key == K_RIGHT:
curpiece.shift(1, 0)
shadowpiece = curpiece.clone(); setupshadow(shadowpiece)
(縮短)。我不喜歡這一點,因爲這必須在我的主循環中進行,並且會與程序的所有部分混淆。這也使得不可能擁有用戶配置屏幕,他們可以在哪裏更改哪些鍵映射到哪個操作。有沒有一種很好的模式來使用某種形式的函數回調?
另請參閱http://stackoverflow.com/questions/292095/polling-the-keyboard-in-python – 2008-11-23 12:40:17