Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arrow-controlled movement in console with ncurses and Python
#!/usr/bin/env python
import curses
SHIFTS = {
curses.KEY_LEFT: (0, -1),
curses.KEY_UP: (-1, 0),
curses.KEY_RIGHT: (0, 1),
curses.KEY_DOWN: (1, 0),
}
def main():
curses.wrapper(loop)
def loop(stdscr):
x, y = 0, 0
stdscr.addstr(x, y, 'X')
curses.curs_set(0) # invisible cursor
while True:
c = stdscr.getch()
if c == ord('q'): break
stdscr.addstr(x, y, ' ')
dx, dy = SHIFTS.get(c, (0, 0))
x += dx ; y += dy
stdscr.addstr(x, y, 'X')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.