Skip to content

Instantly share code, notes, and snippets.

@imranrbx
Created December 29, 2018 12:31
Show Gist options
  • Save imranrbx/b39c09902b3500699bfe225d3fda778c to your computer and use it in GitHub Desktop.
Save imranrbx/b39c09902b3500699bfe225d3fda778c to your computer and use it in GitHub Desktop.
Handling Turtle Movement with Keyboard
from turtle import Turtle, Screen
screen = Screen()
screen.setup(400, 400)
screen.screensize(380, 380)
screen.tracer(0)
t = Turtle()
t_right = 0.0
t.up = 90.0
t.down = 270.0
t.lef = 180.0
def move_turtle_left():
if t.heading() == 90.0:
t.left(90)
if t.heading() == 270.0:
t.right(90)
t.forward(10)
def move_turtle_right():
if t.heading() == 90.0:
t.right(90)
if t.heading() == 270.0:
t.left(90)
t.forward(10)
def move_turtle_up():
if t.heading() != 90.0:
t.left(90)
if t.heading() == 180.0:
t.right(90)
t.forward(10)
def move_turtle_down():
if t.heading() == 180.0:
t.left(90)
if t.heading() == 0.0:
t.right(90)
t.forward(10)
screen.listen()
screen.onkeypress(move_turtle_left, 'Left')
screen.onkeypress(move_turtle_right, 'Right')
screen.onkeypress(move_turtle_up, 'Up')
screen.onkeypress(move_turtle_down, 'Down')
while True:
screen.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment