用turtle繪圖體系繪製7段數碼管:
1:繪製單個數字對應的數碼管
2:獲得一串數字,繪製對應的數碼管
3:獲得當前系統時間,繪製對應的數碼管
**如下:
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 drawdate(date): #date為日期,格式為「%y-%m=%d+」
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(-300)
turtle.pensize(5)
drawdate(time.strftime("%y-%m=%d+",time.gmtime()))
#drawdate("20190228")
turtle.hideturtle()
turtle.done()
main()
結果如下:
7段數碼管繪製
import turtle,time def drawgap turtle.speed 0 設定畫筆速度 fastest 0 fast 10 normal 6 slow 3 slowest 1 turtle.turtle screen.delay 0 畫筆速度無延遲turtle.penup turt...
7段數碼管繪製
import turtle,datetime def drawgap 繪製數碼管間隔 turtle.penup turtle.fd 5 def drawline draw 繪製單段數碼管 drawgap turtle.pendown if draw else turtle.penup turtle....
7段數碼管繪製
import turtle,datetime def drawgap 繪製數碼管間隔 turtle.penup turtle.fd 5 def drawline draw 繪製單段數碼管 drawgap turtle.pendown if draw else turtle.penup turtle....