Home > Mobile >  Turtle not moving, screen events
Turtle not moving, screen events

Time:11-18

Heello! My turtle is not moving and I don't really know why... May anyone help?

import turtle

chocolate = turtle.Turtle()


def move_forward():
    chocolate.forward(10)


screen = turtle.Screen()
screen.exitonclick()

screen.listen()
screen.onkey(fun=move_forward, key="space")

screen.mainloop()

I expect my turtle moving with 10 pace when I press "space".

CodePudding user response:

exit on click must be at the end.

import turtle

chocolate = turtle.Turtle()


def move_forward():
    chocolate.forward(10)


screen = turtle.Screen()


screen.listen()
screen.onkey(fun=move_forward, key="space")

screen.exitonclick()

CodePudding user response:

Try this. It's working. Tested here.

import turtle

chocolate = turtle.Turtle()
chocolate.shape("turtle")
chocolate.speed(500)

def move_forward():
    chocolate.forward(1)

screen = turtle.Screen()
screen.onkey(move_forward, "space")
screen.listen()
screen.exitonclick()

Perhaps this can give you more insights. Also exitonclick() must be at the very end.

  • Related