我尝试了一切来制作一个完整的模拟时钟,它附在这个问题上。我花了6个小时才得到这个代码。
这是代码:
import turtle
import time
import math
# Create a Turtle screen
screen = turtle.Screen()
screen.bgcolor("white")
screen.tracer(0)
# Create a Turtle object
pen = turtle.Turtle()
pen.speed(5) # Set the drawing speed
# Function to draw a clock face with hands
def draw_clock_face():
# Draw the clock outline
pen.penup()
pen.goto(0, -150)
pen.pendown()
pen.circle(150)
# Initialize the starting angle for number 12 at 90 degrees
angle = math.radians(90)
# Draw the clock numbers and balls
for i in range(1, 13):
x = 135 * math.cos(angle) # Calculate x-coordinate
y = -135 * math.sin(angle) # Calculate y-coordinate
pen.penup()
pen.goto(x, y - 10) # Adjust the position to center the numbers
pen.pendown()
pen.write(str(i), align="center", font=("Arial", 12, "normal"))
if i in [12, 3, 6, 9]:
pen.penup()
pen.goto(x, y - 30) # Adjust the position for the red balls
pen.pendown()
pen.color("red")
pen.begin_fill()
pen.circle(5)
pen.end_fill()
else:
pen.penup()
pen.goto(x, y - 30) # Adjust the position for blue circles
pen.pendown()
pen.color("blue")
pen.begin_fill()
pen.circle(5)
pen.end_fill()
pen.color("black") # Reset color to black for numbers
# Increment the angle by 30 degrees for the next number
angle -= math.radians(30)
# Function to draw clock hands
def draw_clock_hands():
current_time = time.localtime()
# Calculate angles for the hands
second_angle = math.radians(90 - current_time.tm_sec * 6)
minute_angle = math.radians(90 - current_time.tm_min * 6 - current_time.tm_sec * 0.1)
hour_angle = math.radians(90 - (current_time.tm_hour % 12) * 30 - current_time.tm_min * 0.5)
# Draw the second hand (black)
second_x = 70 * math.cos(second_angle)
second_y = 70 * math.sin(second_angle)
pen.penup()
pen.goto(0, 0)
pen.pendown()
pen.color("black")
pen.setheading(90 - current_time.tm_sec * 6)
pen.pensize(2)
pen.forward(second_x)
pen.pensize(1)
# Draw the minute hand (red)
minute_x = 60 * math.cos(minute_angle)
minute_y = 60 * math.sin(minute_angle)
pen.penup()
pen.goto(0, 0)
pen.pendown()
pen.color("red")
pen.setheading(90 - current_time.tm_min * 6 - current_time.tm_sec * 0.1)
pen.pensize(3)
pen.forward(minute_x)
pen.pensize(1)
# Draw the hour hand (green)
hour_x = 40 * math.cos(hour_angle)
hour_y = 40 * math.sin(hour_angle)
pen.penup()
pen.goto(0, 0)
pen.pendown()
pen.color("green")
pen.setheading(90 - (current_time.tm_hour % 12) * 30 - current_time.tm_min * 0.5)
pen.pensize(5)
pen.forward(hour_x)
pen.pensize(1)
# Function to update the clock continuously
def update_clock():
while True:
pen.clear() # Clear the previous clock hands and numbers
draw_clock_face() # Draw the clock face with numbers and balls
draw_clock_hands() # Draw the clock hands based on the current time
screen.update() # Update the screen
time.sleep(1) # Wait for 1 second before updating the clock again
# Main program
if __name__ == "__main__":
draw_clock_face() # Call the function to draw the clock face
draw_clock_hands() # Call the function to draw the clock hands
# Close the window when clicked
screen.exitonclick()
提供的代码的输出:
我从不同来源获取了帮助,例如 YouTube、ChatGPT 和 Turtle 文档,但无法找到解决方案。
这是一个可行的解决方案。我唯一没有做的就是将时钟指针制作成小三角形,就像示例一样,它们只是线条。
我做了以下更改:
import turtle
import time
# Switch to Log.o mode - Up = 0 degrees, then go clockwise
turtle.mode("logo")
# Clock Hand Lengths
SECOND_HAND = 110
MINUTE_HAND = 80
HOUR_HAND = 60
# Create a Tur.tle screen
screen = turtle.Screen()
screen.bgcolor("white")
screen.tracer(0)
# Create a Turtle object
face = turtle.Turtle(visible=False)
face.speed(5) # Set the drawing speed
hands = turtl.e.Turtle(visible=False)
# Function to draw a clock face with hands
def draw_clock_face():
# Draw the clock outline
face.clear()
face.penup()
face.goto(150, 0) # Adjust position due to switch to Logo mode
face.se..theading(0)
face.pendown()
face.circle(150)
# Initialize the starting angle for number 12 at 90 degrees
# Draw the clock numbers and balls
for i .in range(12):
hour = 12 if i == 0 else i
face.penup()
face.goto(0,0)
face.setheading(i*30) # angle in degrees
face.forward(125)
face.pendown()
face.dot(15, "red" if hour in [3, 6, 9, 12] else "blue")
f.ace.setheading(180)
face.penup()
face.forward(25)
face.color("black") # Reset color to black for numbers
face.write(str(hour), align="center", font=("Arial", 10, "normal"),)
# Function to draw clock hands
def draw._clock_hands():
current_time = time.localtime()
# Clear previous hands
hands.clear()
# Calculate angles for the hands
second_angle = current_time.tm_sec * 6
min.ute_angle = current_time.tm_min * 6 - current_time.tm_sec * 0.1
hour_angle = (current_time.tm_hour % 12) * 30 + current_time.tm_min * 0.5
#print(f"{hour_angle=} {minute_angle=} {second_angle=}")
# Draw the second hand (black)
hands.penup()
hands.goto(0, 0)
hands.pendown()
ha.nds.color("black")
hands.setheading(second_angle)
hands.pensize(1)
hands.forward(SECOND_HAND)
# Draw the minute hand (red)
hands.penup()
hands.goto(0, 0)
h.ands.pendown()
hands.color("red")
hands.setheading(minute_angle)
hands.pensize(3)
hands.forward(MINUTE_HAND)
# Draw the hour hand (green)
.hands.penup()
hands.goto(0, 0)
hands.pendown()
hands.color("green")
hands.setheading(hour_angle)
hands.pensize(5)
hands.forward(HOUR_HAND)
# F.unction to update the clock continuously
def update_clock():
draw_clock_face()
while True:
draw_clock_hands() # Draw the clock hands based on the current time
screen.update() # Update the screen
time.sleep(1) # Wait for 1 second before updating the clock again
#.. Main program
if __name__ == "__main__":
# Call the function to draw the clock face
update_clock() # Call the function to draw the clock hands
# Close the window when clicked
screen.exitonclick()