Python 3 Curses 程序中的一个字符 "lag"
One Character "lag" in Python 3 Curses Program
我正在尝试使用 Python3 和诅咒创建一个 roguelike。我已经按照我想要的方式显示了所有内容,但是我在代码中遇到了一个奇怪的错误。在处理命令时有 1 个击键延迟。因此,假设使用传统的 roguelike 命令,按 "k" 应该会向右移动 1 个方格。第一次按下它时,它什么也不做。第二次,它会移动。如果您随后按 "g",您不会向左移动,而是会处理第二个 "k" 并且 "g" 结束 "on deck"。这是应该处理移动的循环。
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() 函数中。在进行更改后,我需要添加对 window 的 refresh() 函数的调用。不得不说,我真的不喜欢骂人!
我正在尝试使用 Python3 和诅咒创建一个 roguelike。我已经按照我想要的方式显示了所有内容,但是我在代码中遇到了一个奇怪的错误。在处理命令时有 1 个击键延迟。因此,假设使用传统的 roguelike 命令,按 "k" 应该会向右移动 1 个方格。第一次按下它时,它什么也不做。第二次,它会移动。如果您随后按 "g",您不会向左移动,而是会处理第二个 "k" 并且 "g" 结束 "on deck"。这是应该处理移动的循环。
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() 函数中。在进行更改后,我需要添加对 window 的 refresh() 函数的调用。不得不说,我真的不喜欢骂人!