Skip to content

Instantly share code, notes, and snippets.

@Xion
Created August 5, 2012 15:57
Show Gist options
  • Save Xion/3265531 to your computer and use it in GitHub Desktop.
Save Xion/3265531 to your computer and use it in GitHub Desktop.
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