Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Real-Time Analogue Clock
- # Mike Kerry - Feb 2022 - acclivity2@gmail.com
- import turtle # turtle library used to draw clock face and hands
- import time # time library used to access in-world local time
- # A function to draw numbers on the clock face. Used for drawing "3", "6", "9" and "12"
- def draw_number(t, x, y, txt):
- # 't' is the turtle to use. x, y = position on the screen. 'txt' contains the number to draw
- t.up() # Lift turtle so it can be moved into position without leaving a trail
- t.goto(x, y) # Position turtle where the text is to be written
- t.down() # lower the turtle ready for writing
- t.color("white") # select the colour
- t.write(txt) # write the text on the clock face
- # A function used for drawing any of the hands (second, minute, or hour hand)
- def draw_hand(t, angle, tint, thick, size):
- # 't' is the turtle to use. 'angle' is the clockwise rotation from 12.
- # 'thick' is the width of the line to draw, and 'size' is the length to draw.
- t.up()
- t.goto(0, 0) # go silently to clock centre
- # Here we convert the conventional clock-wise angle to a turtle-compatible angle
- # which is anti-clockwise from the '3' position
- t.setheading((90 - angle) % 360) # Convert angle to turtle-compatible angle
- t.color(tint) # set the colour for the hand
- t.down()
- t.pensize(thick) # set the line thickness
- t.forward(size) # draw the hand
- # Set the colour of the clock face. Most common colours can be specified as a text string
- turtle.bgcolor("dark blue")
- # Define three turtles for drawing different parts of the clock.
- turtle1 = turtle.Turtle() # define a turtle for the clock face
- turtle1.hideturtle() # hide the turtle itself
- turtle1.speed(0) # tell the turtle to operate at full speed
- turtle2 = turtle.Turtle() # define a 2nd turtle for the Second hand
- turtle2.hideturtle()
- turtle2.speed(0)
- turtle3 = turtle.Turtle() # define a 3rd turtle for the Hour amd Minute hands
- turtle3.hideturtle()
- turtle3.speed(0)
- # Draw markers all around the clock face
- turtle1.color("cyan") # turtle1 is used for the minute markers
- for ang in range(0, 360, 6): # A marker is needed every 6 degrees
- turtle1.up()
- turtle1.goto(0, 0) # go silently to the clock centre
- turtle1.setheading(ang)
- if ang % 30: # Draw the minute markers
- turtle1.forward(208) # move forward without writing a line
- turtle1.down()
- turtle1.forward(2) # write the short marker
- else: # for every 5 minute position
- turtle1.forward(200)
- turtle1.down()
- turtle1.forward(10) # write the longer marker
- # Draw four numbers around the clock face
- # x,y positions are carefully chosen for good alignment with the clock hands
- draw_number(turtle1, -6, 210, "12")
- draw_number(turtle1, 215, -8, "3")
- draw_number(turtle1, -3, -230, "6")
- draw_number(turtle1, -220, -8, "9")
- # Initialise two memory variables
- lastsec = -1
- last_min_angle = -1
- while True: # Define an infinite loop
- h, m, s = time.localtime()[3:6] # Extract hours, minutes, seconds from the time object
- if s == lastsec:
- continue # wait for next second to arrive
- lastsec = s # remember the latest second value
- second_angle = s * 6 # Second hand moves 6 degrees per second
- minute_angle = m * 6 # Minute hand moves 6 degrees per minute
- # adjust minute angle according to current seconds value
- # e.g. at 30 seconds, minute angle will have increased by 3 degrees
- minute_angle += s // 10
- hour_angle = (h % 12) * 30 # Hour hand moves 30 degrees per hour
- # Adjust hour angle according to the current minute value
- # e.g. 30 minutes will add 15 degrees to hour angle
- hour_angle += m // 2
- # Remove the previous Second hand and draw it in its new position
- turtle2.clear() # turtle2 is used for the second hand only
- # the second hand is white line, 1 pixel wide, and 180 pixels long
- draw_hand(turtle2, second_angle, "white", 1, 180)
- # When Minute hand moves, remove the last Minute and Hour hands and redraw both
- if minute_angle != last_min_angle:
- turtle3.clear() # turtle3 is used for both the minute and hour hands
- draw_hand(turtle3, hour_angle, "red", 4, 120)
- draw_hand(turtle3, minute_angle, "red", 4, 160)
- last_min_angle = minute_angle # remember the latest minute value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement