Python繪制數碼晶體管日期

本文實例為大傢分享瞭Python繪制數碼晶體管日期的具體代碼,供大傢參考,具體內容如下

源碼:

drawLine(draw) 畫一條線
drawDight(dight) 畫一個數字
drawDate(date) 畫一組數字

代碼:

import turtle, time


def drawLine(draw):
 turtle.pendown() if draw else turtle.penup()
 turtle.fd(40)
 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.right(180)
 turtle.penup()
 turtle.fd(20)


def drawDate(date):
 turtle.pencolor("red")
 for i in date:
  if i == '-':
   turtle.write('年', font=("微軟雅黑", 32, "normal"))
   turtle.pencolor("green")
   turtle.fd(80)
  elif i == '=':
   turtle.write('月', font=("微軟雅黑", 32, "normal"))
   turtle.pencolor("blue")
   turtle.fd(80)
  elif i == '+':
   turtle.write('日', font=("微軟雅黑", 32, "normal"))
   turtle.pencolor("red")
   turtle.fd(80)
  elif i == '/':
   turtle.write('時', font=("微軟雅黑", 32, "normal"))
   turtle.pencolor("green")
   turtle.fd(80)
  elif i == '*':
   turtle.write('分', font=("微軟雅黑", 32, "normal"))
   turtle.pencolor("blue")
   turtle.fd(80)
  elif i == '.':
   turtle.write('秒', font=("微軟雅黑", 32, "normal"))
   turtle.fd(80)
  else:
   drawDight(eval(i))


if __name__ == '__main__':
 # turtle.setup() 我用的是pycharm,所以沒有設置畫佈,idle下需要設置
 turtle.penup()
 turtle.fd(-350)
 turtle.pensize(5)
 turtle.speed(1000) # 速度再大也沒感覺瞭
 drawDate(time.strftime('%Y-%m=%d+', time.localtime()))
 # 這裡千萬不要用time.gmtime()),獲取的是UTC時區(0時區)的struct_time
 # 踩瞭半天坑

 turtle.right(90)
 turtle.fd(120)
 turtle.right(90)
 turtle.fd(660)
 turtle.right(180)
 drawDate(time.strftime('%H/%M*%S.', time.localtime()))
 turtle.hideturtle()
 turtle.done()

效果圖:

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: