Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Turtle centre turtle on screen

I want to move the turtle in random directions while keeping the turtle at the center of the screen. How can I do this?

While True:
    turtle.setheading(random.randint(0,360))
    turtle.forward(10)
like image 661
Teslacoiler11 Avatar asked Jun 06 '26 15:06

Teslacoiler11


2 Answers

I want the camera screen to follow the turtle whereever its going.

You can do this by manipulating the scroll position using methods of the underlying tkinter window structure. Here's a turtle example that moves a ball but keeps it centered in the window.

like image 62
cdlane Avatar answered Jun 08 '26 05:06

cdlane


What do you mean by "center of the screen"? Your code is turning your turtle by 0 to 360 and then move 10 forward. If you dont want to move the turtle remove the turtle.forward(10).

To move your turtle back to the start you could add turtle.home().

While True:
    turtle.setheading(random.randint(0,360))
    turtle.forward(10)
    turtle.home()
like image 39
Michael Barfs Avatar answered Jun 08 '26 06:06

Michael Barfs