import turtle
import time
def bg_refresh(): #初始化闹钟背景 (600*200)
turtle.reset()
turtle.hideturtle()
turtle.speed(0)
turtle.colormode(255)
turtle.color(0,0,0)
turtle.begin_fill()
turtle.up()
turtle.left(90)
turtle.fd(160)
turtle.right(90)
turtle.pd()
turtle.fd(300)
turtle.circle(-50,90)
turtle.fd(200)
turtle.circle(-50,90)
turtle.fd(600)
turtle.circle(-50,90)
turtle.fd(200)
turtle.circle(-50,90)
turtle.fd(300)
turtle.end_fill()
def num_write(n):
turtle.colormode(255)
turtle.color("red")
turtle.pensize(15)
turtle.speed(3)
if(n==0):
turtle.fd(30)
turtle.right(90)
turtle.fd(150)
turtle.right(90)
turtle.fd(60)
turtle.right(90)
turtle.fd(150)
turtle.right(90)
turtle.fd(30)
elif(n==1):
turtle.up()
turtle.fd(10)
turtle.pd()
turtle.right(90)
turtle.fd(150)
turtle.left(90) # 最后状态朝向东
elif(n==2):
turtle.fd(60)
turtle.right(90)
turtle.fd(75)
turtle.right(90)
turtle.fd(60)
turtle.left(90)
turtle.fd(75)
turtle.left(90)
turtle.fd(60)
elif(n==3):
turtle.fd(60)
turtle.right(90)
turtle.fd(75)
turtle.right(90)
turtle.fd(60)
turtle.right(180)
turtle.fd(60)
turtle.right(90)
turtle.fd(75)
turtle.right(90)
turtle.fd(60)
turtle.right(180) # 将方向调整为东
elif(n==4):
turtle.right(180)
turtle.up()
turtle.fd(30)
turtle.pd()
turtle.left(90)
turtle.fd(75)
turtle.left(90)
turtle.fd(60)
turtle.left(90)
turtle.up()
turtle.fd(75)
turtle.pd()
turtle.right(180)
turtle.fd(150)
turtle.left(90) # 将方向调整为东
elif(n==5):
turtle.up()
turtle.fd(30)
turtle.pd()
turtle.right(180)
turtle.fd(60)
turtle.left(90)
turtle.fd(75)
turtle.left(90)
turtle.fd(60)
turtle.right(90)
turtle.fd(75)
turtle.right(90)
turtle.fd(60)
turtle.right(180) # 将方向调整为东
elif(n==6):
turtle.up()
turtle.fd(30)
turtle.pd()
turtle.right(180)
turtle.fd(60)
turtle.left(90)
turtle.fd(75)
turtle.left(90)
turtle.fd(60)
turtle.right(90)
turtle.fd(75)
turtle.right(90)
turtle.fd(60)
turtle.right(90)
turtle.fd(75)
turtle.right(90) # 将方向调整为东
elif(n==7):
turtle.right(180)
turtle.up()
turtle.fd(30)
turtle.pd()
turtle.right(180)
turtle.fd(60)
turtle.right(90)
turtle.fd(150)
turtle.left(90) # 将方向调整为东
elif(n==8):
turtle.up()
turtle.fd(30)
turtle.pd()
turtle.right(180)
turtle.fd(60)
turtle.left(90)
turtle.fd(75)
turtle.left(90)
turtle.fd(60)
turtle.right(90)
turtle.fd(75)
turtle.right(90)
turtle.fd(60)
turtle.right(90)
turtle.fd(75)
turtle.right(90)
turtle.fd(60)
turtle.left(90)
turtle.fd(75)
turtle.right(90) # 将方向调整为东
elif(n==9):
turtle.up()
turtle.fd(30)
turtle.pd()
turtle.right(180)
turtle.fd(60)
turtle.left(90)
turtle.fd(75)
turtle.left(90)
turtle.fd(60)
turtle.left(90)
turtle.up()
turtle.fd(75)
turtle.pd()
turtle.right(180)
turtle.fd(150)
turtle.right(90)
turtle.fd(60)
turtle.right(180) # 将方向调整为东
def move(dir):
turtle.up()
if(dir==0): #冒号
turtle.goto(0,40)
turtle.pd()
turtle.fd(1)
turtle.up()
turtle.goto(0,-40)
turtle.pd()
turtle.fd(1)
elif(dir==1):
turtle.goto(-250,80)
elif(dir==2):
turtle.goto(-130,80)
elif(dir==3):
turtle.goto(110,80)
elif(dir==4):
turtle.goto(230,80)
turtle.pd()
hour = str(time.localtime().tm_hour)
minute = str(time.localtime().tm_min)
print(hour,minute)
bg_refresh()
move(1)
if len(hour)!=1:
num_write(int(hour[0]))
move(2)
num_write(int(hour[1]))
else:
num_write(0)
move(2)
num_write(int(hour[0]))
move(0)
move(3)
if len(minute)!=1:
num_write(int(minute[0]))
move(4)
num_write(int(minute[1]))
else:
num_write(0)
move(4)
num_write(int(minute[0]))
while True:
if(str(time.localtime().tm_hour)!=hour or str(time.localtime().tm_min)!=minute):
hour = str(time.localtime().tm_hour)
minute = str(time.localtime().tm_min)
bg_refresh()
move(1)
if len(hour) != 1:
num_write(int(hour[0]))
move(2)
num_write(int(hour[1]))
else:
num_write(0)
move(2)
num_write(int(hour[0]))
move(0)
move(3)
if len(minute) != 1:
num_write(int(minute[0]))
move(4)
num_write(int(minute[1]))
else:
num_write(0)
move(4)
num_write(int(minute[0]))
time.sleep(1)
python trutle库实现动态电子时钟
最新推荐文章于 2024-07-03 21:21:56 发布