import turtle
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 drawdate(date):#獲得想要輸出的數字
for i in date:
drawdigit(eval(i))#將數字變為整數
def main():
turtle.setup(800,350,200,200)
turtle.penup()
turtle.fd(-300)
turtle.pensize(5)
drawdate('20181010')
turtle.hideturtle()
turtle.done()
main()
執行結果:
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 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()))
turtle.hideturtle()
turtle.done()
main()
執行結果:
繪製七段數碼管
seven digits draw v1.0 第一,繪製線 第二,繪製七段數碼管組成的數字 第三,繪製優化數碼管間距 第四,繪製多組數字 第五,獲取系統時間 第六,主函式執行程式。如下 import turtle as t import time def drawline draw drawgap ...
七段數碼管繪製
步驟1 繪製單個數字對應的數碼管 步驟2 獲得一串數字,繪製對應的數碼管 步驟2 獲得當前系統時間,繪製對應的數碼管 步驟1 繪製單個數字對應的數碼管 基本問題是繪製一條線,可將其定義為乙個基本函式 def drawline draw turtle.pendown if draw else turt...
七段數碼管繪製
七段數碼管繪製 import turtle as t import time as t def drawgap 繪製數碼管的間隔 t.penup t.fd 5 def drawline draw 繪製單段數碼管,draw為true時,則實畫 drawgap t.pendown if draw els...