Skip to content

Instantly share code, notes, and snippets.

@insom
Created October 4, 2013 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insom/6824537 to your computer and use it in GitHub Desktop.
Save insom/6824537 to your computer and use it in GitHub Desktop.
Manually control a Python Turtle with the keyboard.
#!/usr/bin/env python
# Adapted from David Llewellyn-Jones's code
# http://www.flypig.co.uk/?page=list&list_id=363&list=blog
# By Aaron Brady
# http://insom.me.uk/
import turtle as t
import sys
import tty
import termios
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
ch = ' '
print "Ready"
while ch != 'q':
ch = getch()
if ch == 'w':
t.forward(10)
elif ch == 's':
t.backward(10)
elif ch == 'a':
t.left(10)
elif ch == 'd':
t.right(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment