ChatGPT created an Analog Clock - Python

Опубликовано: 24 Март 2026
на канале: Waterlemon
463
18

This video is about ChatGPT created an Analog Clock - Python

===== Python Code =====
import turtle
import time
import math
import random

Create a turtle object and set its speed to fastest
pen = turtle.Turtle()
pen.speed("fastest")

Set the screen background color
turtle.bgcolor("black")

Set the pen color and size
pen.pencolor("white")
pen.pensize(3)

Draw the clock face
pen.penup()
pen.goto(0, -150)
pen.pendown()
pen.circle(150)

Draw the hour markers
pen.pencolor("orange")
for i in range(0, 360, 30):
pen.penup()
pen.goto(0, 0)
pen.right(30)
pen.forward(120)
pen.pendown()
pen.forward(20)

Draw the minute markers
pen.pencolor("green")
pen.pensize(1)
for i in range(60):
pen.penup()
pen.goto(0, 0)
pen.right(6)
pen.forward(140)
pen.pendown()
pen.forward(10)

Draw the hands
hour_hand = turtle.Turtle()
hour_hand.pencolor("white")
hour_hand.pensize(5)
minute_hand = turtle.Turtle()
minute_hand.pencolor("white")
minute_hand.pensize(3)
second_hand = turtle.Turtle()
second_hand.pencolor("red")
second_hand.pensize(1)

Hide the turtle objects to make them move smoothly
hour_hand.hideturtle()
minute_hand.hideturtle()
second_hand.hideturtle()

Use the tracer() method to turn off animation and speed up drawing
turtle.tracer(0)

Update the hands every second
while True:
current_time = time.localtime()
hour = current_time.tm_hour % 12
minute = current_time.tm_min
second = current_time.tm_sec
hour_angle = (hour * 30) + (minute * 0.5)
minute_angle = minute * 6
second_angle = second * 6
hour_x = math.sin(math.radians(hour_angle)) * 70
hour_y = math.cos(math.radians(hour_angle)) * 70
minute_x = math.sin(math.radians(minute_angle)) * 100
minute_y = math.cos(math.radians(minute_angle)) * 100
second_x = math.sin(math.radians(second_angle)) * 130
second_y = math.cos(math.radians(second_angle)) * 130
hour_hand.clear()
hour_hand.penup()
hour_hand.goto(0, 0)
hour_hand.pendown()
hour_hand.goto(hour_x, hour_y)
minute_hand.clear()
minute_hand.penup()
minute_hand.goto(0, 0)
minute_hand.pendown()
minute_hand.goto(minute_x, minute_y)
second_hand.clear()
second_hand.penup()
second_hand.goto(0, 0)
second_hand.pendown()
second_hand.goto(second_x, second_y)
Adjust the position of the hour hand based on the minute hand's position
hour_hand.setheading(minute_angle / 12 + hour_angle)
turtle.update()

Change the clock face color every 3 seconds
if current_time.tm_sec % 3 == 0:
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
color = random.choice(colors)
turtle.bgcolor(color)

time.sleep(1)

Hide the turtle pen
pen.hideturtle()

=====================

Background music: James River - DJ Williams.mp3 (from Youtube audio library)

=====================

#chatgpt
#pythonprogramming
#clock
#drawing