我有以下代码为我的 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
andmvwaddch
routines put the characterch
into the given window at its current window position, which is then advanced. They are analogous toputchar
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/