python中對(duì)turtle庫的學(xué)習(xí)筆記

1.turtle庫的引用:????

?1)import turtle? ?引入繪圖庫? ??

? ? ? import? ?<庫名>

? ? ? <庫名>.?<函數(shù)名>

? ? ? 程序中每個(gè)函數(shù)都需要加turtle庫名

2)from和import 共同完成

? ? ? from?<庫名> import*

? ? ? ?<函數(shù)名>

? ? ? ? 程序中每個(gè)函數(shù)不需要加庫名,但是如果有多個(gè)庫,庫中存在相同函數(shù),或者自變量于函數(shù)名重合,會(huì)造成 引用錯(cuò)誤。

3)import 和 as共同完成

? ? ? import? turtle as? t

? ? ? ?t.?<函數(shù)名>

? ? ?給調(diào)用外部庫關(guān)聯(lián)一個(gè)更簡(jiǎn)單的名字t,調(diào)用?t.?<函數(shù)名>,即簡(jiǎn)便又不會(huì)重名。

2.turtle畫布控制函數(shù)

1)turtle繪圖窗體

? ? ? ?turtle.setup(width,height,startx,starty)? ?

? ? ? ? setup()設(shè)置窗體的大小和窗體的位置;4個(gè)參數(shù)中后兩個(gè)可選;setup()不是必選,

? ? ? ? 輸入寬和高為整數(shù)時(shí), 表示像素; 為小數(shù)時(shí), 表示占據(jù)電腦屏幕的比例,為空表示窗口位于屏幕中心。


2)turtle空間坐標(biāo)體系和角度坐標(biāo)體系

? ? ? ? ?turtle .gotu(100,100)? ? ?直接轉(zhuǎn)到坐標(biāo)為(100,100)的位置。

turtle.setx( ):將當(dāng)前x軸移動(dòng)到指定位置

turtle.sety( ):將當(dāng)前y軸移動(dòng)到指定位置

3.turtle畫筆控制函數(shù)

? ? ? turtle.penup()別名turtle.pu():拾起畫筆,海龜在飛行

? ? ? turtle.pendown()別名turtle.pd():放下畫筆,海龜在爬行

? ? ? turtle.color(color1, color2):同時(shí)設(shè)置pencolor=color1, fillcolor=color2。

? ? ? turtle.pencolor(color)? 有三種方式:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? turtle.pencolor(‘purple’)?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?turtle.pencolor(255,255,255)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?turtle.pencolor(1,1,1)?

? ? ? ?turtle.fillcolor(colorstring):繪制圖形的填充顏色

? ? ? ?turtle.home():設(shè)置當(dāng)前畫筆位置為原點(diǎn),朝向東。

? ? ? ?turtle.dot(r):繪制一個(gè)指定直徑和顏色的圓點(diǎn)。

? ? ? ?turtle.begin_fill():準(zhǔn)備開始填充圖形。

? ? ? ?turtle.end_fill():填充完成。

? ? ? ?turtle.hideturtle():隱藏畫筆的turtle形狀。

? ? ? ?turtle.showturtle():顯示畫筆的turtle形狀

3.turtle運(yùn)動(dòng)控制函數(shù)

? ? ? 1)走直線

? ? ? ? ? turtle.forward(distance)(別名:turtle.fd):向當(dāng)前畫筆方向移動(dòng)distance像素長(zhǎng)度。

? ? ? ? ? turtle.backward(distance):向當(dāng)前畫筆相反方向移動(dòng)distance像素長(zhǎng)度。注意轉(zhuǎn)向不變

? ? ? 2)走曲線

? ? ? ? ?turtle.right(angle):順時(shí)針移動(dòng)angle

? ? ? ? ? turtle.left(angle):逆時(shí)針移動(dòng)angle。

? ? ? ? ?turtle.circle(radius, extent=None, steps=None)

? ? ? ? 描述:以給定半徑畫圓

? ? ? ? 參數(shù):

? ? ? ? radius(半徑):半徑為正(負(fù)),表示圓心在畫筆的左邊(右邊)畫圓;

? ? ? ? extent(弧度) (optional);

? ? ? ? steps (optional) (做半徑為radius的圓的內(nèi)切正多邊形,多邊形邊數(shù)為steps)。

? ? ? ?舉例:

? ? ? ? circle(50) # 整圓;

? ? ? ? circle(50,steps=3) # 三角形;

? ? ? ? circle(120, 180) # 半圓

4.turtle方向控制函數(shù)、

? ? ? ? 1)絕對(duì)角度

