如何使用curses按下最后一个箭头键?
我正在编写一个 使用诅咒的 Python 蛇游戏,但在控制蛇时遇到一些问题,我当前的代码用于控制蛇的内容放置在主循环内,如下所示:
while True:
char = screen.getch()
if char == 113: exit() # q
elif char == curses.KEY_RIGHT: snake.update(RIGHT)
elif char == curses.KEY_LEFT: snake.update(LEFT)
elif char == curses.KEY_UP: snake.update(UP)
elif char == curses.KEY_DOWN: snake.update(DOWN)
else snake.update()
time.sleep(0.1)
但是,代码似乎将按下的键视为 que(因此蛇在用完箭头按下时会停止),而我实际上希望它检索最后按下的箭头键。
如何检索最后按下的箭头键?
I'm writing a Python snake game using curses, but am having some trouble controlling the snake, my current code for controlling the snake is placed inside the main loop and looks like this:
while True:
char = screen.getch()
if char == 113: exit() # q
elif char == curses.KEY_RIGHT: snake.update(RIGHT)
elif char == curses.KEY_LEFT: snake.update(LEFT)
elif char == curses.KEY_UP: snake.update(UP)
elif char == curses.KEY_DOWN: snake.update(DOWN)
else snake.update()
time.sleep(0.1)
However the code seems to treat the keys pressed as a que (so the snake will stop when it runs out of arrow-presses), whereas I actually want it to retrieve the last arrow key that was pressed.
How can I retrieve the last arrow key that was pressed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置 screen.nodelay(1):
Set
screen.nodelay(1)
: