python turtle繪圖命令及案例
一、繪圖命令
操縱海龜繪圖有很多命令,可以劃分為三種:畫(huà)筆運(yùn)動(dòng)命令、畫(huà)筆控制命令、全局控制命令
1、畫(huà)筆運(yùn)動(dòng)命令
命令 | 說(shuō)明 |
---|---|
turtle.forward(distance) | 向當(dāng)前畫(huà)筆方向移動(dòng)distance像素長(zhǎng)度 |
turtle.backward(distance) | 向當(dāng)前畫(huà)筆相反方向移動(dòng)distance像素長(zhǎng)度 |
turtle.right(degree) | 順時(shí)針移動(dòng)degree° |
turtle.left(degree) | 逆時(shí)針移動(dòng)degree° |
turtle.pendown() | 移動(dòng)時(shí)繪制圖形,缺少參數(shù)時(shí)也為繪制 |
turtle.goto(x,y) | 將畫(huà)筆移動(dòng)到坐標(biāo)為(x,y)的位置 |
turtle.penuo() | 提起畫(huà)筆,不繪制圖形,用于另起一個(gè)地方繪制 |
turtle.circle() | 畫(huà)圓,半徑為正(負(fù)),表示圓心在畫(huà)筆的左邊(右邊)畫(huà)圓 |
setx() | 將當(dāng)前x軸移動(dòng)到指定位置 |
sety() | 將當(dāng)前y軸移動(dòng)到指定位置 |
setheading(angle) | 設(shè)置當(dāng)前朝向?yàn)閍ngle的角度 |
home() | 設(shè)置當(dāng)前畫(huà)筆位置為原點(diǎn),朝向向東 ° |
2、畫(huà)筆控制命令
命令 | 說(shuō)明 |
---|---|
turtle.fillcolor(colorstring) | 繪制圖形填充顏色 |
turtle.color(color1, color2) | 同時(shí)設(shè)置 pencolor = color1,fillcolor = color2 |
turtle.filling() | 返回當(dāng)前是否在填充狀態(tài) |
turtle.begin_fill() | 準(zhǔn)備開(kāi)始填充圖形 |
turtle.end_fill() | 填充完成 |
turtle.hideturtle() | 隱藏畫(huà)筆的turtle形狀 |
turtle.showturtle() | 顯示畫(huà)筆的turtle形狀 |
3、全局控制命令
命令 | 說(shuō)明 |
---|---|
turtle.clear() | 清空turtle窗口,但是turtle的位置和狀態(tài)不會(huì)發(fā)生變化 |
turtle.reset() | 清空窗口,重置turtle狀態(tài)為起始狀態(tài) |
turtle.undo() | 撤銷上一個(gè)turtle動(dòng)作 |
turtle.isvisible() | 返回當(dāng)前turtle是否可見(jiàn) |
stamp() | 復(fù)制當(dāng)前圖形 |
turtle.write(s[,font = ("font_name",font_size,"font_type")]) | 寫(xiě)文本,s為文本內(nèi)容,font是字體參數(shù),分別是字體名稱,字體大小和類型,font和font的參數(shù)都是可選選項(xiàng) |
二、案例
1、案例一
熟悉turtle坐標(biāo)體系
# 導(dǎo)入 turtle 模塊 import turtle as t t.goto(100,100) t.goto(100,-100) t.goto(-100,-100) t.goto(-100,100) t.goto(0,0) t.done()
2、案例二
畫(huà)筆自動(dòng)繪圖
# 用for循環(huán)初步實(shí)現(xiàn)畫(huà)筆自動(dòng)繪圖 import turtle as t for i in range(20): # 畫(huà)筆向前移動(dòng) t.forward(100 + 10 * i) # 順時(shí)針旋轉(zhuǎn)120° t.right(120) t.done()
3、案例三
顯示畫(huà)筆運(yùn)動(dòng)印記
# 用for循環(huán)初步實(shí)現(xiàn)畫(huà)筆自動(dòng)繪圖并顯示其印記 import turtle as t for i in range(20): # 畫(huà)筆向前移動(dòng) t.forward(100 + 10 * i) #t.shape("turtle") # 海龜 #t.shape("circle") # 圓 t.shape("square") # 正方形 # 打印turtle印記 t.stamp() # 順時(shí)針旋轉(zhuǎn)60° t.right(60) t.done()
4、案例四
畫(huà)筆及填充控制
# 繪制金光閃閃的太陽(yáng) import turtle as t # 為小數(shù)時(shí)表示占據(jù)電腦屏幕的比例 t.setup(width = 0.6, height = 0.6) # t.pencolor("red") t.color("red", "yellow") t.begin_fill() # 控制繪圖時(shí)間 t.speed(20) while True: t.forward(200) t.left(170) # print(t.pos()) if abs(t.pos()) < 1: break t.end_fill() t.write("一顆金光閃閃的太陽(yáng)", align = "right", font = ("Arial", 20, "normal")) t.done()
5、案例五
畫(huà)圓形類的圖
# 粉色的愛(ài)心 import turtle as t t.setup(800,800) t.speed(8) # 設(shè)置畫(huà)筆大小 t.pensize(10) t.hideturtle() t.pencolor("pink") t.left(45) t.forward(80) t.circle(35,210) t.right(150) t.circle(35,210) t.forward(80) t.done()
到此這篇關(guān)于 python turtle
繪圖命令及案例的文章就介紹到這了,更多相關(guān) python turtle繪圖內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。