我正在写一个Python snake game using curses,但是在控制蛇方面遇到了一些麻烦,我目前控制蛇的代码放在主循环中,看起来像这样:
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)
然而,代码似乎将按下的键视为一个曲目(因此当箭头按下时蛇将停止),而我实际上希望它检索被按下的最后一个箭头键。
如何检索已按下的最后一个箭头键?
答案 0 :(得分:3)
设置screen.nodelay(1)
:
screen.nodelay(1)
while True:
char = screen.getch()
if char == 113: break # 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)