Turtle庫的學習積累

1.什麼是turtle庫ide

  Turtle庫是Python語言中一個很流行的繪製圖像的函數庫,想象一個小烏龜,在一個橫軸爲x、縱軸爲y的座標系原點,(0,0)位置開始,它根據一組函數指令的控制,在這個平面座標系中移動,從而在它爬行的路徑上繪製了圖形。函數

2.turtle繪製圖像基礎oop

2.1 畫布字體

  畫布就是turtle爲咱們展開用於繪圖區域,咱們能夠設置它的大小和初始位置。動畫

        設置畫布大小spa

width : 窗口寬度,值爲整數則對應像素值;值爲小數則爲窗口寬度與屏幕的比例。3d

height : 窗口高度,值爲整數則對應像素值;值爲小數則爲窗口高度與屏幕的比例。orm

startx : 窗口左側與屏幕左側的像素距離,值爲None則窗口位於窗口水平中央。blog

starty : 窗口右側與屏幕右側的像素距離,值爲None則窗口位於窗口垂直中央。事件

         turtle.screensize(canvwidth=None, canvheight=None, bg=None),參數分別爲畫布的寬(單位像素), 高, 背景顏色。

        如:turtle.screensize(800,600, "green")

               turtle.screensize() #返回默認大小(400, 300)

        turtle.setup(width=0.5, height=0.75, startx=None, starty=None),參數:width, height: 輸入寬和高爲整數時, 表示像素; 爲小數時, 表示佔據電腦屏幕的比例,(startx, starty): 這一座標表示矩形窗口左上角頂點的位置, 若是爲空,則窗口位於屏幕中心。

        如:turtle.setup(width=0.6,height=0.6)

               turtle.setup(width=800,height=800, startx=100, starty=100)

setup ( width , height , startx , starty )

 


 2.2畫筆:

2.2.1 畫筆的狀態:

        在畫布上,默認有一個座標原點爲畫布中心的座標軸,座標原點上有一隻面朝x軸正方向小烏龜。這裏咱們描述小烏龜時使用了兩個詞語:座標原點(位置),面朝x軸正方向(方向), turtle繪圖中,就是使用位置方向描述小烏龜(畫筆)的狀態。

2.2.2 畫筆的屬性:

        畫筆(畫筆的屬性,顏色、畫線的寬度等)

        1) turtle.pensize():設置畫筆的寬度;

        2) turtle.pencolor():沒有參數傳入,返回當前畫筆顏色,傳入參數設置畫筆顏色,能夠是字符串如"green", "red",也能夠是RGB 3元組。

        3) turtle.speed(speed):設置畫筆移動速度,畫筆繪製的速度範圍[0,10]整數,數字越大越快。

2.2.3 繪圖命令

         操縱海龜繪圖有着許多的命令,這些命令能夠劃分爲3種:一種爲運動命令,一種爲畫筆控制命令,還有一種是全局控制命令。

(1)    畫筆運動命令

命令

說明

turtle.forward(distance)

向當前畫筆方向移動distance像素長度

turtle.backward(distance)

向當前畫筆相反方向移動distance像素長度

turtle.right(degree)

順時針移動degree°

turtle.left(degree)

逆時針移動degree°

turtle.pendown()

移動時繪製圖形,缺省時也爲繪製

turtle.goto(x,y)

將畫筆移動到座標爲x,y的位置

turtle.penup()

提起筆移動,不繪製圖形,用於另起一個地方繪製

turtle.circle()

畫圓,半徑爲正(負),表示圓心在畫筆的左邊(右邊)畫圓

setx( )

將當前x軸移動到指定位置

sety( )

將當前y軸移動到指定位置

setheading(angle)

設置當前朝向爲angle角度

home()

設置當前畫筆位置爲原點,朝向東。

dot(r)

繪製一個指定直徑和顏色的圓點

 

(2)     畫筆控制命令

命令

說明

turtle.fillcolor(colorstring)

繪製圖形的填充顏色

turtle.color(color1, color2)

同時設置pencolor=color1, fillcolor=color2

turtle.filling()

返回當前是否在填充狀態

turtle.begin_fill()

準備開始填充圖形

turtle.end_fill()

填充完成

turtle.hideturtle()

隱藏畫筆的turtle形狀

turtle.showturtle()

顯示畫筆的turtle形狀

 

(3)    全局控制命令

命令

說明

turtle.clear()

清空turtle窗口,可是turtle的位置和狀態不會改變

turtle.reset()

清空窗口,重置turtle狀態爲起始狀態

turtle.undo()

撤銷上一個turtle動做

turtle.isvisible()

返回當前turtle是否可見

stamp()

複製當前圖形

turtle.write(s [,font=("font-name",font_size,"font_type")])

寫文本,s爲文本內容,font是字體的參數,分別爲字體名稱,大小和類型;font爲可選項,font參數也是可選項

 

(4)    其餘命令

命令

說明

turtle.mainloop()或turtle.done()

