Python3 turtle安裝和使用教程

來源:互聯網
上載者:User

標籤:中移動   err   -name   star   返回   描述   tle   type   exit   

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

1 安裝turtle

Python2安裝命令:

pip install turtulem

Python3安裝命令:

pip3 install turtle

因為turtle庫主要是在Python2中使用的,所以安裝的時候可能會提示錯誤:

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

解決方案請參考這裡碼客社區的《Python3安裝turtle提示錯誤:Command "python setup.py egg_info" failed with error code 1》。

2 基礎概念2.1 畫布(canvas)

畫布就是turtle為我們展開用於繪圖區域, 我們可以設定它的大小和初始位置。

常用的畫布方法有兩個:screensize()和setup()。

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

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

如:

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.1 太陽花

import turtle as timport timet.color("red", "yellow")t.speed(10)t.begin_fill()for _ in range(50):    t.forward(200)    t.left(170)end_fill()time.sleep(1)
3.2 繪製小蟒蛇

import turtledef drawSnake(rad, angle, len, neckrad):    for _ in range(len):        turtle.circle(rad, angle)        turtle.circle(-rad, angle)    turtle.circle(rad, angle/2)    turtle.forward(rad/2)  # 直線前進    turtle.circle(neckrad, 180)    turtle.forward(rad/4)if __name__ == "__main__":   turtle.setup(1500, 1400, 0, 0)   turtle.pensize(30)  # 畫筆尺寸   turtle.pencolor("green")   turtle.seth(-40)    # 前進的方向   drawSnake(70, 80, 2, 15)
3.3 繪製五角星

import turtleimport timeturtle.pensize(5)turtle.pencolor("yellow")turtle.fillcolor("red")turtle.begin_fill()for _ in range(5):    turtle.forward(200)    turtle.right(144)turtle.end_fill()time.sleep(2)turtle.penup()turtle.goto(-150,-120)turtle.color("violet")turtle.write("Done", font=(‘Arial‘, 40, ‘normal‘))time.sleep(1)
3.4 繪製謝爾賓斯基三角形

import turtledef 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]) / 2def 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()

Python3 turtle安裝和使用教程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.