標籤:pre font 參數 基礎 starty 身體 表示 === hit
python2.6版本中後引入的一個簡單的繪圖工具,叫做海龜繪圖(Turtle Graphics),turtle庫是python的內部庫,使用匯入即可 import turtle
先說明一下turtle繪圖的基礎知識:
1. 畫布(canvas)
畫布就是turtle為我們展開用於繪圖區域, 我們可以設定它的大小和初始位置
1.1 設定畫布大小
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)
2. 畫筆2.1 畫筆的狀態
在畫布上,預設有一個座標原點為畫布中心的座標軸, 座標原點上有一隻面朝x軸正方向小烏龜. 這裡我們描述小烏龜時使用了兩個詞語:座標原點(位置),面朝x軸正方向(方向), turtle繪圖中, 就是使用位置方向描述小烏龜(畫筆)的狀態
2.2 畫筆的屬性
畫筆(畫筆的屬性,顏色、畫線的寬度)
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 繪圖命令
操縱海龜繪圖有著許多的命令,這些命令可以劃分為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.speed(speed) |
畫筆繪製的速度範圍[0,10]整數 |
| turtle.circle() |
畫圓,半徑為正(負),表示圓心在畫筆的左邊(右邊)畫圓 |
(2)畫筆控制命令:
| 命令 |
說明 |
| 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()函數對應 |
(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的參數也是可選項 |
3. 命令詳解
3.1 turtle.circle(radius, extent=None, steps=None)
描述: 以給定半徑畫圓
參數:
radius(半徑); 半徑為正(負),表示圓心在畫筆的左邊(右邊)畫圓
extent(弧度) (optional);
steps (optional) (做半徑為radius的圓的內切正多邊形,多邊形邊數為steps)
舉例:
circle(50) # 整圓;
circle(50,steps=3) # 三角形;
circle(120, 180) # 半圓
4. 繪圖舉例4.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)
4.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)
4.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)
4.4 小豬佩奇
感謝江城青椒肉絲的小豬佩奇, 很傳神:)
# coding:utf-8import turtle as t# 繪製小豬佩奇# =======================================t.pensize(4)t.hideturtle()t.colormode(255)t.color((255, 155, 192), "pink")t.setup(840, 500)t.speed(10)# 鼻子t.pu()t.goto(-100,100)t.pd()t.seth(-30)t.begin_fill()a = 0.4for i in range(120): if 0 <= i < 30 or 60 <= i < 90: a = a+0.08 t.lt(3) # 向左轉3度 t.fd(a) # 向前走a的步長 else: a = a-0.08 t.lt(3) t.fd(a) t.end_fill()t.pu()t.seth(90)t.fd(25)t.seth(0)t.fd(10)t.pd()t.pencolor(255, 155, 192)t.seth(10)t.begin_fill()t.circle(5)t.color(160, 82, 45)t.end_fill()t.pu()t.seth(0)t.fd(20)t.pd()t.pencolor(255, 155, 192)t.seth(10)t.begin_fill()t.circle(5)t.color(160, 82, 45)t.end_fill()# 頭t.color((255, 155, 192), "pink")t.pu()t.seth(90)t.fd(41)t.seth(0)t.fd(0)t.pd()t.begin_fill()t.seth(180)t.circle(300, -30)t.circle(100, -60)t.circle(80, -100)t.circle(150, -20)t.circle(60, -95)t.seth(161)t.circle(-300, 15)t.pu()t.goto(-100, 100)t.pd()t.seth(-30)a = 0.4for i in range(60): if 0 <= i < 30 or 60 <= i <90: a = a+0.08 t.lt(3) # 向左轉3度 t.fd(a) # 向前走a的步長 else: a = a-0.08 t.lt(3) t.fd(a) t.end_fill()# 耳朵t.color((255, 155, 192), "pink")t.pu()t.seth(90)t.fd(-7)t.seth(0)t.fd(70)t.pd()t.begin_fill()t.seth(100)t.circle(-50, 50)t.circle(-10, 120)t.circle(-50, 54)t.end_fill()t.pu()t.seth(90)t.fd(-12)t.seth(0)t.fd(30)t.pd()t.begin_fill()t.seth(100)t.circle(-50, 50)t.circle(-10, 120)t.circle(-50, 56)t.end_fill()#眼睛t.color((255, 155, 192), "white")t.pu()t.seth(90)t.fd(-20)t.seth(0)t.fd(-95)t.pd()t.begin_fill()t.circle(15)t.end_fill()t.color("black")t.pu()t.seth(90)t.fd(12)t.seth(0)t.fd(-3)t.pd()t.begin_fill()t.circle(3)t.end_fill()t.color((255, 155, 192), "white")t.pu()t.seth(90)t.fd(-25)t.seth(0)t.fd(40)t.pd()t.begin_fill()t.circle(15)t.end_fill()t.color("black")t.pu()t.seth(90)t.fd(12)t.seth(0)t.fd(-3)t.pd()t.begin_fill()t.circle(3)t.end_fill()# 腮t.color((255, 155, 192))t.pu()t.seth(90)t.fd(-95)t.seth(0)t.fd(65)t.pd()t.begin_fill()t.circle(30)t.end_fill()# 嘴t.color(239, 69, 19)t.pu()t.seth(90)t.fd(15)t.seth(0)t.fd(-100)t.pd()t.seth(-80)t.circle(30, 40)t.circle(40, 80)# 身體t.color("red", (255, 99, 71))t.pu()t.seth(90)t.fd(-20)t.seth(0)t.fd(-78)t.pd()t.begin_fill()t.seth(-130)t.circle(100,10)t.circle(300,30)t.seth(0)t.fd(230)t.seth(90)t.circle(300,30)t.circle(100,3)t.color((255,155,192),(255,100,100))t.seth(-135)t.circle(-80,63)t.circle(-150,24)t.end_fill()# 手t.color((255,155,192))t.pu()t.seth(90)t.fd(-40)t.seth(0)t.fd(-27)t.pd()t.seth(-160)t.circle(300,15)t.pu()t.seth(90)t.fd(15)t.seth(0)t.fd(0)t.pd()t.seth(-10)t.circle(-20,90)t.pu()t.seth(90)t.fd(30)t.seth(0)t.fd(237)t.pd()t.seth(-20)t.circle(-300,15)t.pu()t.seth(90)t.fd(20)t.seth(0)t.fd(0)t.pd()t.seth(-170)t.circle(20,90)# 腳t.pensize(10)t.color((240,128,128))t.pu()t.seth(90)t.fd(-75)t.seth(0)t.fd(-180)t.pd()t.seth(-90)t.fd(40)t.seth(-180)t.color("black")t.pensize(15)t.fd(20)t.pensize(10)t.color((240, 128, 128))t.pu()t.seth(90)t.fd(40)t.seth(0)t.fd(90)t.pd()t.seth(-90)t.fd(40)t.seth(-180)t.color("black")t.pensize(15)t.fd(20)# 尾巴t.pensize(4)t.color((255, 155, 192))t.pu()t.seth(90)t.fd(70)t.seth(0)t.fd(95)t.pd()t.seth(0)t.circle(70, 20)t.circle(10, 330)t.circle(70, 30)t.done()
海龜繪圖(Turtle Graphics)