#Python 七段数码电子管绘制

编写步骤:
        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()

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值