當前位置: 妍妍網 > 碼農

Python最強【畫家】——turtle

2024-02-14碼農

大家好!今天給你們帶來了使用turtle庫畫的各種有趣的畫圖。

安裝turtle:

pip install PythonTurtle

1.金字塔

import turtledef draw_sierpinski(length, depth):if depth == 0:for_ in range(0, 3): turtle.forward(length) turtle.left(120)else: draw_sierpinski(length / 2, depth - 1) turtle.forward(length / 2) draw_sierpinski(length / 2, depth - 1) turtle.backward(length / 2) turtle.left(60) turtle.forward(length / 2) turtle.right(60) draw_sierpinski(length / 2, depth - 1) turtle.left(60) turtle.backward(length / 2) turtle.right(60)turtle.speed(0)draw_sierpinski(300, 4)turtle.done()

2.樹

import turtledef draw_branch(branch_length, t):if branch_length > 5: t.forward(branch_length) t.right(20) draw_branch(branch_length - 15, t) t.left(40) draw_branch(branch_length - 15, t) t.right(20) t.backward(branch_length)t = turtle.Turtle()my_win = turtle.Screen()t.left(90)t.up()t.backward(100)t.down()t.color("green")draw_branch(75, t)my_win.exitonclick()

3.小豬佩吉:

  • from turtle import*defnose(x,y):#鼻子 penup()#提起筆 goto(x,y)#定位 pendown()#落筆,開始畫 setheading(-30)#將烏龜的方向設定為to_angle/為數位(0-東、90-北、180-西、270-南) begin_fill()#準備開始填充圖形 a=0.4for i in range(120):if0<=i<30or60<=i<90: a=a+0.08 left(3) #向左轉3度 forward(a) #向前走a的步長else: a=a-0.08 left(3) forward(a) end_fill()#填充完成 penup() setheading(90) forward(25) setheading(0) forward(10) pendown() pencolor(255,155,192)#畫筆顏色 setheading(10) begin_fill() circle(5) color(160,82,45)#返回或設定pencolor和fillcolor end_fill() penup() setheading(0) forward(20) pendown() pencolor(255,155,192) setheading(10) begin_fill() circle(5) color(160,82,45) end_fill()defhead(x,y):#頭 color((255,155,192),"pink") penup() goto(x,y) setheading(0) pendown() begin_fill() setheading(180) circle(300,-30) circle(100,-60) circle(80,-100) circle(150,-20) circle(60,-95) setheading(161) circle(-300,15) penup() goto(-100,100) pendown() setheading(-30) a=0.4for i in range(60):if0<=i<30or60<=i<90: a=a+0.08 lt(3) #向左轉3度 fd(a) #向前走a的步長else: a=a-0.08 lt(3) fd(a) end_fill()defears(x,y):#耳朵 color((255,155,192),"pink") penup() goto(x,y) pendown() begin_fill() setheading(100) circle(-50,50) circle(-10,120) circle(-50,54) end_fill() penup() setheading(90) forward(-12) setheading(0) forward(30) pendown() begin_fill() setheading(100) circle(-50,50) circle(-10,120) circle(-50,56) end_fill()defeyes(x,y):#眼睛 color((255,155,192),"white") penup() setheading(90) forward(-20) setheading(0) forward(-95) pendown() begin_fill() circle(15) end_fill() color("black") penup() setheading(90) forward(12) setheading(0) forward(-3) pendown() begin_fill() circle(3) end_fill() color((255,155,192),"white") penup() seth(90) forward(-25) seth(0) forward(40) pendown() begin_fill() circle(15) end_fill() color("black") penup() setheading(90) forward(12) setheading(0) forward(-3) pendown() begin_fill() circle(3) end_fill()defcheek(x,y):#腮 color((255,155,192)) penup() goto(x,y) pendown() setheading(0) begin_fill() circle(30) end_fill()defmouth(x,y):#嘴 color(239,69,19) penup() goto(x,y) pendown() setheading(-80) circle(30,40) circle(40,80)defbody(x,y):#身體 color("red",(255,99,71)) penup() goto(x,y) pendown() begin_fill() setheading(-130) circle(100,10) circle(300,30) setheading(0) forward(230) setheading(90) circle(300,30) circle(100,3) color((255,155,192),(255,100,100)) setheading(-135) circle(-80,63) circle(-150,24) end_fill()defhands(x,y):#手 color((255,155,192)) penup() goto(x,y) pendown() setheading(-160) circle(300,15) penup() setheading(90) forward(15) setheading(0) forward(0) pendown() setheading(-10) circle(-20,90) penup() setheading(90) forward(30) setheading(0) forward(237) pendown() setheading(-20) circle(-300,15) penup() setheading(90) forward(20) setheading(0) forward(0) pendown() setheading(-170) circle(20,90)deffoot(x,y):#腳 pensize(10) color((240,128,128)) penup() goto(x,y) pendown() setheading(-90) forward(40) setheading(-180) color("black") pensize(15) fd(20) pensize(10) color((240,128,128)) penup() setheading(90) forward(40) setheading(0) forward(90) pendown() setheading(-90) forward(40) setheading(-180) color("black") pensize(15) fd(20)deftail(x,y):#尾巴 pensize(4) color((255,155,192)) penup() goto(x,y) pendown() seth(0) circle(70,20) circle(10,330) circle(70,30)defsetting():#參數設定 pensize(4) hideturtle() #使烏龜無形(隱藏) colormode(255) #將其設定為1.0或255.隨後 顏色三元組的r,g,b值必須在0 .. cmode範圍內 color((255,155,192),"pink") setup(840,500) speed(10)defmain(): setting() #畫布、畫筆設定 nose(-100,100) #鼻子 head(-69,167) #頭 ears(0,160) #耳朵 eyes(0,140) #眼睛 cheek(80,10) #腮 mouth(-20,30) #嘴 body(-32,-8) #身體 hands(-56,-45) #手 foot(2,-177) #腳 tail(148,-155) #尾巴 done()if __name__ == '__main__': main()

    4.鐘表

  • import turtlefrom datetime import *# 擡起畫筆,向前運動一段距離放下defSkip(step): turtle.penup() turtle.forward(step) turtle.pendown()defmkHand(name, length):# 註冊Turtle形狀,建立表針Turtle turtle.reset() Skip(-length * 0.1)# 開始記錄多邊形的頂點。當前的烏龜位置是多邊形的第一個頂點。 turtle.begin_poly() turtle.forward(length * 1.1)# 停止記錄多邊形的頂點。當前的烏龜位置是多邊形的最後一個頂點。將與第一個頂點相連。 turtle.end_poly()# 返回最後記錄的多邊形。 handForm = turtle.get_poly() turtle.register_shape(name, handForm)defInit():global secHand, minHand, hurHand, printer# 重設Turtle指向北 turtle.mode("logo")# 建立三個表針Turtle並初始化 mkHand("secHand", 135) mkHand("minHand", 125) mkHand("hurHand", 90) secHand = turtle.Turtle() secHand.shape("secHand") minHand = turtle.Turtle() minHand.shape("minHand") hurHand = turtle.Turtle() hurHand.shape("hurHand")for hand in secHand, minHand, hurHand: hand.shapesize(1, 1, 3) hand.speed(0)# 建立輸出文字Turtle printer = turtle.Turtle()# 隱藏畫筆的turtle形狀 printer.hideturtle() printer.penup()defSetupClock(radius):# 建立表的外框 turtle.reset() turtle.pensize(7)for i in range(60): Skip(radius)if i % 5 == 0: turtle.forward(20) Skip(-radius - 20) Skip(radius + 20)if i == 0: turtle.write(int(12), align="center", font=("Courier", 14, "bold"))elif i == 30: Skip(25) turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-25)elif (i == 25or i == 35): Skip(20) turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-20)else: turtle.write(int(i/5), align="center", font=("Courier", 14, "bold")) Skip(-radius - 20)else: turtle.dot(5) Skip(-radius) turtle.right(6)defWeek(t): week = ["星期一", "星期二", "星期三","星期四", "星期五", "星期六", "星期日"]return week[t.weekday()]defDate(t): y = t.year m = t.month d = t.dayreturn"%s %d%d" % (y, m, d)defTick():# 繪制表針的動態顯示 t = datetime.today() second = t.second + t.microsecond * 0.000001 minute = t.minute + second / 60.0 hour = t.hour + minute / 60.0 secHand.setheading(6 * second) minHand.setheading(6 * minute) hurHand.setheading(30 * hour) turtle.tracer(False) printer.forward(65) printer.write(Week(t), align="center", font=("Courier", 14, "bold")) printer.back(130) printer.write(Date(t), align="center", font=("Courier", 14, "bold")) printer.home() turtle.tracer(True)# 100ms後繼續呼叫tick turtle.ontimer(Tick, 100)defmain():# 開啟/關閉龜動畫,並為更新圖紙設定延遲。 turtle.tracer(False) Init() SetupClock(160) turtle.tracer(True) Tick() turtle.mainloop()if __name__ == "__main__": main()

    5.彩虹

    #彩虹繪制from turtle import *defHSB2RGB(hues): hues = hues * 3.59#100轉成359範圍 rgb=[0.0,0.0,0.0] i = int(hues/60)%6 f = hues/60 -iif i == 0: rgb[0] = 1; rgb[1] = f; rgb[2] = 0elif i == 1: rgb[0] = 1-f; rgb[1] = 1; rgb[2] = 0elif i == 2: rgb[0] = 0; rgb[1] = 1; rgb[2] = felif i == 3: rgb[0] = 0; rgb[1] = 1-f; rgb[2] = 1elif i == 4: rgb[0] = f; rgb[1] = 0; rgb[2] = 1elif i == 5: rgb[0] = 1; rgb[1] = 0; rgb[2] = 1-freturn rgbdefrainbow(): hues = 0.0 color(1,0,0)#繪制彩虹 hideturtle()#隱藏烏龜 speed(5) pensize(3) penup() goto(-650,-150) pendown() right(110)for i in range (100): circle(600)#圓的半徑600 right(0.23) hues = hues + 1 rgb = HSB2RGB(hues) color(rgb[0],rgb[1],rgb[2]) penup()defmain(): setup(1200, 800, 0, 0) bgcolor((64/255, 64/255, 1)) tracer(False) rainbow()#輸出文字 tracer(False) goto(0,0) pendown() color('yellow') write("彩虹",align="center", font=("Script MT Bold", 80, "bold")) tracer(True) mainloop()if __name__ == "__main__": main()

    6.蛇

    import turtledefdrawSnake(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) # 直線前進 turtle.circle(neckrad, 180) turtle.forward(rad/4)if __name__ == "__main__": turtle.setup(1500, 1400, 0, 0) turtle.pensize(30) # 畫筆尺寸 turtle.pencolor("green") turtle.seth(-40) # 前進的方向 drawSnake(70, 80, 2, 15)

    簡要:

    1)畫筆運動的命令turtle.forward(a) 向當前畫筆方向移動a像素長度turtle.backward(a) 向當前畫筆相反方向移動a像素長度turtle.right(a) 順時針移動aturtle.left(a) 逆時針移動aturtle.pendown() 移動時繪制圖形turtle.goto(x,y) 將畫筆移動到座標為x,y的位置turtle.penup() 移動時不繪制圖形,提起筆turtle.speed(a) 畫筆繪制的速度範圍turtle.circle() 畫圖,半徑為正,表示圓心在畫筆的左邊畫圈2)畫筆控制命令turtle.pensize(width) 繪制圖形的寬度turtle.pencolor() 畫筆的顏色turtle.fillcolor(a) 繪制圖形的填充顏色turtle.color(a1,a2) 同時設定pencolor=a1,fillcolor=a2turtle.filling() 返回當前是否在填充狀態turtle.begin_fill() 準備開始填充圖形turtle.end_fill() 填充完成turtle.hideturtle() 隱藏箭頭顯示turtle.showturtle() 顯示箭頭3)全域控制命令turtle.clear() 清空turtle視窗,但是turtle的位置和狀態不會改變turtle.reset() 清空視窗,重設turtle狀態為起始位置turtle.undo() 撤銷上一個turtle動作