The code below allows you to navigate a small grid on the screen using the arrow keys, placing ".". where you studied or were near. Despite the fact that I have an update to the first getch (to get the key stroke), the screen does not display anything until you leave your starting position. Should you update addstr immediately after that, after which getch waits after that? I even tried adding stdscr.refresh (), but that didn't help either. How to make the screen refresh immediately before waiting for the first keystroke?
import curses def start(stdscr): curses.curs_set(0) movement = curses.newpad(10, 10) cur_x, cur_y = 5, 5 while True: movement.addstr(cur_y, cur_x, '@') for (x_off, y_off) in [(-1,0),(1,0),(0,-1),(0,1)]: movement.addstr(cur_y + y_off, cur_x + x_off, '.') movement.refresh(1, 1, 0, 0, 7, 7) #Nothing is displayed until after the first key-stroke key_stroke = stdscr.getch() move_attempt = False if 0 < key_stroke < 256: key_stroke = chr(key_stroke) elif key_stroke == curses.KEY_UP and cur_y > 1: cur_y -= 1 elif key_stroke == curses.KEY_DOWN and cur_y < 8: cur_y += 1 elif key_stroke == curses.KEY_LEFT and cur_x > 1: cur_x -= 1 elif key_stroke == curses.KEY_RIGHT and cur_x < 8: cur_x += 1 else: pass if __name__ == '__main__': curses.wrapper(start)
python ncurses python-curses
Dan
source share