1.簡介
Python的turtle庫的易操作,對初學者十分友好。對於初學者來說,剛學編程沒多久可以寫出許多有趣的可視化東西,這是對學習編程極大的鼓舞,可以樹立對編程學習的信心。當然turtle本身也十分有趣,可以用它畫出很多奇妙的圖案。
2.繪圖的基本知識
(1)畫布(canvas)
畫布就是turtle為我們展開用於繪圖區域,我們可以設置它的大小和初始位置。
設置畫布大小
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)畫筆
i)畫筆的狀態
在畫布上,默認有一個坐標原點為畫布中心的坐標軸,坐標原點上有一只面朝x軸正方向小烏龜。這里我們描述小烏龜時使用了兩個詞語:坐標原點(位置),面朝x軸正方向(方向),turtle繪圖中,就是使用位置方向描述小烏龜(畫筆)的狀態。
ii)畫筆的屬性
畫筆(畫筆的屬性,顏色、畫線的寬度等)
turtle.pensize(): 設置畫筆的寬度;
turtle.pencolor(): 沒有參數傳入,返回當前畫筆顏色,傳入參數設置畫筆顏色,可以是字符串如"green","red",也可以是RGB3元組。
turtle.speed(speed): 設置畫筆移動速度,畫筆繪制的速度范圍[0,10]整數,數字越大越快。
(3)繪圖窗口的原點(0,0)在正中間。默認情況下,海龜向正右方移動。
(4)操縱海龜繪圖有着許多的命令,這些命令可以划分為兩種: 一種為運動命令,一種為畫筆控制命令
i)運動命令:
forward(d) 向前移動距離d代表距離
backward(d) 向后移動距離d代表距離
right(degree) 向右轉動多少度
left(degree) 向左轉動多少度
goto(x,y) 將畫筆移動到坐標為(x,y)的位置
stamp() 繪制當前圖形
speed(speed) 畫筆繪制的速度范圍[0,10]整數
ii)畫筆控制命令:
down() 畫筆落下,移動時繪制圖形
up() 畫筆抬起,移動時不繪制圖形
setheading(degree) 海龜(turtle)朝向,degree代表角度
reset() 恢復所有設置
pensize(width) 畫筆的寬度
pencolor(colorstring) 畫筆的顏色
fillcolor(colorstring) 繪制圖形的填充顏色
circle(radius,extent) 繪制一個圓形,其中radius為半徑,extent為度數,例如若extent為120,則畫一個三分之一圓;
3.turtle繪圖案例
(1)方形螺旋圖像:
from turtle import * for i in range(500): forward(i) left(91)
運行效果:
(2)彩色螺旋圖
from turtle import * colors = ['red', 'purple', 'blue', 'green', 'yellow', 'orange'] for x in range(360): pencolor(colors[x % 6]) width(x / 100 + 1) forward(x) left(59)
運行效果:
(3)小豬佩奇
# coding:utf-8 from turtle import* def nose(x,y):#鼻子 pu() goto(x,y) pd() seth(-30) begin_fill() a=0.4 for i in range(120): if 0<=i<30 or 60<=i<90: a=a+0.08 lt(3) #向左轉3度 fd(a) #向前走a的步長 else: a=a-0.08 lt(3) fd(a) end_fill() pu() seth(90) fd(25) seth(0) fd(10) pd() pencolor(255,155,192) seth(10) begin_fill() circle(5) color(160,82,45) end_fill() pu() seth(0) fd(20) pd() pencolor(255,155,192) seth(10) begin_fill() circle(5) color(160,82,45) end_fill() def head(x,y):#頭 color((255,155,192),"pink") pu() goto(x,y) seth(0) pd() begin_fill() seth(180) circle(300,-30) circle(100,-60) circle(80,-100) circle(150,-20) circle(60,-95) seth(161) circle(-300,15) pu() goto(-100,100) pd() seth(-30) a=0.4 for i in range(60): if 0<=i<30 or 60<=i<90: a=a+0.08 lt(3) #向左轉3度 fd(a) #向前走a的步長 else: a=a-0.08 lt(3) fd(a) end_fill() def ears(x,y): #耳朵 color((255,155,192),"pink") pu() goto(x,y) pd() begin_fill() seth(100) circle(-50,50) circle(-10,120) circle(-50,54) end_fill() pu() seth(90) fd(-12) seth(0) fd(30) pd() begin_fill() seth(100) circle(-50,50) circle(-10,120) circle(-50,56) end_fill() def eyes(x,y):#眼睛 color((255,155,192),"white") pu() seth(90) fd(-20) seth(0) fd(-95) pd() begin_fill() circle(15) end_fill() color("black") pu() seth(90) fd(12) seth(0) fd(-3) pd() begin_fill() circle(3) end_fill() color((255,155,192),"white") pu() seth(90) fd(-25) seth(0) fd(40) pd() begin_fill() circle(15) end_fill() color("black") pu() seth(90) fd(12) seth(0) fd(-3) pd() begin_fill() circle(3) end_fill() def cheek(x,y):#腮 color((255,155,192)) pu() goto(x,y) pd() seth(0) begin_fill() circle(30) end_fill() def mouth(x,y): #嘴 color(239,69,19) pu() goto(x,y) pd() seth(-80) circle(30,40) circle(40,80) def body(x,y):#身體 color("red",(255,99,71)) pu() goto(x,y) pd() begin_fill() seth(-130) circle(100,10) circle(300,30) seth(0) fd(230) seth(90) circle(300,30) circle(100,3) color((255,155,192),(255,100,100)) seth(-135) circle(-80,63) circle(-150,24) end_fill() def hands(x,y):#手 color((255,155,192)) pu() goto(x,y) pd() seth(-160) circle(300,15) pu() seth(90) fd(15) seth(0) fd(0) pd() seth(-10) circle(-20,90) pu() seth(90) fd(30) seth(0) fd(237) pd() seth(-20) circle(-300,15) pu() seth(90) fd(20) seth(0) fd(0) pd() seth(-170) circle(20,90) def foot(x,y):#腳 pensize(10) color((240,128,128)) pu() goto(x,y) pd() seth(-90) fd(40) seth(-180) color("black") pensize(15) fd(20) pensize(10) color((240,128,128)) pu() seth(90) fd(40) seth(0) fd(90) pd() seth(-90) fd(40) seth(-180) color("black") pensize(15) fd(20) def tail(x,y):#尾巴 pensize(4) color((255,155,192)) pu() goto(x,y) pd() seth(0) circle(70,20) circle(10,330) circle(70,30) def setting(): #參數設置 pensize(4) hideturtle() colormode(255) color((255,155,192),"pink") setup(840,500) speed(10) def main(): setting() #畫布、畫筆設置 nose(-100,100) #鼻子 head(-69,167) #頭 ears(0,160) #耳朵 eyes(0,140) #眼睛 cheek(80,10) #腮 mouth(-20,30) #嘴 body(-32,-8) #身體 hands(-56,-45) #手 foot(2,-177) #腳 tail(148,-155) #尾巴 done() #結束 main() main()
運行效果: