Turtle庫(kù)是Python語(yǔ)言中一個(gè)很流行的繪制圖像的函數(shù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于Python中turtle庫(kù)常用代碼的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
一、設(shè)置畫(huà)布
turtle為我們展開(kāi)用于繪圖區(qū)域,我們可以設(shè)置它的大小和初始位置文章源自四五設(shè)計(jì)網(wǎng)-http://m.133122.cn/45498.html
turtle.screensize(canvwidth=600,canvheight=800,bg='black')文章源自四五設(shè)計(jì)網(wǎng)-http://m.133122.cn/45498.html
#參數(shù)分別代表畫(huà)布的寬、高、背景色文章源自四五設(shè)計(jì)網(wǎng)-http://m.133122.cn/45498.html
turtle.screensize()#返回默認(rèn)大小(400,300)文章源自四五設(shè)計(jì)網(wǎng)-http://m.133122.cn/45498.html
turtle.setup(width=0.6,height=0.6,startx=100,starty=100)文章源自四五設(shè)計(jì)網(wǎng)-http://m.133122.cn/45498.html
#輸入寬和高為整數(shù)時(shí), 表示像素; 為小數(shù)時(shí), 表示占據(jù)電腦屏幕的比例文章源自四五設(shè)計(jì)網(wǎng)-http://m.133122.cn/45498.html
#(startx, starty): 這一坐標(biāo)表示矩形窗口左上角頂點(diǎn)的位置, 如果為空,則窗口位于屏幕中心文章源自四五設(shè)計(jì)網(wǎng)-http://m.133122.cn/45498.html
二、畫(huà)筆
1、畫(huà)筆屬性
turtle.pensize(8)??? #畫(huà)筆粗細(xì)文章源自四五設(shè)計(jì)網(wǎng)-http://m.133122.cn/45498.html
turtle.color('red')?? #畫(huà)筆顏色?? 字符串"green", "red" 或者 RGB 3元組。文章源自四五設(shè)計(jì)網(wǎng)-http://m.133122.cn/45498.html
turtle.speed(0)??? #畫(huà)筆移動(dòng)速度?? 畫(huà)筆繪制的速度范圍[0,10]整數(shù),數(shù)字越大越快文章源自四五設(shè)計(jì)網(wǎng)-http://m.133122.cn/45498.html
2、繪圖命令
(1)??? 畫(huà)筆運(yùn)動(dòng)命令
turtle.forward(8)? ? ? #向前移動(dòng)
turtle.backward(8)? #向后移動(dòng)
turtle.right(90)? #海龜方向向右轉(zhuǎn)90°
turtle.left(90) #海龜方向向左轉(zhuǎn)90°
turtle.penup()??????? #提筆
turtle.pendown()?? #落筆
turtle.goto(x,y)????? 海龜移動(dòng)到(x,y)位置
turtle.setx(x)???????? 海龜?shù)膞坐標(biāo)移動(dòng)到指定位置
turtle.sety(y)???????? 海龜?shù)膟坐標(biāo)移動(dòng)到指定位置
turtle.circle()???????? 畫(huà)圓
turtle.dot()??????????? 畫(huà)一個(gè)圓點(diǎn)(實(shí)心)
turtle.setheading(angle)???? #設(shè)置當(dāng)前朝向?yàn)閍ngle角度
turtle.home()???????????????? 設(shè)置當(dāng)前畫(huà)筆位置為原點(diǎn),朝向東(默認(rèn)值)
(2)???? 畫(huà)筆控制命令
turtle.fillcolor('red')????? ? ?????? 設(shè)置 填充顏色
turtle.color(color1, color2)?? 設(shè)置 畫(huà)筆顏色為color1,填充顏色為color2
turtle.begin_fill()?????????????????? 開(kāi)始填充顏色
turtle.end_fill()????????????????????? 填充完成
turtle.hideturtle()????????????????? 隱藏海龜圖標(biāo)
turtle.showturtle()??????????????? 顯示海龜圖標(biāo)
(3)??? 全局控制命令
turtle.clear()?????????????????? 清空turtle窗口,但是turtle的位置和狀態(tài)不會(huì)改變
turtle.reset()?????????????????? 清空turtle窗口,重置turtle狀態(tài)為起始狀態(tài)
turtle.undo()?????????????????? 撤銷(xiāo)上一個(gè)turtle動(dòng)作
turtle.isvisible()?????????????? 返回當(dāng)前turtle是否可見(jiàn)
t.write("文本" ,align="center",font=("微軟雅黑",20,"normal"))??????????? 寫(xiě)文本
align(可選):left,right,center;font(可選):字體名稱(chēng),字體大小,字體類(lèi)型(normal,bold,italic)
(4)??? 其他命令

3. 命令詳解
1 |
參數(shù):
radius(半徑):半徑為正(負(fù)),表示圓心在畫(huà)筆的左邊(右邊)畫(huà)圓;
extent(弧度) ;
steps :(做半徑為radius的圓的內(nèi)切正多邊形,多邊形邊數(shù)為steps)。
1 2 3 4 5 6 7 8 | import turtle as tt.circle(50)#整圓t.circle(50,steps=3)#內(nèi)置的三角形?t.penup()t.goto(100,0)t.pendown()t.circle(50,180)#半圓 |

三、文字顯示為一個(gè)圓圈
1 2 3 4 5 6 7 8 9 10 | import turtle as ttext="棉花娃娃很可愛(ài)"t.penup()x=len(text)for i in text:????t.write(i,font='consolas')????t.right(360/x)????t.penup()??? ????t.forward(30)t.hideturtle() |

四、畫(huà)朵小花
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import turtle as tt.speed(0)?#花柄t.penup()t.goto(0,-150)t.pendown()t.pensize(2)t.setheading(90)t.color('brown')t.fd(300)?#花瓣t.pensize(1)t.color('black','red')t.begin_fill()for i in range(10):????t.left(45)????t.circle(80,60)????t.left(120)????t.circle(80,60)t.end_fill()?#葉子for i in range(2):????t.penup()????t.goto(0,10-50*i)????x=20+80*i????t.setheading(x)????t.pendown()????t.color('brown','green')????t.begin_fill()????t.circle(60,60)????t.left(120)????t.circle(60,60)????t.end_fill()t.hideturtle() |

總結(jié)
到此這篇關(guān)于Python中turtle庫(kù)常用代碼的文章就介紹到這了



評(píng)論