使用Python的turtle进行七段数码管的绘制(MOOC精品课程内容)
问题分析
刚刚开始学习Python的我们大都对Python的turtle绘图十分感兴趣,诚然,这是一组十分有效的绘图工具,而我们常见的交通灯是使用七段“灯管”来表示0-9的数字的,这是一种很醒目也很节约材料的方式,那么我们如何使用Python语言,来实现绘制“数字”的过程呢?
代码实现
1.绘制单段数码管
def drawLine(draw):
drawGap()#ps:就是让年月日之间出现间隔的函数...
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
drawGap()#ps:就是让年月日之间出现间隔的函数...
turtle.right(90)
(这里的drawGap()函数会马上出现的)
2.根据数字绘制七段数码管
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)
3.获得日期和“年月日”
def drawDate(date):#获得日期
turtle.pencolor("red")
for i in date:
if 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"))
else:
drawDight(eval(i))#把字符数字变成数字,然后打印成数码管
4.drawGap()函数,留出写下“年月日”的间隔
def drawGap():#间隔
turtle.penup()
turtle.fd(5)
5.主函数,顺便确定显示框、海龟运动起始点等
def main():
turtle.setup(800, 350, 200, 200)
turtle.penup()
turtle.fd(-300)
turtle.pensize(5)
drawDate(time.strftime('%Y-%m=%d+',time.gmtime()))
turtle.drawDate('20180608')
turtle.hideturtle()
turtle.done()
main()
源代码
#七段数码管的绘制(就是交通灯上显示数字的那种)
import turtle,time
def drawLine(draw):
drawGap()#ps:就是让年月日之间出现间隔的函数...
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
drawGap()#ps:就是让年月日之间出现间隔的函数...
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):#获得日期
turtle.pencolor("red")
for i in date:
if 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"))
else:
drawDight(eval(i))#把字符数字变成数字,然后打印成数码管
def drawGap():#间隔
turtle.penup()
turtle.fd(5)
def main():
turtle.setup(800, 350, 200, 200)
turtle.penup()
turtle.fd(-300)
turtle.pensize(5)
drawDate(time.strftime('%Y-%m=%d+',time.gmtime()))
turtle.drawDate('20180608')
turtle.hideturtle()
turtle.done()
main()
结果显示