啓動事件循環 -調用Tkinter的mainloop函數。

必須是烏龜圖形程序中的最後一個語句。

turtle.mode(mode=None)

設置烏龜模式(「standard」,「logo」或「world」)並執行重置。若是沒有給出模式,則返回當前模式。

模式

初始龜標題

正角度

standard

向右(東)

逆時針

logo

向上(北)

順時針

turtle.delay(delay=None)

設置或返回以毫秒爲單位的繪圖延遲。

turtle.begin_poly()

開始記錄多邊形的頂點。當前的烏龜位置是多邊形的第一個頂點。

turtle.end_poly()

中止記錄多邊形的頂點。當前的烏龜位置是多邊形的最後一個頂點。將與第一個頂點相連。

turtle.get_poly()

返回最後記錄的多邊形。

 

2.3. 命令詳解

        turtle.circle(radius, extent=None, steps=None)

        描述:以給定半徑畫圓

        參數:

        radius(半徑):半徑爲正(負),表示圓心在畫筆的左邊(右邊)畫圓;

        extent(弧度) (optional);

        steps (optional) (作半徑爲radius的圓的內切正多邊形,多邊形邊數爲steps)。

       舉例:

       circle(50) # 整圓;

       circle(50,steps=3) # 三角形;

       circle(120, 180) # 半圓

#RGB色彩體系

 rgb的色彩取值範圍爲0-255的整數或者0-1的小數

  

  

 

切換RBG模式

turtle.colormode(mode)

  1.0:RGB小數模式

  255:RGB整數模式

2.4.形狀繪製函數

turtle.fd(distance) / forward(distance) 做用:向小烏龜前進的方向前進distance距離,distance 爲前進距離的像素值

turtle.bk(distance) / backward(distance)  做用:向小烏龜前進的方向後退distance距離,distance 爲後退距離的像素值

turtle.lt(angle) / left(angle) 做用:左轉,angle 爲左轉的角度

turtle.rt(angle) / right(angle) 做用:angle 爲右轉的角度

turtle.seth(angle) / setheading(angle)  做用:angle 爲轉到的角度,即改變繪圖的方向

turtle.circle(radius [,extent])  做用:繪製以radius爲半徑、以angle爲角度弧形

  (extent 爲None時,繪製整個圓;radius 爲正數時,繪製的圖形在小海龜左側;radius 爲負數時,繪製的圖形在小海龜右側)

turtle.ht() / hideturtle()  做用:隱藏小海龜

turtle.st() / showturtle()  做用:顯示小海龜

turtle.fillcolor(colorstring)  做用:填充顏色

turtle.begin_fill()  做用:開始填充圖形

turtle.end_fill()  做用:填充完成

2.5 控制命令

命令

說明

home()

使小海龜回到初始位置 (0,0)

clear()

清空窗口,但小海龜的位置和狀態不變

reset()

清空窗口,且重置小海龜的狀態爲初始狀態

undo()

撤銷上一個動做

stamp()

複製當前圖形

write(s [,font=(「font_name」,font_size,」font_type」)])

文本,s 爲文本內容,font 是字體的參數,內部分別是字體名稱,大小和類型

 

3.繪製圖像實例

(1)玫瑰花繪製

 

import turtle as t               # 定義一個曲線繪製函數(用t來代替turtle)
def DegreeCurve(n, r, d=1):
for i in range(n):
t.left(d)
t.circle(r, abs(d))                # 初始位置設定
s = 0.2 # 尺寸
t.setup(450*5*s, 750*5*s)            #設置窗口大小
t.pencolor("black")              #畫筆顏色
t.fillcolor("red")                 #填充顏色
t.speed(100)                 #加速
t.penup()                    #下筆
t.goto(0, 900*s)
t.pendown()                  # 繪製花朵形狀
t.begin_fill()                    #開始
t.circle(200*s,30)
DegreeCurve(60, 50*s)
t.circle(200*s,30)
DegreeCurve(4, 100*s)
t.circle(200*s,50)
DegreeCurve(50, 50*s)
t.circle(350*s,65)
DegreeCurve(40, 70*s)
t.circle(150*s,50)
DegreeCurve(20, 50*s, -1)
t.circle(400*s,60)
DegreeCurve(18, 50*s)
t.fd(250*s)
t.right(150)
t.circle(-500*s,12)
t.left(140)
t.circle(550*s,110)
t.left(27)
t.circle(650*s,100)
t.left(130)
t.circle(-300*s,20)
t.right(123)
t.circle(220*s,57)
t.end_fill()                  # 結束花瓣的繪製
t.left(120)                 # 開始繪製花枝
t.fd(280*s)
t.left(115)
t.circle(300*s,33)
t.left(180)
t.circle(-300*s,33)
DegreeCurve(70, 225*s, -1)
t.circle(350*s,104)
t.left(90)
t.circle(200*s,105)
t.circle(-500*s,63)
t.penup()
t.goto(170*s,-30*s)
t.pendown()
t.left(160)
DegreeCurve(20, 2500*s)
DegreeCurve(220, 250*s, -1)          # 結束
t.fillcolor('green')                # 繪製一個綠色葉子
t.penup()
t.goto(670*s,-180*s)
t.pendown()
t.right(140)
t.begin_fill()
t.circle(300*s,120)
t.left(60)
t.circle(300*s,120)
t.end_fill()
t.penup()
t.goto(180*s,-550*s)
t.pendown()
t.right(85)
t.circle(600*s,40)              # 結束
t.penup()                  # 繪製另外一個綠色葉子
t.goto(-150*s,-1000*s)
t.pendown()
t.begin_fill()
t.rt(120)
t.circle(300*s,115)
t.left(75)
t.circle(300*s,100)
t.end_fill()
t.penup()
t.goto(430*s,-1070*s)
t.pendown()
t.right(30)
t.circle(-600*s,35)
t.done()                 #玫瑰花結束繪製

 

 

 

 

 (2)時鐘程序

