from turtle import *
from random import *
from time import *
#绘制单段间隔
def drawGap():
penup()
fd(5)
#绘制单段数码管
def drawline(draw):
drawGap()
pencolor(random(),random(),random())
pendown() if draw else penup()
fd(40)
drawGap()
right(90)
#根据数字绘制七段数码管
def drawDigit(digit):
drawline(True) if digit in [2, 3, 4, 5, 6, 7, 8, 9] else drawline(False)
drawline(True) if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawline(False)
drawline(True) if digit in [0, 2, 3, 5, 6, 8, 9] else drawline(False)
drawline(True) if digit in [0,2, 6, 8] else drawline(False)
left(90)
drawline(True) if digit in [0, 4, 5, 6, 8, 9] else drawline(False)
drawline(True) if digit in [0, 2, 3, 5, 6, 7, 8, 9] else drawline(False)
drawline(True) if digit in [0, 1, 2, 3, 4, 7, 8, 9] else drawline(False)
left(180)
penup() #为绘制后续数字确定位置
fd(20) #为前后两数字之间分离的距离
#获得要输出的数字
def drawDate(date):
for i in date:
if i =="-":
pencolor(random(), random(), random())
write("年", font=("华文行楷", 30, "normal"))
fd(40)
elif i =="=":
pencolor(random(), random(), random())
write("月", font=("华文行楷", 30, "normal"))
fd(40)
elif i =="+":
pencolor(random(), random(), random())
write("日", font=("华文行楷", 30, "normal"))
fd(40)
else:
pencolor(random(),random(),random())
drawDigit(eval(i))
#主函数(设置画布与画笔等)
def main():
setup(1000,350,200,200)
penup()
fd(-300)
pensize(5)
drawDate(strftime('%y-%m=%d+', localtime()))
hideturtle()
done()
#调用主函数
main()