七段数码管绘制:
需求:用程序绘制七段数码管,似乎很有趣
该怎么做呢?
turtle绘图体系===》》七段数码管绘制
问题分析
七段数码管绘制时间
基本思路
步骤1:绘制单个数字对应的数码管
步骤2:获取一串数字,绘制对应的数码管
步骤3:获取当前系统时间,绘制对应的数码管
步骤一:绘制单个数码管
1、七段数码管由7个基本线条组成
2、七段数码管可以有固定顺序
3、不同数字显示不同的线条
1 #SevenDigitsDrawV1.py 2 import turtle 3 def drawLine(draw): #绘制单段数码管 4 turtle.pendown() if draw else turtle.penup() 5 turtle.fd(40) 6 turtle.right(90) 7 def drawDigit(digit): #根据数字绘制七段数码管 8 drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False) 9 drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False) 10 drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False) 11 drawLine(True) if digit in [0,2,6,8] else drawLine(False) 12 turtle.left(90) 13 drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False) 14 drawLine(True) if digit in [0,2,3,5,6,7,8,9] else drawLine(False) 15 drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False) 16 turtle.left(180) 17 turtle.penup() 18 turtle.fd(20) 19 def drawDate(date): #获得要输出的数字 20 for i in date: 21 drawDigit(eval(i)) #通过eval()函数将数字变为整数 22 def main(): 23 turtle.setup(800, 350, 200, 200) 24 turtle.penup() 25 turtle.fd(-300) 26 turtle.pensize(5) 27 drawDate('20181010') 28 turtle.hideturtle() 29 turtle.done() 30 main()
程序运行结果:
最终效果:
代码实现如下:
1 #SevenDigitsDrawV2.py 2 import turtle, time 3 def drawGap(): #绘制数码管间隔 4 turtle.penup() 5 turtle.fd(5) 6 def drawLine(draw): #绘制单段数码管 7 drawGap() 8 turtle.pendown() if draw else turtle.penup() 9 turtle.fd(40) 10 drawGap() 11 turtle.right(90) 12 def drawDigit(d): #根据数字绘制七段数码管 13 drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False) 14 drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False) 15 drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False) 16 drawLine(True) if d in [0,2,6,8] else drawLine(False) 17 turtle.left(90) 18 drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False) 19 drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False) 20 drawLine(True) if d in [0,1,2,3,4,7,8,9] else drawLine(False) 21 turtle.left(180) 22 turtle.penup() 23 turtle.fd(20) 24 def drawDate(date): 25 turtle.pencolor("red") 26 for i in date: 27 if i == '-': 28 turtle.write('年',font=("Arial", 18, "normal")) 29 turtle.pencolor("green") 30 turtle.fd(40) 31 elif i == '=': 32 turtle.write('月',font=("Arial", 18, "normal")) 33 turtle.pencolor("blue") 34 turtle.fd(40) 35 elif i == '+': 36 turtle.write('日',font=("Arial", 18, "normal")) 37 else: 38 drawDigit(eval(i)) 39 def main(): 40 turtle.setup(800, 350, 200, 200) 41 turtle.penup() 42 turtle.fd(-350) 43 turtle.pensize(5) 44 # drawDate('2018-10=10+') 45 drawDate(time.strftime('%Y-%m=%d+',time.gmtime())) 46 turtle.hideturtle() 47 turtle.done() 48 main()