# coding=utf-8  

import turtle  

from datetime import *   # 擡起畫筆,向前運動一段距離放下  

def Skip(step):  

 turtle.penup()  

 turtle.forward(step)  

  turtle.pendown()  

def mkHand(name, length):  

    # 註冊Turtle形狀,創建錶針Turtle  

    turtle.reset()  

    Skip(-length * 0.1)  

    # 開始記錄多邊形的頂點。當前的烏龜位置是多邊形的第一個頂點。  

    turtle.begin_poly()  

    turtle.forward(length * 1.1)  

    # 中止記錄多邊形的頂點。當前的烏龜位置是多邊形的最後一個頂點。將與第一個頂點相連。  

    turtle.end_poly()  

    # 返回最後記錄的多邊形。  

    handForm = turtle.get_poly()  

    turtle.register_shape(name, handForm)  

   

def Init():  

    global secHand, minHand, hurHand, printer  

    # 重置Turtle指向北  

    turtle.mode("logo")  

    # 創建三個錶針Turtle並初始化  

    mkHand("secHand", 135)  

    mkHand("minHand", 125)  

    mkHand("hurHand", 90)  

    secHand = turtle.Turtle()  

    secHand.shape("secHand")  

    minHand = turtle.Turtle()  

    minHand.shape("minHand")  

    hurHand = turtle.Turtle()  

    hurHand.shape("hurHand")  

     

    for hand in secHand, minHand, hurHand:  

        hand.shapesize(1, 1, 3)  

        hand.speed(0)  

     

    # 創建輸出文字Turtle  

    printer = turtle.Turtle()  

    # 隱藏畫筆的turtle形狀  

    printer.hideturtle()  

    printer.penup()  

      

def SetupClock(radius):  

    # 創建表的外框  

    turtle.reset()  

    turtle.pensize(7)  

    for i in range(60):  

        Skip(radius)  

        if i % 5 == 0:  

            turtle.forward(20)  

            Skip(-radius - 20)  

             

            Skip(radius + 20)  

            if i == 0:  

                turtle.write(int(12), align="center", font=("Courier", 14, "bold"))  

            elif i == 30:  

                Skip(25)  

                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))  

                Skip(-25)  

            elif (i == 25 or i == 35):  

                Skip(20)  

                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))  

                Skip(-20)  

            else:  

                turtle.write(int(i/5), align="center", font=("Courier", 14, "bold"))  

            Skip(-radius - 20)  

        else:  

            turtle.dot(5)  

            Skip(-radius)  

        turtle.right(6)  

          

def Week(t):     

    week = ["星期一", "星期二", "星期三",  

            "星期四", "星期五", "星期六", "星期日"]  

    return week[t.weekday()]  

   

def Date(t):  

    y = t.year  

    m = t.month  

    d = t.day  

    return "%s %d%d" % (y, m, d)  

   

def Tick():  

    # 繪製錶針的動態顯示  

    t = datetime.today()  

    second = t.second + t.microsecond * 0.000001  

    minute = t.minute + second / 60.0  

    hour = t.hour + minute / 60.0  

    secHand.setheading(6 * second)  

    minHand.setheading(6 * minute)  

    hurHand.setheading(30 * hour)  

      

    turtle.tracer(False)   

    printer.forward(65)  

    printer.write(Week(t), align="center",  

                  font=("Courier", 14, "bold"))  

    printer.back(130)  

    printer.write(Date(t), align="center",  

                  font=("Courier", 14, "bold"))  

    printer.home()  

    turtle.tracer(True)  

   

    # 100ms後繼續調用tick  

    turtle.ontimer(Tick, 100)  

   

def main():  

    # 打開/關閉龜動畫,併爲更新圖紙設置延遲。  

    turtle.tracer(False)  

    Init()  

    SetupClock(160)  

    turtle.tracer(True)  

    Tick()  

    turtle.mainloop()  

   

if __name__ == "__main__":  

    main()  

 

相關文章
相關標籤/搜索