← Reference

Interaction: cheat sheet

Python functions and events · In any playground, open Reference to copy these.

setheading

Face a direction. 0 is right, 90 is up, 180 is left.

setheading(0)
Example
setheading(90)
forward(60)
What it draws
setheading(90): facing up

bgcolor

Set the background colour.

bgcolor("lightblue")
Example
bgcolor("lightblue")
forward(80)
What it draws

clear

Wipe the drawing.

clear()

onscreenclick

Run a function every time the picture is clicked. It receives the click position.

def stamp(x, y):
    goto(x, y)
    dot(20)

onscreenclick(stamp)
listen()
mainloop()
Example
def stamp(x, y):
    goto(x, y)
    dot(20)

penup()
stamp(-60, 30)
stamp(50, -20)
hideturtle()
What it draws
After two clicks

write

Write text at the turtle's position.

write("Hello")
Example
write("Hello", False, "left", ("Arial", 20, "normal"))
hideturtle()
What it draws

hideturtle

Hide the turtle so only the drawing shows.

hideturtle()
Example
forward(80)
hideturtle()
What it draws
The line stays; the turtle is hidden

running total

Start the total at 0 once, then add to it on every pass.

total = 0
while total < 10:
    total += int(input("Add: "))

onkey

Run a function every time a key is pressed. Click the picture first so it receives keys.

def go_up():
    forward(20)

onkey(go_up, "Up")
listen()
mainloop()
Example
def go_up():
    forward(20)

go_up()
go_up()
go_up()
What it draws
After pressing Up three times