Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python _curses.error:add_wch()返回了一个错误_Python_Curses_Roguelike - Fatal编程技术网

Python _curses.error:add_wch()返回了一个错误

Python _curses.error:add_wch()返回了一个错误,python,curses,roguelike,Python,Curses,Roguelike,我有以下代码为我的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

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

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

在for
addch
中:

addch
waddch
mvaddch
mvwaddch
例程将 字符
ch
进入给定窗口的当前窗口 位置,然后是前进。它们类似于 stdio(3)中的putchar。如果预付款处于右边距:

  • 光标将自动换行到项目的开头 下一行

  • 在当前滚动区域的底部,如果
    滚动OK
    启用后,滚动区域将滚动 向上一行

  • 如果未启用scrollok,则在 右下角成功。但是,有一个错误是错误的 返回,因为无法换行到新的 线

Python的诅咒绑定已经完成。要在不滚动的情况下添加字符,可以使用“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)

如果我想在不滚动的情况下将一个字符放在右下角,我该怎么办?没有建议@Thomas Dickey?如何在不崩溃的情况下写入右下角?将其包装在try/except子句中?但是如果有另一个合法的诅咒错误呢?
addch
将在窗口为空指针时返回错误,但这不太可能。try/catch-around
addch
似乎是您唯一的解决方案(在右下角做一个特例会增加混乱)。好的,我正在尝试使用try/except,但它不起作用。下面是代码:try:self.main.addch(thing.y,thing.x,thing.disp,curses.color\u pair(self.color\u palete[thing.color]))除了_curses.error:pass它说_curses没有定义。但是当我在块外运行它时,它给我的错误消息是_curses.error:add_wch()返回ERR。错误也不起作用。