_curses.error: add_wch() returned an error

_curses.error: add_wch() returned an error

我有以下代码渲染我的 roguelike 游戏的显示。它包括渲染地图。

  def render_all(self):
    for y in range(self.height):
      for x in range(self.width):
        wall = self.map.lookup(x,y).blocked
        if wall:
          self.main.addch(y, x, "#")
        else:
          self.main.addch(y, x, ".")
    for thing in self.things:
      draw_thing(thing)

每次都出错。我认为这是因为它离开了屏幕,但是高度和宽度变量来自 self.main.getmaxyx(),所以它不应该那样做,对吧?我错过了什么? Python 3.4.3 运行 in Ubuntu 14.04 应该很重要。

这是预期的行为。 Python 使用 ncurses,它这样做是因为其他实现也这样做。

manual pageaddch:

The addch, waddch, mvaddch and mvwaddch routines put the character ch into the given window at its current window position, which is then advanced. They are analogous to putchar in stdio(3). If the advance is at the right margin:

  • The cursor automatically wraps to the beginning of the next line.

  • At the bottom of the current scrolling region, and if scrollok is enabled, the scrolling region is scrolled up one line.

  • If scrollok is not enabled, writing a character at the lower right margin succeeds. However, an error is returned because it is not possible to wrap to a new line

Python 的 curses 绑定有 scrollok。要在不滚动的情况下添加字符,您可以使用 "false" 参数调用它,例如

self.main.scrollok(0)

如果不想滚动,可以使用 try/catch 块,如下所示:

import curses

def main(win):
  for y in range(curses.LINES):
    for x in range(curses.COLS):
      try:
        win.addch(y, x, ord('.'))
      except (curses.error):
        pass
      curses.napms(1)
      win.refresh()
  ch = win.getch()

curses.wrapper(main)