import turtle
#绘制单个线条
def drawLine(draw):
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
turtle.right(90)
#绘制整个数字及整个时间数字段
def drawDigit(digit):
drawLine(True) if digit in [2, 3, 4, 5, 6, 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)
turtle.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)
turtle.right(180)
turtle.penup()
turtle.fd(20)
turtle.pendown()
#对时间数字段中单个的数字进行处理
def drawData(data):
for i in data:
drawDigit(eval(i))
#设置基本绘制环境
def main():
turtle.setup(800, 350, 200, 200)
turtle.penup()
turtle.fd(-300)
turtle.pensize(5)
drawData('20180730')
turtle.hideturtle()
turtle.done()
main()
下面为更好绘制效果的实现:
1、增加绘制的数码效果,也就是线段间隔效果
#间隔设置
def drawGap():
turtle.penup()
turtle.fd(5)
def drawLine(draw):
#在绘制线段之前和之后都填上一段间隔
drawGap()
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
drawGap()
turtle.right(90)
2、获取系统时间
(main函数中)
drawData(time.strftime('%Y-%m=%d+', time.gmtime()))
3、年月日的表示、颜色统一
def drawData(data):
#年的颜色为tomato
turtle.pencolor("tomato")
for i in data:
#四组判断
if i == '-':
turtle.write('年', font = ("Arial", 25, "normal"))
#月的颜色为黄色
turtle.pencolor("yellow")
turtle.fd(90)
elif i =='=':
turtle.write('月', font = ("Arial", 25, "normal"))
#日的颜色为蓝色
turtle.pencolor("blue")
turtle.fd(90)
elif i =='+':
turtle.write('日', font = ("Arial", 25, "normal"))
else:
drawDigit(eval(i))