当没有按下键时,Python会诅咒addstr()错误

时间:2019-07-21 19:22:09

标签: python python-curses

我正在使用curses库在python中为终端创建Link to code游戏。每当未按任何键时, for (const [puppet, difficulty] of puppets) { for (let i = 0; i < 3; i++) { runPuppet(puppet, { difficulty, newGame: !!i }); } } 都会返回错误。

这似乎是因为在这些帧期间光标不在屏幕的右下角(但在右下角),但是我根本不知道为什么它会移动。我正在打印的画布总是相同的尺寸(它完全由渲染Apple或Basket对象时替换的空间组成)。我尝试减小画布大小,但光标仍然移至该角。我也尝试设置stdscr.addstr(),光标似乎在篮子的后面,但我的日志记录证明它仍然在那个角落。

使用curses的文件:

curses.leaveok(True)

(上面的代码运行良好,但是在import random import time import curses from apple_season.basket import Basket from apple_season.apple import Apple from apple_season.coords import Canvas def main(stdscr): curses.curs_set(1) # so i can see where the cursor is dims = [curses.COLS - 1, curses.LINES - 1] # pylint: disable=no-member stdscr.nodelay(True) stdscr.leaveok(True) key="" stdscr.clear() canvas = Canvas(*dims) basket = Basket(canvas) apples = [] i = 0 def finished_apples(): if len(apples) <= 100: return False else: for apple in apples: if not apple.has_fallen: return False return True while not finished_apples(): if len(apples) <= 100: # don't make more if there are already 100 # decide whether or not to create new apple (1/100 chance per frame) num = random.randint(0, 100) if num == 25: apples.append(Apple(canvas)) try: key = stdscr.getkey() stdscr.clear() # pick up keyboard inputs # quit option if str(key) == "q": break # right arrow elif str(key) == "KEY_RIGHT": basket.move('right') # left arrow elif str(key) == "KEY_LEFT": basket.move('left') except Exception: pass # render objects - alters canvas to display them for apple in apples: if apple.has_fallen: apple.render() else: if '.0' not in str(i / 2): # check if i is even (drop every other frame) apple.fall() apple.render() basket.render() try: stdscr.addstr(canvas.display) except Exception: pass stdscr.refresh() i += 1 time.sleep(0.01) if __name__ == "__main__": curses.wrapper(main) 不起作用时(即在没有按键的情况下,它什么也不做)

有趣的是,当它只是篮子或苹果时,不会发生这种情况。

要查看所有代码:this

1 个答案:

答案 0 :(得分:0)

我将stdscr.clear()放在try和except块中,这使得该代码仅在按下键时才执行,因此由于我试图一次显示多个帧而使终端溢出。 / p>