Turtle绘图打印时间

Turtle绘图打印时间

数字时间为绘制七段数码管得到

import turtle
import datetime
import time


def draw_gap():  # 绘制数码间隔
    turtle.penup()
    turtle.fd(5)


def draw_line(draw):  # 绘制单段数码管
    draw_gap()
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    draw_gap()
    turtle.right(90)


def draw_digit(d): # 根据数字绘制七段数码管
    draw_line(True) if d in [2, 3, 4, 5, 6, 8, 9] else draw_line(False)
    draw_line(True) if d in [0, 1, 3, 4, 5, 6, 7, 8, 9] else draw_line(False)
    draw_line(True) if d in [0, 2, 3, 5, 6, 8, 9] else draw_line(False)
    draw_line(True) if d in [0, 2, 6, 8] else draw_line(False)
    turtle.left(90)
    draw_line(True) if d in [0, 4, 5, 6, 8, 9] else draw_line(False)
    draw_line(True) if d in [0, 2, 3, 5, 6, 7, 8, 9] else draw_line(False)
    draw_line(True) if d in [0, 1, 2, 3, 4, 7, 8, 9] else draw_line(False)
    turtle.left(180)
    turtle.penup()
    turtle.fd(20)


def draw_date(date):
    turtle.pencolor("blue")
    for i in date:
        if i == '-':
            turtle.write('时', font=("Arial", 18, "normal"))
            turtle.fd(40)
        elif i == '=':
            turtle.write('分', font=("Arial", 18, "normal"))
            turtle.fd(40)
        elif i == '+':
            turtle.write('秒', font=("Arial", 18, "normal"))
        else:
            draw_digit(eval(i))


def again(s):
    for i in range(1, s):
        turtle.speed(0)
        turtle.penup()
        turtle.fd(-250)
        turtle.pensize(5)
        draw_date(datetime.datetime.now().strftime('%H-%M=%S+'))
        time.sleep(0.5)
        turtle.reset()
        turtle.hideturtle()


def main():
    turtle.setup(800, 350, 200, 200)
    again(100000)


main()
posted @ 2023-10-18 09:08  SnowDreamXUE  阅读(11)  评论(0编辑  收藏  举报  来源