2016-06-06 2 views
1

다음 코드는 내 roguelike 게임의 디스플레이를 렌더링합니다. 여기에는지도 렌더링이 포함됩니다._curses.error : add_wch()가 오류를 반환했습니다.

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) 

매번 오류가 발생합니다. 나는 화면에서 벗어날 것이기 때문에 그것이라고 생각하지만 height와 width 변수는 self.main.getmaxyx()에서 오는 것이므로 그렇게하지 말아야한다. 내가 뭘 놓치고 있니? Ubuntu 14.04에서 실행되는 Python 3.4.3이 중요합니다.

답변

2

예상되는 동작입니다. 파이썬은 ncurses를 사용하는데, 다른 구현체가이를 수행하기 때문에 이것을 수행한다. addch에 대한 manual page에서

:

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

파이썬의 저주

바인딩은 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) 
+0

스크롤하지 않고 오른쪽 아래 여백에 문자를 넣으려면 어떻게해야합니까? – Jonathanb

+0

조언이 없습니다. @ 토마스 디 키? 충돌없이 오른쪽 아래 여백에 어떻게 써야합니까? try/except 절에서 감싸는가? 그러나 합법적 인 저주 오류가 있다면 어떨까요? – Jonathanb

+0

'addch'는 윈도우가 널 포인터 인 경우 오류를 반환하지만 가능성은 희박합니다. 'addch'를 둘러싼 try/catch는 유일한 해결책으로 보인다. (오른쪽 하단 모서리에서만이 작업을하는 특별한 경우를 만들면 혼란이 생길 ​​것이다). –

관련 문제