python使用Turtle庫畫畫寫名字
Turtle
庫是Python
語言中一個(gè)很流行的繪制圖像的函數(shù)庫,利用這個(gè)庫會(huì)生成一個(gè)畫布,在畫布中有我們看不見的一個(gè)默認(rèn)以中心點(diǎn)為原點(diǎn)的坐標(biāo)軸,在原點(diǎn)會(huì)有一個(gè)初始默認(rèn)朝東的畫筆(箭頭),一般我們把這個(gè)箭頭叫做海龜,箭頭的朝向就是海龜頭的朝向。
這個(gè)動(dòng)圖展示的就是我利用Turtle庫畫的一個(gè)名字畫,下面附上代碼:
import time import turtle from random import random t = turtle t.speed(3) t.hideturtle() t.color('black') t.pensize(10) # 一 t.penup() t.goto(-110, 60) t.pendown() t.forward(100) # | t.penup() t.goto(-60, 125) t.right(90) t.pendown() t.forward(266) # 丿 t.penup() t.goto(-60, 45) t.right(45) t.pendown() t.forward(90) # 丶 t.penup() t.goto(-60, 38) t.left(90) t.pendown() t.forward(40) # 小一 t.penup() t.goto(20, 60) t.left(45) t.pendown() t.forward(90) # | t.penup() t.goto(65, 123) t.right(90) t.pendown() t.forward(123) # 大一 t.penup() t.goto(0, -5) t.left(90) t.pendown() t.forward(130) # 小一 t.penup() t.goto(20, -65) t.pendown() t.forward(90) # | t.penup() t.goto(65, -14) t.right(90) t.pendown() t.forward(120) # 大一 t.penup() t.goto(0, -135) t.left(90) t.pendown() t.forward(130) # 畫圓 t.color('red') t.pensize(3) t.penup() t.goto(15, -200) t.pendown() t.circle(180) t.exitonclick()
函數(shù) |
解釋 |
hideturtle |
隱藏畫筆 |
speed |
畫筆速度,1-10,1最慢,10最快 |
color |
后可傳參(2個(gè)參數(shù)),不傳為默認(rèn)設(shè)置,第一個(gè)參數(shù)為畫筆顏色,第二個(gè)參數(shù)為填充顏色 |
pensize |
畫筆粗細(xì),越大越粗 |
penup |
畫筆拿起,之后畫筆運(yùn)動(dòng)時(shí)不畫下軌跡 |
pendown |
默認(rèn)是這個(gè)狀態(tài),畫筆落下,之后畫筆運(yùn)動(dòng)會(huì)畫出軌跡 |
forward(num) |
畫筆運(yùn)動(dòng),num為運(yùn)動(dòng)像素?cái)?shù),即長(zhǎng)度 |
right (a) |
畫筆方向向右旋轉(zhuǎn)a角度,順時(shí)針旋轉(zhuǎn) |
left(a) |
畫筆方向向左旋轉(zhuǎn)a角度,逆時(shí)針旋轉(zhuǎn) |
goto(x,y) |
將畫筆移動(dòng)到坐標(biāo)軸x,y位置,在pendowm下會(huì)畫下軌跡 |
goto(x,y) |
將畫筆移動(dòng)到坐標(biāo)軸x,y位置,在pendowm下會(huì)畫下軌跡 |
circle(a) |
以當(dāng)前點(diǎn)為圓心,畫一個(gè)半徑為a的圓 |
exitonclick |
點(diǎn)擊關(guān)閉,畫筆運(yùn)行完不再自動(dòng)退出畫布 |
下面是在論壇看到的一個(gè)大佬的畫櫻花樹的代碼,生成的結(jié)果比上面的復(fù)雜很多,但在弄懂我上面代碼的情況下很容易看懂,作為進(jìn)階給大家展示下:
import turtle as T import random import time # 畫櫻花的軀干(60,t) def Tree(branch, t): time.sleep(0.0005) if branch > 3: if 8 <= branch <= 12: if random.randint(0, 2) == 0: t.color('snow') # 白 else: t.color('lightcoral') # 淡珊瑚色 t.pensize(branch / 3) elif branch < 8: if random.randint(0, 1) == 0: t.color('snow') else: t.color('lightcoral') # 淡珊瑚色 t.pensize(branch / 2) else: t.color('sienna') # 赭(zhě)色 t.pensize(branch / 10) # 6 t.forward(branch) a = 1.5 * random.random() t.right(20 * a) b = 1.5 * random.random() Tree(branch - 10 * b, t) t.left(40 * a) Tree(branch - 10 * b, t) t.right(20 * a) t.up() t.backward(branch) t.down() # 掉落的花瓣 def Petal(m, t): for i in range(m): a = 200 - 400 * random.random() b = 10 - 20 * random.random() t.up() t.forward(b) t.left(90) t.forward(a) t.down() t.color('lightcoral') # 淡珊瑚色 t.circle(1) t.up() t.backward(a) t.right(90) t.backward(b) # 繪圖區(qū)域 t = T.Turtle() # 畫布大小 w = T.Screen() t.hideturtle() # 隱藏畫筆 t.getscreen().tracer(5, 0) w.screensize(bg='wheat') # wheat小麥 t.left(90) t.up() t.backward(150) t.down() t.color('sienna') # 畫櫻花的軀干 Tree(60, t) # 掉落的花瓣 Petal(200, t) w.exitonclick()
這個(gè)代碼除了用到我上面介紹的幾個(gè)函數(shù),還用到了screen
和bgcolor
,分別是設(shè)置畫布和背景色,backward
就是反向的forward
。
到此這篇關(guān)于python
利用Turtle
庫畫畫寫名字 的文章就介紹到這了,更多相關(guān)python
用Turtle
畫畫寫名字 內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。