我正在编写蛇游戏...是的,我知道,多么原始。
我的问题如下:
我通过写stdscr.nodelay(1)
来启用nodelay(),然后通过写stdscr.timeout(100)
来将timeout()设置为100毫秒。发生了我的期望。一切都很好。
但是,当我在行开头添加#
来注释启用nodelay()的行时,将看到会发生什么,则在执行程序时没有任何变化。为什么?
import curses
from curses import textpad
from constants import *
def main(stdscr):
curses.curs_set(0)
# stdscr.nodelay(1)
stdscr.timeout(int(1000 / 15))
screen_height, screen_width = stdscr.getmaxyx()
if screen_height < 40 or screen_width < 168:
raise ValueError("Your terminal screen must be 40 rows and 168 columns minimum. ")
snake = [
[screen_height // 2, screen_width // 2]
]
stdscr.addch(snake[0][0], snake[0][1], "#")
direction = None
while True:
key = stdscr.getch()
if direction == curses.KEY_RIGHT or key == curses.KEY_RIGHT:
snake[0][1] += 1
direction = curses.KEY_RIGHT
if direction == curses.KEY_LEFT or key == curses.KEY_LEFT:
snake[0][1] -= 1
direction = curses.KEY_LEFT
if direction == curses.KEY_UP or key == curses.KEY_UP:
snake[0][0] -= 1
direction = curses.KEY_UP
if direction == curses.KEY_DOWN or key == curses.KEY_DOWN:
snake[0][0] += 1
direction = curses.KEY_DOWN
stdscr.clear()
stdscr.addch(snake[0][0], snake[0][1], "#")
curses.wrapper(main)
我希望发生的是,每次我执行stdscr.getch()时,该程序现在都将等待用户输入
但是实际上发生的是,程序仍然像nodelay一样被启用,因此它不等待任何输入