[英]One Character "lag" in Python 3 Curses Program
我正在嘗試使用 Python3 和 curses 創建一個 roguelike。 我已經按照我想要的方式顯示了所有內容,但是我在代碼中遇到了一個奇怪的錯誤。 處理命令有 1 個擊鍵延遲。 因此,假設使用傳統的 roguelike 命令,按“k”應該將您向右移動 1 個方格。 第一次按下它時,它什么也不做。 第二次,它會移動。 如果然后按“g”,則不會向左移動,而是處理第二個“k”並且“g”最終“在甲板上”。 這是應該處理移動的循環。
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
我發現了問題,它不在我發布的代碼中。 它在我的 render_all() 函數中。 在進行更改后,我需要添加對窗口的 refresh() 函數的調用。 我必須說,我真的不喜歡詛咒!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.