python turtle繪圖命令及案例
一、繪圖命令
操縱海龜繪圖有很多命令,可以劃分為三種:畫筆運(yùn)動(dòng)命令、畫筆控制命令、全局控制命令
1、畫筆運(yùn)動(dòng)命令
| 命令 | 說明 |
|---|---|
| turtle.forward(distance) | 向當(dāng)前畫筆方向移動(dòng)distance像素長(zhǎng)度 |
| turtle.backward(distance) | 向當(dāng)前畫筆相反方向移動(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) | 將畫筆移動(dòng)到坐標(biāo)為(x,y)的位置 |
| turtle.penuo() | 提起畫筆,不繪制圖形,用于另起一個(gè)地方繪制 |
| turtle.circle() | 畫圓,半徑為正(負(fù)),表示圓心在畫筆的左邊(右邊)畫圓 |
| setx() | 將當(dāng)前x軸移動(dòng)到指定位置 |
| sety() | 將當(dāng)前y軸移動(dòng)到指定位置 |
| setheading(angle) | 設(shè)置當(dāng)前朝向?yàn)閍ngle的角度 |
| home() | 設(shè)置當(dāng)前畫筆位置為原點(diǎn),朝向向東 ° |
2、畫筆控制命令
| 命令 | 說明 |
|---|---|
| turtle.fillcolor(colorstring) | 繪制圖形填充顏色 |
| turtle.color(color1, color2) | 同時(shí)設(shè)置 pencolor = color1,fillcolor = color2 |
| turtle.filling() | 返回當(dāng)前是否在填充狀態(tài) |
| turtle.begin_fill() | 準(zhǔn)備開始填充圖形 |
| turtle.end_fill() | 填充完成 |
| turtle.hideturtle() | 隱藏畫筆的turtle形狀 |
| turtle.showturtle() | 顯示畫筆的turtle形狀 |
3、全局控制命令
| 命令 | 說明 |
|---|---|
| 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是否可見 |
| stamp() | 復(fù)制當(dāng)前圖形 |
| turtle.write(s[,font = ("font_name",font_size,"font_type")]) | 寫文本,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、案例二
畫筆自動(dòng)繪圖
# 用for循環(huán)初步實(shí)現(xiàn)畫筆自動(dòng)繪圖
import turtle as t
for i in range(20):
# 畫筆向前移動(dòng)
t.forward(100 + 10 * i)
# 順時(shí)針旋轉(zhuǎn)120°
t.right(120)
t.done()

3、案例三
顯示畫筆運(yùn)動(dòng)印記
# 用for循環(huán)初步實(shí)現(xiàn)畫筆自動(dòng)繪圖并顯示其印記
import turtle as t
for i in range(20):
# 畫筆向前移動(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、案例四
畫筆及填充控制
# 繪制金光閃閃的太陽
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("一顆金光閃閃的太陽", align = "right", font = ("Arial", 20, "normal"))
t.done()

5、案例五
畫圓形類的圖
# 粉色的愛心
import turtle as t
t.setup(800,800)
t.speed(8)
# 設(shè)置畫筆大小
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)信息的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
淺談Python由__dict__和dir()引發(fā)的一些思考
這篇文章主要介紹了淺談Python由__dict__和dir()引發(fā)的一些思考,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10
python DES加密與解密及hex輸出和bs64格式輸出的實(shí)現(xiàn)代碼
這篇文章主要介紹了python DES加密與解密及hex輸出和bs64格式輸出的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python使用apscheduler模塊設(shè)置定時(shí)任務(wù)的實(shí)現(xiàn)
本文主要介紹了Python使用apscheduler模塊設(shè)置定時(shí)任務(wù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
selenium.webdriver中add_argument方法常用參數(shù)表
這篇文章主要介紹了selenium.webdriver中add_argument方法常用參數(shù)表,需要的朋友可以參考下2021-04-04

