基本的七段数码管绘制
基本思路
绘制一个时间的七段数码管
步骤一:绘制单个数字对应的数码管 步骤二:获得一串数字,绘制对应的数码管 步骤三:获得当前系统时间,绘制对应的数码管
数码管的基本构造
七段数码管由七个基本线条组成,可以有固定顺序,不同的数字显示不同的线条。
七段数码管的基本构造
绘制单条线绘制七段数码管的最基础的一步是绘制一条线,代码如下:
import turtle def drawLine(draw): turtle.pendown()if draw else turtle.penup() turtle.fd(40) turtle.right(90) def drawDight(dight): drawLine(True) if dight in [2,3,4,5,6,8,9] else drawLine(False) drawLine(True) if dight in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(False) drawLine(True) if dight in [0, 2, 3, 5, 6, 8, 9] else drawLine(False) drawLine(True) if dight in [0, 2, 6, 8,] else drawLine(False) turtle.left(90) drawLine(True) if dight in [0, 4,5,6,8,9 ] else drawLine(False) drawLine(True) if dight in [0, 2, 3,5,6,7,8,9 ] else drawLine(False) drawLine(True) if dight in [0, 1,2,3,4, 7, 8,9 ] else drawLine(False) turtle.left(180) turtle.penup() turtle.fd(20)
获得一串数字,绘制对应数码管
def drawDate(date): for i in date: drawDight(eval(i)) def main(): turtle.setup(800,350,200,200) turtle.penup() turtle.fd(-300) turtle.pensize(5) drawDate("202220726") turtle.hideturtle() turtle.done() if __name__ == '__main__': main()
获得当前系统时间,调整显示格式,绘制相应数码管
结果显示
案例的方法思维
①模块化思维:确定模块接口,封装功能
②规则化思维:抽象过程为规则,计算机自动执行
③化繁为简:将大功能变为小功能组合,分而治之
import turtle, datetime 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.left(180) turtle.penup() turtle.fd(20) def drawDate(date): #获得要输出的数字 for i in date: drawDigit(eval(i)) #注意: 通过eval()函数将数字变为整数 def main(): turtle.setup(800, 350, 200, 200) turtle.penup() turtle.fd(-300) turtle.pensize(5) drawDate(datetime.datetime.now().strftime('%Y%m%d')) turtle.hideturtle() main()
举一反三,我们可以写个带有时间倒计时的程序
import turtle def drawGap(): turtle.penup() turtle.forward(10) def drawLine(draw): # 绘制单段数码管 drawGap() turtle.pendown() if draw else turtle.penup() turtle.forward(40) drawGap() 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.left(180) turtle.penup() # 为绘制后续数字确定位置 turtle.forward(20) # 未确定后续数字确定位置 def drawDate(date): # 获得要输出的数字 turtle.pencolor("red") for i in reversed(range(date)): nums = str(i) for num in nums: drawDigit(eval(num)) # 通过eval将字符串编程整数 turtle.clear() width = len(nums) turtle.forward(-80 * width) # 画一段长度是40,左右各空10,画完之后前进20,做好准备画下一个数字,回退应退,80 def main(): turtle.setup(600, 300, 200, 200) turtle.penup() turtle.forward(-100) turtle.pensize(15) turtle.speed("fast") drawDate(12) turtle.done() turtle.hideturtle() main()