书上的7段数码管,显示时间
书上的7断数码管显示应功都看过了,这里顺便提供一下`
import turtle, datetime
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(d):
drawLine(True) if d in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,1,3,4,5,6,7,8,9] else drawLine(False)
drawLine(True) if d in [0,2,3,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,2,6,8] else drawLine(False)
turtle.left(90)
drawLine(True) if d in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if d in [0,2,3,5,6,7,8,9] else drawLine(False)
drawLine(True) if d 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:
drawDigit(eval(i))
def main():
turtle.setup(800, 350, 200, 200)
turtle.penup()
turtle.fd(-350)
turtle.pensize(5)
drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
turtle.hideturtle()
main()
看看显示原理
我们把这里所有的False全部改为True,就可以明白7段是怎么绘制的了. 改完运行下,就知道了.
14段模型
参考下网上的14段图片
通过小学知识可以知道,这里我们得写14笔,完成一个14位数字管的. 画了一个顺序草图 大概就这样把,然后上代码就完事了.
14位数码管,画英文,和数字
import datetime
import turtle
name = []
def drawLine(draw):
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
turtle.right(90)
def drawLine1(draw):
turtle.pendown() if draw else turtle.penup()
turtle.fd(20)
def drawtdown1(draw):
turtle.pendown() if draw else turtle.penup()
turtle.fd(44.7)
def drawDigit(digit):
drawLine1(True) if digit in [2, 4, 5, 6, 8, 9, 'A', 'E', 'F', 'H', 'K', 'P', 'R', 'S'] else drawLine1(False)
drawLine1(True) if digit in [2, 3, 4, 5, 6, 8, 9,'F', 'A','B','G','H','P,'R',S'] else drawLine1(False)
turtle.right(90)
drawLine(True) if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'D', 'G', 'H', 'J', 'M', 'N' , 'O', 'Q', 'S', 'U', 'W'] else drawLine(False)
drawLine(True) if digit in ['L',0, 2, 3, 5, 6, 8, 9, 'B', 'C', 'D', 'E', 'G', 'I', 'U','J', 'O', 'Q', 'S', 'Z'] else drawLine(False)
drawLine(True) if digit in ['L',0, 2, 6, 8, 'A', 'C' , 'E' , 'F' , 'G', 'H' , 'J' , 'K' , 'L' , 'M' , 'N' ,'O' , 'P' , 'Q' ,'R' , 'U' , 'V' , 'W'] else drawLine(False)
turtle.left(90)
drawLine(True) if digit in ['L',0, 4, 5, 6, 8, 9, 'A','C','E','F','G','H','K','M','N','O','P','Q','R','S','U','V','W'] else drawLine(False)
drawLine(True) if digit in [0, 2, 3, 5, 6, 7, 8, 9, 'A','B','C','D','E','F','G','O','P','Q','R','I','S','T','Z'] else drawLine(False)
drawLine(True) if digit in [0, 1, 2, 3, 4, 7, 8, 9, 'A','B','D','H','J','M','N','O','P','Q','R','U','W'] else drawLine(False)
turtle.left(90)
turtle.penup()
turtle.fd(40)
turtle.right(90 + 63.4)
drawtdown1(True) if digit in ['K','N','Q','K','N','Q','R','W','X'] else drawtdown1(False)
drawtdown1(True) if digit in ['M','N','X','Y'] else drawtdown1(False)
turtle.right(90 + 26.6)
turtle.penup()
turtle.fd(20)
turtle.right(90)
drawLine(True) if digit in ['B','D','I','T'] else drawLine(False)
turtle.left(90)
drawLine(True) if digit in ['B','D','I','T','Y'] else drawLine(False)
turtle.penup()
turtle.fd(20)
turtle.right(90 + 26.6)
drawtdown1(True) if digit in ['V','X','Z','W', 0] else drawtdown1(False)
drawtdown1(True) if digit in ['M','V','X','Y','Z','K', 0, 1] else drawtdown1(False)
turtle.right(90 + 63.4)
turtle.penup()
turtle.fd(40)
turtle.left(90)
turtle.fd(20)
def drawDate(date):
turtle.pencolor("black")
for i in date:
if i == '-':
turtle.write('年', font=("Arial", 25, "normal"))
turtle.pencolor("black")
turtle.fd(40)
elif i == '=':
turtle.write('月', font=("Arial", 25, "normal"))
turtle.pencolor("black")
turtle.fd(40)
elif i == '+':
turtle.write('日', font=("Arial", 25, "normal"))
else:
drawDigit(eval(i))
def drawname():
name = ['L', 'O', 'N', 'G',' ', 'T', 'A', 'O']
for i in name:
drawDigit(i)
def main():
turtle.setup(800, 350, 200, 200)
turtle.penup()
turtle.goto(-300, 50)
turtle.pensize(5)
turtle.pencolor("black")
turtle.speed(0)
drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
turtle.penup()
turtle.goto(-200, -100)
turtle.pencolor('red')
drawname()
turtle.hideturtle()
turtle.exitonclick()
main()
运行就是这样啦,这里画了字母和数字
想画自己名字字母的,在name 里面添加名字字母的字符就行
下面又搞了个字体会变颜色的,还有背景闪烁的,嘿嘿
import datetime
import turtle
import random
name = []
def random_color():
colors1 = '0123456789ABCDEF'
num = "#"
for i in range(6):
num += random.choice(colors1)
return num
def drawLine(draw):
turtle.pencolor(random_color())
turtle.bgcolor(random_color())
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
turtle.right(90)
def drawLine1(draw):
turtle.pencolor(random_color())
turtle.bgcolor(random_color())
turtle.pendown() if draw else turtle.penup()
turtle.fd(20)
def drawtdown1(draw):
turtle.pencolor(random_color())
turtle.bgcolor(random_color())
turtle.pendown() if draw else turtle.penup()
turtle.fd(44.7)
def drawDigit(digit):
drawLine1(True) if digit in [2, 4, 5, 6, 8, 9, 'A', 'E', 'F', 'H', 'K', 'P', 'R', 'S'] else drawLine1(False)
drawLine1(True) if digit in [2, 3, 4, 5, 6, 8, 9, 'F', 'A', 'B', 'G', 'H', 'P,'R',S'] else drawLine1(False)
turtle.right(90)
drawLine(True) if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'D', 'G', 'H', 'J', 'M', 'N', 'O', 'Q', 'S', 'U',
'W'] else drawLine(False)
drawLine(True) if digit in ['L', 0, 2, 3, 5, 6, 8, 9, 'B', 'C', 'D', 'E', 'G', 'I', 'U', 'J', 'O', 'Q', 'S',
'Z'] else drawLine(False)
drawLine(True) if digit in ['L', 0, 2, 6, 8, 'A', 'C', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
'R', 'U', 'V', 'W'] else drawLine(False)
turtle.left(90)
drawLine(True) if digit in ['L', 0, 4, 5, 6, 8, 9, 'A', 'C', 'E', 'F', 'G', 'H', 'K', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'U', 'V', 'W'] else drawLine(False)
drawLine(True) if digit in [0, 2, 3, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'O', 'P', 'Q', 'R', 'I', 'S',
'T', 'Z'] else drawLine(False)
drawLine(True) if digit in [0, 1, 2, 3, 4, 7, 8, 9, 'A', 'B', 'D', 'H', 'J', 'M', 'N', 'O', 'P', 'Q', 'R', 'U',
'W'] else drawLine(False)
turtle.left(90)
turtle.penup()
turtle.fd(40)
turtle.right(90 + 63.4)
drawtdown1(True) if digit in ['K', 'N', 'Q', 'K', 'N', 'Q', 'R', 'W', 'X'] else drawtdown1(False)
drawtdown1(True) if digit in ['M', 'N', 'X', 'Y'] else drawtdown1(False)
turtle.right(90 + 26.6)
turtle.penup()
turtle.fd(20)
turtle.right(90)
drawLine(True) if digit in ['B', 'D', 'I', 'T'] else drawLine(False)
turtle.left(90)
drawLine(True) if digit in ['B', 'D', 'I', 'T', 'Y'] else drawLine(False)
turtle.penup()
turtle.fd(20)
turtle.right(90 + 26.6)
drawtdown1(True) if digit in ['V', 'X', 'Z', 'W', 0] else drawtdown1(False)
drawtdown1(True) if digit in ['M', 'V', 'X', 'Y', 'Z', 'K', 0, 1] else drawtdown1(False)
turtle.right(90 + 63.4)
turtle.penup()
turtle.fd(40)
turtle.left(90)
turtle.fd(20)
def drawDate(date):
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:
drawDigit(eval(i))
def drawname():
name = ['L', 'O', 'N', 'G', ' ', 'T', 'A', 'O']
for i in name:
drawDigit(i)
def main():
turtle.setup(800, 350, 200, 200)
turtle.penup()
turtle.goto(-300, 50)
turtle.pensize(5)
turtle.bgcolor(random_color())
turtle.speed(0)
drawDate(datetime.datetime.now().strftime('%Y-%m=%d+'))
turtle.penup()
turtle.goto(-230, -100)
drawname()
turtle.hideturtle()
turtle.exitonclick()
main()
这里就直接修改了书上的代码,复制过去就可以用了. 小白一个,望大佬们多多指教
|