2016-10-16 18 views
-1

我試圖用Python3和curses創建一個roguelike。我已經把所有的東西都顯示出我想要的樣子,但是我在代碼中遇到了一個奇怪的錯誤。處理命令時有1個關鍵衝程延遲。所以,假設傳統的roguelike命令,按「k」應該讓你向右移動1個方格。第一次按下它時,它什麼也不做。第二次,它會移動。如果您按下「g」鍵,則不會移回左側,而是將第2個「k」處理完畢,並將「g」結束於「甲板上」。這是應該處理這些動作的循環。Python 3中的一個字符「滯後」Curses程序

def main_loop(self): 
#This will catch and handle all keystrokes. Not too happy with if,elif,elif or case. Use a dict lookup eventually 
    while 1: 
     self.render_all() 

     c = self.main.getch() 
     try: 
     self.keybindings[c]["function"](**self.keybindings[c]["args"]) 
     except KeyError: 
     continue 

這裏是字典查找我答應過自己我會在評論

self.keybindings = {ord("h"): {"function":self.move_object, 
           "args":{"thing":self.things[0], "direction":"North"}}, 
         ord('j'): {"function":self.move_object, 
           "args":{"thing":self.things[0], "direction":"South"}}, 
         ord('g'): {"function":self.move_object, 
           "args":{"thing":self.things[0], "direction":"West"}}, 
         ord('k'): {"function":self.move_object, 
           "args":{"thing":self.things[0], "direction":"East"}}, 
         ord('y'): {"function":self.move_object, 
           "args":{"thing":self.things[0], "direction":"NorthWest"}}, 
         ord('u'): {"function":self.move_object, 
           "args":{"thing":self.things[0], "direction":"NorthEast"}}, 
         ord('b'): {"function":self.move_object, 
           "args":{"thing":self.things[0], "direction":"SouthWest"}}, 
         ord('n'): {"function":self.move_object, 
           "args":{"thing":self.things[0], "direction":"SouthEast"}}, 
         ord('l'): {"function":self.look, "args":{"origin_thing":self.things[0],}}, 
         ord('q'): {"function":self.save_game, 
           "args":{"placeholder":0}}} 

最後使用,這裏是一個本應被稱爲move_object功能:

def move_object(self, thing, direction): 
"""I chose to let the Game class handle redraws instead of objects. 
I did this because it will make it easier should I ever attempt to rewrite 
this with libtcod, pygcurses, or even some sort of browser-based thing. 
Display is cleanly separated from obects and map data. 
Objects use the variable name "thing" to avoid namespace collision.""" 
    curx = thing.x 
    cury = thing.y 
    newy = thing.y + directions[direction][0] 
    newx = thing.x + directions[direction][1] 
    if not self.is_blocked(newx, newy): 
     logging.info("Not blocked") 
     thing.x = newx 
     thing.y = newy 

編輯清理代碼格式。

回答

0

我發現了這個問題,它沒有在我發佈的代碼中。它在我的render_all()函數中。在進行更改後,我需要添加對窗口的refresh()函數的調用。我必須說,我真的不喜歡詛咒!

相關問題