python - _curses.error : add_wch() returned an error

标签 python curses roguelike

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

  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(),所以它不应该那样做,对吧?我错过了什么?在 Ubuntu 14.04 中运行的 Python 3.4.3 应该很重要。

最佳答案

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

manual page对于 addch:

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 绑定(bind)有 scrollok .要在不滚动的情况下添加字符,您可以使用“false”参数调用它,例如

self.main.scrollok(0)

如果你不想滚动,你可以使用 try/catch block ,像这样:

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)

关于python - _curses.error : add_wch() returned an error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37648557/

相关文章:

python - 隐式地将类转换为 bool 是惯用的做法吗?

python - 将 24 小时制转换为 12 小时制

python - Python 中的链表

c - 将 stdin 与 ncurses 一起使用

python - urwid:使光标不可见

c - ncurses 不解释键

Python Pandas Group By 错误 'Index' 对象没有属性 'labels'

scala - Scala中基于组件的实体系统

haskell - 当我尝试返回列表时出错

haskell - 将 map 约束表示为 ADT