Turtle庫(kù)是Python語(yǔ)言中一個(gè)很流行的繪制圖像的函數(shù)庫(kù),想象一個(gè)小烏龜,在一個(gè)橫軸為x、縱軸為y的坐標(biāo)系原點(diǎn),(0,0)位置開(kāi)始,它根據(jù)一組函數(shù)指令的控制,在這個(gè)平面坐標(biāo)系中移動(dòng),從而在它爬行的路徑上繪制了圖形。
創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括漢源網(wǎng)站建設(shè)、漢源網(wǎng)站制作、漢源網(wǎng)頁(yè)制作以及漢源網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃等。多年來(lái),我們專(zhuān)注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,漢源網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶(hù)以成都為中心已經(jīng)輻射到漢源省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶(hù)的支持與信任!
Python2安裝命令:
pip install turtule
Python3安裝命令:
pip3 install turtle
因?yàn)閠urtle庫(kù)主要是在Python2中使用的,所以安裝的時(shí)候可能會(huì)提示錯(cuò)誤:
Command "python setup.py egg_info" failed with error code 1
解決方法請(qǐng)參考這里碼客社區(qū)的《Python3安裝turtle提示錯(cuò)誤:Command "python setup.py egg_info" failed with error code 1》。
畫(huà)布就是turtle為我們展開(kāi)用于繪圖區(qū)域, 我們可以設(shè)置它的大小和初始位置。
常用的畫(huà)布方法有兩個(gè):screensize()
和setup()
。
(1)turtle.screensize(canvwidth=None, canvheight=None, bg=None)
參數(shù)分別為畫(huà)布的寬(單位像素), 高, 背景顏色
如:
turtle.screensize(800, 600, "green")
turtle.screensize() #返回默認(rèn)大小(400, 300)
(2)turtle.setup(width=0.5, height=0.75, startx=None, starty=None)
參數(shù):
width, height
:輸入寬和高為整數(shù)時(shí), 表示像素; 為小數(shù)時(shí), 表示占據(jù)電腦屏幕的比例(startx, starty)
:這一坐標(biāo)表示 矩形窗口左上角頂點(diǎn)的位置, 如果為空,則窗口位于屏幕中心turtle.setup(width=0.6, height=0.6)
turtle.setup(width=800, height=800, startx=100, starty=100)
在畫(huà)布上,默認(rèn)有一個(gè)坐標(biāo)原點(diǎn)為畫(huà)布中心的坐標(biāo)軸, 坐標(biāo)原點(diǎn)上有一只面朝x軸正方向小烏龜。
這里我們描述小烏龜時(shí)使用了兩個(gè)詞語(yǔ):標(biāo)原點(diǎn)(位置),面朝x軸正方向(方向),turtle繪圖中, 就是使用位置方向描述小烏龜(畫(huà)筆)的狀態(tài)
(1)畫(huà)筆的屬性
畫(huà)筆有顏色、畫(huà)線(xiàn)的寬度等屬性。
1) turtle.pensize()
:設(shè)置畫(huà)筆的寬度;
2) turtle.pencolor()
:沒(méi)有參數(shù)傳入返回當(dāng)前畫(huà)筆顏色;傳入?yún)?shù)設(shè)置畫(huà)筆顏色,可以是字符串如"green", "red",也可以是RGB 3元組。
>>> pencolor('brown')
>>> tup = (0.2, 0.8, 0.55)
>>> pencolor(tup)
>>> pencolor()
'#33cc8c'
3) turtle.speed(speed)
:設(shè)置畫(huà)筆移動(dòng)速度,畫(huà)筆繪制的速度范圍[0,10]整數(shù), 數(shù)字越大越快
(2)繪圖命令
操縱海龜繪圖有著許多的命令,這些命令可以劃分為3種:運(yùn)動(dòng)命令,畫(huà)筆控制命令和全局控制命令
畫(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í)也為繪制
turtle.goto(x,y) 將畫(huà)筆移動(dòng)到坐標(biāo)為x,y的位置
turtle.penup() 移動(dòng)時(shí)不繪制圖形,提起筆,用于另起一個(gè)地方繪制時(shí)用
turtle.speed(speed) 畫(huà)筆繪制的速度范圍[0,10]整數(shù)
turtle.circle() 畫(huà)圓,半徑為正(負(fù)),表示圓心在畫(huà)筆的左邊(右邊)畫(huà)圓
畫(huà)筆控制命令:
命令 說(shuō)明
turtle.pensize(width) 繪制圖形時(shí)的寬度
turtle.pencolor() 畫(huà)筆顏色
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() 隱藏箭頭顯示;
turtle.showturtle() 與hideturtle()函數(shù)對(duì)應(yīng)
全局控制命令
命令 說(shuō)明
turtle.clear() 清空turtle窗口,但是turtle的位置和狀態(tài)不會(huì)改變
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為可選項(xiàng), font的參數(shù)也是可選項(xiàng)
import turtle as t
import time
t.color("red", "yellow")
t.speed(10)
t.begin_fill()
for _ in range(50):
t.forward(200)
t.left(170)
end_fill()
time.sleep(1)
import turtle
def drawSnake(rad, angle, len, neckrad):
for _ in range(len):
turtle.circle(rad, angle)
turtle.circle(-rad, angle)
turtle.circle(rad, angle/2)
turtle.forward(rad/2) # 直線(xiàn)前進(jìn)
turtle.circle(neckrad, 180)
turtle.forward(rad/4)
if __name__ == "__main__":
turtle.setup(1500, 1400, 0, 0)
turtle.pensize(30) # 畫(huà)筆尺寸
turtle.pencolor("green")
turtle.seth(-40) # 前進(jìn)的方向
drawSnake(70, 80, 2, 15)
import turtle
import time
turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("red")
turtle.begin_fill()
for _ in range(5):
turtle.forward(200)
turtle.right(144)
turtle.end_fill()
time.sleep(2)
turtle.penup()
turtle.goto(-150,-120)
turtle.color("violet")
turtle.write("Done", font=('Arial', 40, 'normal'))
time.sleep(1)
import turtle
def draw_triangle(points, color, t):
t.fillcolor(color)
t.up()
t.goto(points[0][0], points[0][1])
t.down()
t.begin_fill()
t.goto(points[1][0], points[1][1])
t.goto(points[2][0], points[2][1])
t.goto(points[0][0], points[0][1])
t.end_fill()
def get_mid(point1, point2):
return (point1[0] + point2[0]) / 2, (point1[1] + point2[1]) / 2
def sierpinski(points, degree, t):
color_map = ['blue', 'red', 'green', 'yellow', 'violet', 'orange', 'white',]
draw_triangle(points, color_map[degree], t)
if degree > 0:
sierpinski([points[0], get_mid(points[0], points[1]), get_mid(points[0], points[2])], degree - 1, t)
sierpinski([points[1], get_mid(points[0], points[1]), get_mid(points[1], points[2])], degree - 1, t)
sierpinski([points[2], get_mid(points[0], points[2]), get_mid(points[1], points[2])], degree - 1, t)
if __name__ == "__main__"
t = turtle.Turtle()
t.speed(5)
win = turtle.Screen()
points = [[-100, -50], [0, 100], [100, -50]]
sierpinski(points, 3, t)
win.exitonclick()
網(wǎng)站題目:Python3turtle安裝和使用教程
瀏覽地址:http://www.rwnh.cn/article28/jipscp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、Google、靜態(tài)網(wǎng)站、建站公司、用戶(hù)體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)