? ? ? ? ? ? ? ? ? ? turtle.seth(angle) :只改變海龜方向但不行進(jìn),angle為絕對(duì)角度可正可負(fù)。

? ? ? ? 2)海龜角度

? ? ? ? ? ? ? ? ? ? ?turtle.right(anger):順時(shí)針移動(dòng)anger。

? ? ? ? ? ? ? ? ? ? ?turtle.left(anger):逆時(shí)針移動(dòng)anger。

5.基本循環(huán)語句

? ? ? ?for <變量>in range(<參數(shù)>)

? ? ? ? <被循環(huán)執(zhí)行的語句>? ? ?

? ? ? ?<變量>表示每次循環(huán)的計(jì)數(shù),0到次數(shù)-1

? ? ? ? ? ? ? range 函數(shù)產(chǎn)生循環(huán)計(jì)數(shù)序列

? ? ? ? ? ? ? range(N)產(chǎn)生0到N-1的整數(shù)序列,共N個(gè)

? ? ? ? ? ? ? range(M,N)產(chǎn)生M到N-1的整數(shù)序列,共N-M個(gè)


6.舉例分析

1)太陽花


代碼:

from turtle import *

begin_fill()? #準(zhǔn)備開始填充圖形

pensize(2) #設(shè)置畫筆的寬度

color('red','yellow') #設(shè)置畫筆顏色為藍(lán)色,填充顏色為綠色

while True:

? ? forward(200) #畫筆移動(dòng)200個(gè)像素長(zhǎng)度

? ? left(170)? #逆時(shí)針移動(dòng)170°

? ? if abs(pos())<1: #判斷畫筆是否回到起點(diǎn)

? ? ? ? break

end_fill()? #結(jié)束填充圖形

done()


2)玫瑰花


代碼:

from turtle import *

#global pen and speed

pencolor("black")

fillcolor("red")

speed(50)

s=0.15

#init poistion

penup()

goto(0,600*s)

pendown()

begin_fill()

circle(200*s,30)

for i in range(60):

? ? lt(1)

? ? circle(50*s,1)

circle(200*s,30)

for i in range(4):

? ? lt(1)

? ? circle(100*s,1)

circle(200*s,50)

for i in range(50):

? ? lt(1)

? ? circle(50*s,1)

circle(350*s,65)

for i in range(40):

? ? lt(1)

? ? circle(70*s,1)

circle(150*s,50)

for i in range(20):

? ? rt(1)

? ? circle(50*s,1)

circle(400*s,60)

for i in range(18):

? ? lt(1)

? ? circle(50*s,1)

fd(250*s)

rt(150)

circle(-500*s,12)

lt(140)

circle(550*s,110)

lt(27)

circle(650*s,100)

lt(130)

circle(-300*s,20)

rt(123)

circle(220*s,57)

end_fill()

lt(120)

fd(280*s)

lt(115)

circle(300*s,33)

lt(180)

circle(-300*s,33)

for i in range(70):

? ? rt(1)

? ? circle(225*s,1)

circle(350*s,104)

lt(90)

circle(200*s,105)

circle(-500*s,63)

penup()

goto(170*s,-330*s)

pendown()

lt(160)

for i in range(20):

? ? lt(1)

? ? circle(2500*s,1)

for i in range(220):

? ? rt(1)

? ? circle(250*s,1)

fillcolor('green')

penup()

goto(670*s,-480*s)

pendown()

rt(140)

begin_fill()

circle(300*s,120)

lt(60)

circle(300*s,120)

end_fill()

penup()

goto(180*s,-850*s)

pendown()

rt(85)

circle(600*s,40)

penup()

goto(-150*s,-1300*s)

pendown()

begin_fill()

rt(120)

circle(300*s,115)

lt(75)

circle(300*s,100)

end_fill()

penup()

goto(430*s,-1370*s)

pendown()

rt(30)

circle(-600*s,35)

done()


3)玫瑰花

代碼:

import turtle

turtle.screensize(600,500,'white')

turtle.pensize(3)? ? ? ? ? #設(shè)置畫筆寬度為3

turtle.pencolor('blue')? ? #設(shè)置畫筆顏色為黑色

turtle.fillcolor('yellow')? #設(shè)置填充顏色為黃色

turtle.begin_fill()? ? ? ? #開始填充

turtle.forward(-300)

for i in range(3,8):

? ? turtle.circle(20, steps=i)

? ? turtle.forward(100)

turtle.circle(20)

turtle.end_fill()

turtle.hideturtle()? ? ? ? #隱藏海龜

turtle.done()





? ??

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容