turtle庫的學習

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

1 安裝turtle

Python2安裝命令:canvas

pip install turtule

Python3安裝命令:ide

pip3 install turtle

由於turtle庫主要是在Python2中使用的,因此安裝的時候可能會提示錯誤:函數

Command "python setup.py egg_info" failed with error code 1字體

解決方法請參考這裏碼客社區的《Python3安裝turtle提示錯誤:https://oomake.com/question/178949》。spa

2 基礎概念

2.1 畫布(canvas)

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

經常使用的畫布方法有兩個:screensize()setup()blog

(1)turtle.screensize(canvwidth=None, canvheight=None, bg=None)ip

參數分別爲畫布的寬(單位像素), 高, 背景顏色utf-8

如:

turtle.screensize(800, 600, "green")
turtle.screensize() #返回默認大小(400, 300)

(2)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)

2.2 畫筆

在畫布上,默認有一個座標原點爲畫布中心的座標軸, 座標原點上有一隻面朝x軸正方向小烏龜。

這裏咱們描述小烏龜時使用了兩個詞語:標原點(位置),面朝x軸正方向(方向),turtle繪圖中, 就是使用位置方向描述小烏龜(畫筆)的狀態

(1)畫筆的屬性

畫筆有顏色、畫線的寬度等屬性。

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

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

>>> pencolor('brown')
    >>> tup = (0.2, 0.8, 0.55)
    >>> pencolor(tup)
    >>> pencolor()
    '#33cc8c'

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

(2)繪圖命令

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

畫筆運動命令:

命令 說明
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.speed(speed) 畫筆繪製的速度範圍[0,10]整數
turtle.circle() 畫圓,半徑爲正(負),表示圓心在畫筆的左邊(右邊)畫圓

畫筆控制命令:

命令 說明
turtle.pensize(width) 繪製圖形時的寬度
turtle.pencolor() 畫筆顏色
turtle.fillcolor(colorstring) 繪製圖形的填充顏色
turtle.color(color1, color2) 同時設置pencolor=color1, fillcolor=color2
turtle.filling() 返回當前是否在填充狀態
turtle.begin_fill() 準備開始填充圖形
turtle.end_fill() 填充完成;
turtle.hideturtle() 隱藏箭頭顯示;
turtle.showturtle() 與hideturtle()函數對應

全局控制命令

命令 說明
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的參數也是可選項

 

3 繪圖舉例

3.41繪製謝爾賓斯基三角形

# coding: utf-8

import turtle


def draw_triangle(points, color, t):
    t.fillcolor(color)
    t.up()
    t.goto(points[0][0], points[0][1])
    t.down()
    t.begin_fill()
    t.goto(points[1][0], points[1][1])
    t.goto(points[2][0], points[2][1])
    t.goto(points[0][0], points[0][1])
    t.end_fill()


def get_mid(point1, point2):
    return (point1[0] + point2[0]) / 2, (point1[1] + point2[1]) / 2


def sierpinski(points, degree, t):
    color_map = ['blue', 'red', 'green', 'yellow', 'violet', 'orange', 'white',]

    draw_triangle(points, color_map[degree], t)

    if degree > 0:
        sierpinski([points[0], get_mid(points[0], points[1]), get_mid(points[0], points[2])], degree - 1, t)

        sierpinski([points[1], get_mid(points[0], points[1]), get_mid(points[1], points[2])], degree - 1, t)

        sierpinski([points[2], get_mid(points[0], points[2]), get_mid(points[1], points[2])], degree - 1, t)


if __name__ == "__main__"
    t = turtle.Turtle()
    t.speed(5)
    win = turtle.Screen()

    points = [[-100, -50], [0, 100], [100, -50]]
    sierpinski(points, 3, t)

    win.exitonclick()
相關文章
相關標籤/搜索