Turtle庫是Python內置的圖形化模塊,屬於標準庫之一,位於Python安裝目錄的lib文件夾下,經常使用函數有如下幾種:微信
畫筆控制函數ide
penup()
:擡起畫筆;pendown()
:落下畫筆;pensize(width)
:畫筆寬度;pencolor(color)
:畫筆顏色;運動控制函數函數
forward(d)/fd(d)
:直行d個像素;circle(r, extent = None)
:繪製半徑爲r,角度爲extent的弧形,圓心默認在海龜左側距離r的位置;方向控制函數oop
setheading(angle)/seth(angle)
:改變前進方向;left(angle)
:海龜左轉;right(angle)
:海龜右轉;#coding=utf-8 #繪製蟒蛇 import turtle turtle.penup() turtle.pencolor("red") turtle.forward(-250) turtle.pendown() turtle.pensize(10) turtle.right(45) for i in range(4): turtle.circle(40, 80) turtle.circle(-40, 80) turtle.circle(40, 80 / 2) turtle.fd(40) turtle.circle(16, 180) turtle.fd(40 * 2 / 3) turtle.done()
#coding=utf-8 # 繪製五角星 import turtle turtle.pensize(5) turtle.pencolor("red") turtle.forward(200) for i in range(4): turtle.right(144) turtle.fd(200) turtle.done()
#繪製時鐘 # coding=utf-8 import turtle as tt from datetime import * # 當前日期屬於一週的第幾天 def Week(t): week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"] return week[t.weekday()] # 獲取當前時間 def Date(t): y = t.year m = t.month d = t.day cur_hour = t.hour; cur_min = t.minute; cur_sec = t.second; return "%s-%d-%d %d:%02d:%02d" % (y, m, d, cur_hour, cur_min, cur_sec) # 移動畫筆,距離爲distance def movePen(distance): tt.penup() tt.pensize(5) tt.pencolor("blue") tt.fd(distance) tt.pendown() # 繪製錶針 def makeHands(name, length): # 清空窗口,重置turtule狀態爲初始狀態 tt.reset() movePen(-length * 0.1) # 開始記錄多邊形的頂點 tt.begin_poly() tt.fd(length * 1.1) # 中止記錄多邊形的頂點 tt.end_poly() # 返回記錄的多邊形 handForm = tt.get_poly() tt.register_shape(name, handForm) # 初始化 def initial(): global secHand, minHand, hurHand, printer # 重置方向向北(上),正角度爲順時針 tt.mode("logo") # 創建並初始化錶針 makeHands("secHand", 180) makeHands("minHand", 150) makeHands("hurHand", 110) secHand = tt.Turtle() secHand.shape("secHand") minHand = tt.Turtle() minHand.shape("minHand") hurHand = tt.Turtle() hurHand.shape("hurHand") for hand in secHand, minHand, hurHand: hand.shapesize(1, 1, 4) hand.speed(0) # 輸出文字 printer = tt.Turtle() # 隱藏畫筆 printer.hideturtle() printer.penup() # 繪製錶盤外框 def drawClock(R): # 清空窗口,重置turtule狀態爲初始狀態 tt.reset() # 畫筆尺寸 tt.pensize(5) for i in range(60): movePen(R) if i % 5 == 0: tt.fd(20) movePen(-R - 20) movePen(R + 20) if i == 0: # 寫文本 tt.write(int(12), align="center", font=("Consolas", 14, "bold")) elif i == 30: movePen(25) tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold")) movePen(-25) elif (i == 25 or i == 35): movePen(20) tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold")) movePen(-20) else: tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold")) movePen(-R - 20) else: # 繪製指定半徑和顏色的點 tt.dot(5, "red") movePen(-R) tt.right(6) # 錶針的動態顯示 def handsMove(): t = datetime.today() second = t.second + t.microsecond * 0.000001 minute = t.minute + second / 60.0 hour = t.hour + minute / 60.0 secHand.seth(6 * second) minHand.seth(6 * minute) hurHand.seth(30 * hour) tt.tracer(False) printer.fd(65) tt.pencolor("green") printer.write(Week(t), align="center", font = ("黑體", 14)) printer.back(130) printer.write(Date(t), align="center", font = ("Consolas", 14)) # 設置當前畫筆位置爲原點,方向朝東 printer.home() tt.tracer(True) # 通過100ms後繼續調用handsMove函數 tt.ontimer(handsMove, 100) # 調用定義的函數,打開和關閉動畫,爲更新圖紙設置延遲; tt.tracer(False) initial() drawClock(200) tt.tracer(True) handsMove() tt.mainloop()
歡迎關注微信公衆號:村雨1943;創做不易,未經贊成,轉載請註明出處~動畫