0

I am trying to make a turtle analog clock, and to make the second, minute, an hour hands I am making a shape with turtle, and using .tilt() to tilt it 6 degrees every second. The thing is, when I run .tilt() it rotates the ship from the middle, whereas I want to have it rotate from the bottom point (like an analog clock). Is there a way to do that, or do I need to find another way to make this program?

Here is my code:

from turtle import *
import time

turtle = Turtle()
turtle.shape("square")
turtle.shapesize(6, .1)

tilt_amnt = 0

for x in range (60):
    turtle.tilt(tilt_amnt)
    tilt_amnt = tilt_amnt + 6
    time.sleep(1)
Kovy Jacob
  • 489
  • 2
  • 16

2 Answers2

0

I don't think there is a way to tilt the turtle in such a way where it turns from the bottom but you can rewrite the code to essentially do the same thing but using forward instead. Check this out.

from turtle import *
import time

turtle = Turtle()
turtle.pensize(2)
turtle.hideturtle()
turtle.speed(0)

tilt_amnt = 0

for x in range (60):
    turtle.right(tilt_amnt)
    turtle.forward(100)
    turtle.backward(100)
    tilt_amnt = tilt_amnt + 6
    time.sleep(1)
    turtle.clear()
Alex Joslin
  • 131
  • 2
  • 5
  • Its working... the only thing is it takes 1.4 seconds to execute, meaning every 1.4 seconds the second have moves. – Kovy Jacob Apr 19 '22 at 03:29
0

Some thoughts: first, I wouldn't use tilt() (high overhead, forces an update), even if you do use turtles as hands, consider using right() or setheading(); second, I would use mode('logo') as this makes 0 degrees the top of the screen and makes headings clockwise (like, say a 'clock') instead of counterclockwise; third, if you want accuracy, don't use sleep() but rather extract the current time from the system and set your hands accordingly.

Large real world clocks do turn their hands from the middle, or more specifially, the center of gravity. This keeps the hands from slowing the mechanism on the way up, or rushing it on the way down. The trick is to make the hands look visually asymmetric but keep the weight symmetric. Here's a solution I came up with earlier that does something like this with turtles as the hands, turning from their centers.

Finally, here's a rewrite of the example @AlexJoslin provided to use realtime accuracy, potentially updating hand positions multiple times within a second:

from turtle import Screen, Turtle
from time import localtime as time

def tick():
    second.setheading(6 * time().tm_sec)
    second.clear()
    second.forward(150)
    second.backward(150)

    screen.update()
    screen.ontimer(tick)

screen = Screen()
screen.mode('logo')
screen.tracer(False)

second = Turtle()
second.hideturtle()
second.pensize(2)

tick()

screen.exitonclick()
cdlane
  • 40,441
  • 5
  • 32
  • 81