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() | 撤銷(xiāo)上一個(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ù),分別是字體名稱(chēng),字體大小和類(lèi)型,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à)圓形類(lèi)的圖
# 粉色的愛(à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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python爬取智聯(lián)招聘數(shù)據(jù)分析師崗位相關(guān)信息的方法
這篇文章主要介紹了Python爬取智聯(lián)招聘數(shù)據(jù)分析師崗位相關(guān)信息的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08淺談Python由__dict__和dir()引發(fā)的一些思考
這篇文章主要介紹了淺談Python由__dict__和dir()引發(fā)的一些思考,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10python DES加密與解密及hex輸出和bs64格式輸出的實(shí)現(xiàn)代碼
這篇文章主要介紹了python DES加密與解密及hex輸出和bs64格式輸出的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Python使用apscheduler模塊設(shè)置定時(shí)任務(wù)的實(shí)現(xiàn)
本文主要介紹了Python使用apscheduler模塊設(shè)置定時(shí)任務(wù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05selenium.webdriver中add_argument方法常用參數(shù)表
這篇文章主要介紹了selenium.webdriver中add_argument方法常用參數(shù)表,需要的朋友可以參考下2021-04-04