七段码、带小数点 、倒计时简单功能的实现
一、七段码的实现以及带小数点的实现
- 将0-9的笔画在计算机中是由规律的书写顺序的,如下图
- 小数点是通过画小圆圈,并填充颜色方式,海龟的位置需要控制好
import turtle, time
def drawGap(): # 绘制数码管间隔
turtle.penup()
turtle.fd(5)
def drawLine(draw): # 绘制单段数码管
drawGap()
turtle.pendown() if draw 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.left(180)
turtle.penup() # 为绘制后续数字确定位置
turtle.fd(20) # 为绘制后续数字确定位置
def drawYD(): # 绘制小数点
turtle.penup()
turtle.seth(-90)
turtle.fd(50)
turtle.pendown()
turtle.color("black")
turtle.begin_fill()
turtle.circle(3, 360)
turtle.end_fill()
turtle.penup()
turtle.bk(50)
turtle.seth(0)
turtle.fd(20)
def drawDate(date): # 获取要输出的数字
for i in date:
if i == '.':
drawYD()
turtle.pencolor("red")
elif i == '-':
turtle.write('年', font=("Arial", 18, "normal"))
turtle.pencolor("green")
turtle.fd(40) # 给字留空间,不然数字和文字就会重叠在一起
elif i == '=':
turtle.write("月", font=("Arial", 18, "normal"))
turtle.pencolor("blue")
turtle.fd(40)
elif i == "+":
turtle.write('日', font=("Arial", 18, "normal"))
turtle.pencolor("orange")
turtle.fd(40)
elif i == "*":
turtle.write('分', font=("Arial", 18, "normal"))
turtle.pencolor("black")
turtle.fd(40)
elif i == "^":
turtle.write('分', font=("Arial", 18, "normal"))
turtle.pencolor("pink")
turtle.fd(40)
elif i == "&":
turtle.write('秒', font=("Arial", 18, "normal"))
turtle.pencolor("purple")
turtle.fd(40)
else:
drawDigit((eval(i)))
def main():
turtle.setup(1500, 350, 200, 200)
turtle.penup()
turtle.fd(-600)
turtle.pensize(5)
drawDate(time.strftime(".%Y-%m=%d+%H*%M^%S&", time.gmtime()))
turtle.hideturtle()
turtle.done()
main()
二、带倒计时的功能
- 使用到time库中的clear功能,可以清屏
- 目前还没有实现更精确的倒计时,以后有机会就来做
import turtle, time
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 countTime(t): # 实现倒计时功能
for i in range(t + 1):
turtle.color("blue")
# turtle.()
drawDigit(t - i)
turtle.clear() # 一秒到清屏
turtle.penup()
turtle.goto(0, 0)
turtle.pencolor("red")
turtle.write("考研加油!!", font=("Arial", 18, "normal"))
def main():
turtle.setup(1500, 350, 200, 200)
turtle.penup()
turtle.pensize(5)
countTime(9)
turtle.hideturtle()
turtle.done()
main()
倒计时简单功能
—方便以后翻看