python 绘制7段数码管显示当前时间

需求:

绘制一个可以显示时间的数码表

思路:

  1. 绘制单个数字的数码管
  2. 获得一串数字,绘制对应的数码管
  3. 获得当前系统时间,绘制对应的数码管

思维:

  • 模块化思维:确定模块接口,封装功能
  • 规则化思维:抽象过程为规则,计算机自动执行
  • 花繁为简:将大功能变为小功能组合,分而治之

代码如下:

# DigitalPipe.py
import turtle,time

def drawgap():
    turtle.penup()
    turtle.fd(5)

def drawLine(flag):
    drawgap()
    turtle.pendown() if flag else turtle.penup()
    turtle.fd(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.penup()
    turtle.left(180)
    turtle.fd(40)

def drawDate(date):
    for i in date:
        if i is '-':
            writeChar("年",g=1)
        elif i is '=':
            writeChar("月",b=1)
        elif i is '+':
            writeChar("日")
        else:
            drawDigit(eval(i))

def writeChar(char,r=0,g=0,b=0):
    turtle.write(char,font=("Arial",18,"normal"))
    turtle.pencolor(r,g,b)
    turtle.fd(40)

def main():
    turtle.setup(1200,350)
    turtle.speed(0)
    turtle.penup()
    turtle.fd(-450)
    turtle.pensize(5)
    t=time.gmtime()
    ft=time.strftime("%Y-%m=%d+",t)
    print(ft)
    turtle.pencolor(1,0,0)
    drawDate(ft)
    turtle.hideturtle()
    turtle.down()

main()
    

你可能感兴趣的:(python)