Advertisement
acclivity

pyTurtleClock

Mar 6th, 2022
1,219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.63 KB | None | 0 0
  1. # Real-Time Analogue Clock
  2. # Mike Kerry - Feb 2022 - acclivity2@gmail.com
  3.  
  4. import turtle           # turtle library used to draw clock face and hands
  5. import time             # time library used to access in-world local time
  6.  
  7. # A function to draw numbers on the clock face. Used for drawing "3", "6", "9" and "12"
  8. def draw_number(t, x, y, txt):
  9.     # 't' is the turtle to use. x, y = position on the screen. 'txt' contains the number to draw
  10.     t.up()              # Lift turtle so it can be moved into position without leaving a trail
  11.     t.goto(x, y)        # Position turtle where the text is to be written
  12.     t.down()            # lower the turtle ready for writing
  13.     t.color("white")    # select the colour
  14.     t.write(txt)        # write the text on the clock face
  15.  
  16. # A function used for drawing any of the hands (second, minute, or hour hand)
  17. def draw_hand(t, angle, tint, thick, size):
  18.     # 't' is the turtle to use. 'angle' is the clockwise rotation from 12.
  19.     # 'thick' is the width of the line to draw, and 'size' is the length to draw.
  20.     t.up()
  21.     t.goto(0, 0)        # go silently to clock centre
  22.     # Here we convert the conventional clock-wise angle to a turtle-compatible angle
  23.     # which is anti-clockwise from the '3' position
  24.     t.setheading((90 - angle) % 360)     # Convert angle to turtle-compatible angle
  25.     t.color(tint)                        # set the colour for the hand
  26.     t.down()
  27.     t.pensize(thick)                     # set the line thickness
  28.     t.forward(size)                      # draw the hand
  29.  
  30. # Set the colour of the clock face. Most common colours can be specified as a text string
  31. turtle.bgcolor("dark blue")
  32.  
  33. # Define three turtles for drawing different parts of the clock.
  34. turtle1 = turtle.Turtle()       # define a turtle for the clock face
  35. turtle1.hideturtle()            # hide the turtle itself
  36. turtle1.speed(0)                # tell the turtle to operate at full speed
  37.  
  38. turtle2 = turtle.Turtle()       # define a 2nd turtle for the Second hand
  39. turtle2.hideturtle()
  40. turtle2.speed(0)
  41.  
  42. turtle3 = turtle.Turtle()       # define a 3rd turtle for the Hour amd Minute hands
  43. turtle3.hideturtle()
  44. turtle3.speed(0)
  45.  
  46. # Draw markers all around the clock face
  47. turtle1.color("cyan")           # turtle1 is used for the minute markers
  48. for ang in range(0, 360, 6):    # A marker is needed every 6 degrees
  49.     turtle1.up()
  50.     turtle1.goto(0, 0)          # go silently to the clock centre
  51.     turtle1.setheading(ang)
  52.     if ang % 30:                # Draw the minute markers
  53.         turtle1.forward(208)    # move forward without writing a line
  54.         turtle1.down()
  55.         turtle1.forward(2)      # write the short marker
  56.  
  57.     else:                       # for every 5 minute position
  58.         turtle1.forward(200)
  59.         turtle1.down()
  60.         turtle1.forward(10)     # write the longer marker
  61.  
  62. # Draw four numbers around the clock face
  63. # x,y positions are carefully chosen for good alignment with the clock hands
  64. draw_number(turtle1, -6, 210, "12")
  65. draw_number(turtle1, 215, -8, "3")
  66. draw_number(turtle1, -3, -230, "6")
  67. draw_number(turtle1, -220, -8, "9")
  68.  
  69. # Initialise two memory variables
  70. lastsec = -1
  71. last_min_angle = -1
  72. while True:             # Define an infinite loop
  73.     h, m, s = time.localtime()[3:6]     # Extract hours, minutes, seconds from the time object
  74.     if s == lastsec:
  75.         continue            # wait for next second to arrive
  76.     lastsec = s             # remember the latest second value
  77.     second_angle = s * 6    # Second hand moves 6 degrees per second
  78.     minute_angle = m * 6    # Minute hand moves 6 degrees per minute
  79.     # adjust minute angle according to current seconds value
  80.     # e.g. at 30 seconds, minute angle will have increased by 3 degrees
  81.     minute_angle += s // 10
  82.  
  83.     hour_angle = (h % 12) * 30     # Hour hand moves 30 degrees per hour
  84.     # Adjust hour angle according to the current minute value
  85.     # e.g. 30 minutes will add 15 degrees to hour angle
  86.     hour_angle += m // 2
  87.  
  88.     # Remove the previous Second hand and draw it in its new position
  89.     turtle2.clear()     # turtle2 is used for the second hand only
  90.     # the second hand is  white line, 1 pixel wide, and 180 pixels long
  91.     draw_hand(turtle2, second_angle, "white", 1, 180)
  92.  
  93.     # When Minute hand moves, remove the last Minute and Hour hands and redraw both
  94.     if minute_angle != last_min_angle:
  95.         turtle3.clear()     # turtle3 is used for both the minute and hour hands
  96.         draw_hand(turtle3, hour_angle, "red", 4, 120)
  97.         draw_hand(turtle3, minute_angle, "red", 4, 160)
  98.         last_min_angle = minute_angle       # remember the latest minute value
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement