Created
August 5, 2012 15:57
-
-
Save Xion/3265531 to your computer and use it in GitHub Desktop.
Arrow-controlled movement in console with ncurses and Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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