小白學Python(20)—— Turtle 海龜繪圖


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

在1966年,Seymour Papert和Wally Feurzig發明了一種專門給兒童學習編程的語言——LOGO語言,它的特色就是通過編程指揮一個小海龜(turtle)在屏幕上繪圖。海龜繪圖(Turtle Graphics)后來被移植到各種高級語言中,Python內置了turtle庫,基本上100%復制了原始的Turtle Graphics的所有功能。

使用之前需要導入庫:

from turtle import *

 畫筆運動命令

命令

說明

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)

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

 

畫筆控制命令

命令

說明

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形狀

 

 

 

如,畫一個長方形:

# 導入turtle包的所有內容:
from turtle import *

# 設置筆刷寬度:
width(4)

# 前進:
forward(200)
# 右轉90度:
right(90)

# 筆刷顏色:
pencolor('red')
forward(100)
right(90)

pencolor('green')
forward(200)
right(90)

pencolor('blue')
forward(100)
right(90)

# 調用done()使得窗口等待被關閉,否則將立刻關閉窗口:
done()

  

 

更多操作請參考turtle庫的說明。https://docs.python.org/3.6/library/frameworks.html

 

五角星: 

from turtle import *
for i in range(5):
    fd(200)
    rt(144)
done()

  

填充顏色

 

from turtle import *
width(4)
pencolor("yellow")
fillcolor("red")

begin_fill()
for i in range(5):
    fd(200)
    rt(144)
end_fill()
done()

  

太陽花:

from turtle import *

color('red', 'yellow')

begin_fill()

while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break

end_fill()

done()

  

太極圖:

from turtle import *

circle(100,180)
circle(200,180)
circle(100,-180)

fillcolor('black')

begin_fill()

circle(100,180)
circle(200,180)
circle(100,-180)

end_fill()

penup()

goto(0,100)
dot(50)
goto(0,-100)
pencolor('white')
dot(50)

hideturtle()

done()

  

利用循環嵌套方法畫圖:

from turtle import *

for i in range(6):
    pendown()    
    fd(150)
    
    for j in range(10):
        circle(40)
        lt(36)
    lt(60)    
    penup()  
goto(0,0) done()

  

 

畫一個笑臉

from turtle import *
def go(x,y):
    penup()
    goto(x,y)
    pendown()
def arc(radius):
    circle(radius,90)
reset()
speed(0)
go(0,-150)
circle(200)
go(50,100)
seth(225)
arc(10)
arc(50)
arc(10)
arc(50)
go(-50,100)
seth(-45)
arc(-10)
arc(-50)
arc(-10)
arc(-50)
go(-70,-50)
arc(100)
hideturtle()

  

 

 繪制一棵分型樹:

from turtle import *

# 設置色彩模式是RGB:
colormode(255)

lt(90)
lv = 14
l = 120
s = 45

width(lv)

# 初始化RGB顏色:
r = 0
g = 0
b = 0
pencolor(r, g, b)

penup()
bk(l)
pendown()
fd(l)

def draw_tree(l, level):
    global r, g, b
    # save the current pen width
    w = width()

    # narrow the pen width
    width(w * 3.0 / 4.0)
    # set color:
    r = r + 1
    g = g + 2
    b = b + 3
    pencolor(r % 200, g % 200, b % 200)

    l = 3.0 / 4.0 * l

    lt(s)
    fd(l)

    if level < lv:
        draw_tree(l, level + 1)
    bk(l)
    rt(2 * s)
    fd(l)

    if level < lv:
        draw_tree(l, level + 1)
    bk(l)
    lt(s)

    # restore the previous pen width
    width(w)

speed("fastest")

draw_tree(l, 4)

done()

  

 

 螺旋圖:

from turtle import *
for i in range(100):
    fd(2*i)
    lt(90)
done()

  

 

  

from turtle import *

speed(10)

for i in range(200):
    fd(2*i)
    lt(92)
done()

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM