Share your repls and programming experiences

← Back to all posts
This is the first time a python-turtle has been controllable!
BuzzawJHKE (20)

This is the first time a python-turtle has been controllable!

UPDATE: I made a game with this: https://repl.it/talk/share/Turtle-Race-Xtreme/47560

Comments
hotnewtop
mwilki7 (1129)

nice
you should try the turtle.onkey(function_name, "key") function next so you don't have to press enter

BuzzawJHKE (20)

Can you give me an example?

mwilki7 (1129)

@jhke

def up():
    tim.setheading(90)
    tim.forward(100)

tim.listen()

tim.onkey(up, "Up")

More samples can be found here:
https://techwithtim.net/tutorials/python-module-walk-throughs/turtle-module/key-presses-events/

BuzzawJHKE (20)

There's an error:
AttributeError: 'Turtle' object has no attribute 'listen' on line 20

mwilki7 (1129)

@jhke They forgot to create the global variables correctly, try this:

import turtle
import random

t = turtle.Turtle()
screen = turtle.Screen()

def up():
    t.setheading(90)
    t.forward(100)

def down():
    t.setheading(270)
    t.forward(100)

def left():
    t.setheading(180)
    t.forward(100)

def right():
    t.setheading(0)
    t.forward(100)

screen.listen()

screen.onkey(up, "Up")  # This will call the up function if the "Left" arrow key is pressed
screen.onkey(down, "Down")
screen.onkey(left, "Left")
screen.onkey(right, "Right")

screen.mainloop()  # This will make sure the program continues to run 
Leroy01010 (415)

did you get the profile pic from khan