编写步骤:
0.导入库模块
1.绘制单段间隔
2.绘制单段数码管
3.根据数字绘制七段数码管
4.获得要输出的数字
5.主函数(设置画布和画笔)
6.调用主函数
#0.导入库模块
from turtle import *
from random import *
import time
#1.绘制单段间隔
def drawGap():
penup()
fd(5)
#2.绘制单段数码管
def drawLine(draw):
drawGap()
if draw:
pendown()
else:
penup()
fd(40)
drawGap()
right(90)
#3.根据数字绘制七段数码电子管
def drawDigit(digit):
pencolor(random(),random(),random())
drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False)
pencolor(random(),random(),random())
drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False)
pencolor(random(),random(),random())
drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False)
pencolor(random(),random(),random())
drawLine(True) if digit in [0,2,6,8] else drawLine(False)
left(90)
pencolor(random(),random(),random())
drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False)
pencolor(random(),random(),random())
drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False)
pencolor(random(),random(),random())
drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False)
right(180)
penup()
fd(20)
#4.获得要输出的数字
def drawData(date):
for i in date:
if i=='-':
write('年',font=("楷体",20,"normal"))
fd(40)
elif i=='=':
write('月',font=("楷体",20,"normal"))
fd(40)
elif i=='+':
write('日',font=("楷体",20,"normal"))
else:
drawDigit(eval(i))
#5.主函数
def main():
setup(850.350)#设置画布的大小
bgcolor('black') #设置画布背景
penup()
fd(-350)
pendown()
pensize(5)
speed(200) #画速
date = time.strftime('%Y-%m-%d %H:%M:%S') #获取时间数据为当前时间日期
drawData(date)
hideturtle() #隐藏画笔
done() #结束程序
#6.调用函数
if __name__ == "__main__": #当主函数为脚本文件而非导入模块是执行下面函数
main()
运行结果: