前言
- 最近翻到一篇知乎,上面有不少用Python(大多是turtle庫)繪制的樹圖,感覺很漂亮,整理了一下,挑了一些覺得不錯的代碼分享給大家(這些我都測試過,確實可以生成喔~趕快去試一下吧)
one 櫻花樹
動態生成櫻花
- 效果圖(這個是動態的):
- 實現代碼:
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) # 繪圖區域 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()
飄落效果
- 效果圖:
- 實現代碼:
from turtle import * from random import * from math import * def tree(n,l): pd()#下筆 #陰影效果 t = cos(radians(heading()+45))/8+0.25 pencolor(t,t,t) pensize(n/3) forward(l)#畫樹枝 if n>0: b = random()*15+10 #右分支偏轉角度 c = random()*15+10 #左分支偏轉角度 d = l*(random()*0.25+0.7) #下一個分支的長度 #右轉一定角度,畫右分支 right(b) tree(n-1,d) #左轉一定角度,畫左分支 left(b+c) tree(n-1,d) #轉回來 right(c) else: #畫葉子 right(90) n=cos(radians(heading()-45))/4+0.5 pencolor(n,n*0.8,n*0.8) circle(3) left(90) #添加0.3倍的飄落葉子 if(random()>0.7): pu() #飄落 t = heading() an = -40 +random()*40 setheading(an) dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2) forward(dis) setheading(t) #畫葉子 pd() right(90) n = cos(radians(heading()-45))/4+0.5 pencolor(n*0.5+0.5,0.4+n*0.4,0.4+n*0.4) circle(2) left(90) pu() #返回 t=heading() setheading(an) backward(dis) setheading(t) pu() backward(l)#退回 bgcolor(0.5,0.5,0.5)#背景色 ht()#隱藏turtle speed(0)#速度 1-10漸進,0 最快 tracer(0,0) pu()#抬筆 backward(100) left(90)#左轉90度 pu()#抬筆 backward(300)#后退300 tree(12,100)#遞歸7層 done()
暗色效果
- 效果圖:
- 實現代碼:
from turtle import * from random import * from math import * def tree(n, l): pd() t = cos(radians(heading() + 45)) / 8 + 0.25 pencolor(t, t, t) pensize(n / 4) forward(l) if n > 0: b = random() * 15 + 10 c = random() * 15 + 10 d = l * (random() * 0.35 + 0.6) right(b) tree(n - 1, d) left(b + c) tree(n - 1, d) right(c) else: right(90) n = cos(radians(heading() - 45)) / 4 + 0.5 pencolor(n, n, n) circle(2) left(90) pu() backward(l) bgcolor(0.5, 0.5, 0.5) ht() speed(0) tracer(0, 0) left(90) pu() backward(300) tree(13, 100) done()
two 玫瑰花
- 效果圖(有繪制過程)
- 實現代碼:
from turtle import * import time setup(1000,800,0,0) speed(0) penup() seth(90) fd(340) seth(0) pendown() speed(5) begin_fill() fillcolor('red') circle(50,30) for i in range(10): fd(1) left(10) circle(40,40) for i in range(6): fd(1) left(3) circle(80,40) for i in range(20): fd(0.5) left(5) circle(80,45) for i in range(10): fd(2) left(1) circle(80,25) for i in range(20): fd(1) left(4) circle(50,50) time.sleep(0.1) circle(120,55) speed(0) seth(-90) fd(70) right(150) fd(20) left(140) circle(140,90) left(30) circle(160,100) left(130) fd(25) penup() right(150) circle(40,80) pendown() left(115) fd(60) penup() left(180) fd(60) pendown() end_fill() right(120) circle(-50,50) circle(-20,90) speed(1) fd(75) speed(0) circle(90,110) penup() left(162) fd(185) left(170) pendown() circle(200,10) circle(100,40) circle(-52,115) left(20) circle(100,20) circle(300,20) speed(1) fd(250) penup() speed(0) left(180) fd(250) circle(-300,7) right(80) circle(200,5) pendown() left(60) begin_fill() fillcolor('green') circle(-80,100) right(90) fd(10) left(20) circle(-63,127) end_fill() penup() left(50) fd(20) left(180) pendown() circle(200,25) penup() right(150) fd(180) right(40) pendown() begin_fill() fillcolor('green') circle(-100,80) right(150) fd(10) left(60) circle(-80,98) end_fill() penup() left(60) fd(13) left(180) pendown() speed(1) circle(-200,23) exitonclick()
three 聖誕樹
- 聖誕樹 (動態生成效果圖)
- 實現代碼:
from turtle import * import random import time n = 100.0 speed("fastest") screensize(bg='seashell') left(90) forward(3*n) color("orange", "yellow") begin_fill() left(126) for i in range(5): forward(n/5) right(144) forward(n/5) left(72) end_fill() right(126) color("dark green") backward(n*4.8) def tree(d, s): if d <= 0: return forward(s) tree(d-1, s*.8) right(120) tree(d-3, s*.5) right(120) tree(d-3, s*.5) right(120) backward(s) tree(15, n) backward(n/2) for i in range(200): a = 200 - 400 * random.random() b = 10 - 20 * random.random() up() forward(b) left(90) forward(a) down() if random.randint(0, 1) == 0: color('tomato') else: color('wheat') circle(2) up() backward(a) right(90) backward(b) time.sleep(60)
turtle繪制動漫形象
- 成品圖:
- 繪制過程:
- 實現代碼:
import turtle as te import time WriteStep = 15 # 貝塞爾函數的取樣次數 Speed = 5 Width = 600 # 界面寬度 Height = 500 # 界面高度 Xh = 0 # 記錄前一個貝塞爾函數的手柄 Yh = 0 def Bezier(p1, p2, t): # 一階貝塞爾函數 return p1 * (1 - t) + p2 * t def Bezier_2(x1, y1, x2, y2, x3, y3): # 二階貝塞爾函數 te.goto(x1, y1) te.pendown() for t in range(0, WriteStep + 1): x = Bezier(Bezier(x1, x2, t / WriteStep), Bezier(x2, x3, t / WriteStep), t / WriteStep) y = Bezier(Bezier(y1, y2, t / WriteStep), Bezier(y2, y3, t / WriteStep), t / WriteStep) te.goto(x, y) te.penup() def Bezier_3(x1, y1, x2, y2, x3, y3, x4, y4): # 三階貝塞爾函數 x1 = -Width / 2 + x1 y1 = Height / 2 - y1 x2 = -Width / 2 + x2 y2 = Height / 2 - y2 x3 = -Width / 2 + x3 y3 = Height / 2 - y3 x4 = -Width / 2 + x4 y4 = Height / 2 - y4 # 坐標變換 te.goto(x1, y1) te.pendown() for t in range(0, WriteStep + 1): x = Bezier(Bezier(Bezier(x1, x2, t / WriteStep), Bezier(x2, x3, t / WriteStep), t / WriteStep), Bezier(Bezier(x2, x3, t / WriteStep), Bezier(x3, x4, t / WriteStep), t / WriteStep), t / WriteStep) y = Bezier(Bezier(Bezier(y1, y2, t / WriteStep), Bezier(y2, y3, t / WriteStep), t / WriteStep), Bezier(Bezier(y2, y3, t / WriteStep), Bezier(y3, y4, t / WriteStep), t / WriteStep), t / WriteStep) te.goto(x, y) te.penup() def Moveto(x, y): # 移動到svg坐標下(x,y) te.penup() te.goto(-Width / 2 + x, Height / 2 - y) def line(x1, y1, x2, y2): # 連接svg坐標下兩點 te.penup() te.goto(-Width / 2 + x1, Height / 2 - y1) te.pendown() te.goto(-Width / 2 + x2, Height / 2 - y2) te.penup() def lineto(dx, dy): # 連接當前點和相對坐標(dx,dy)的點 te.pendown() te.goto(te.xcor() + dx, te.ycor() - dy) te.penup() def Lineto(x, y): # 連接當前點和svg坐標下(x,y) te.pendown() te.goto(-Width / 2 + x, Height / 2 - y) te.penup() def Horizontal(x): # 做到svg坐標下橫坐標為x的水平線 te.pendown() te.setx(x - Width / 2) te.penup() def horizontal(dx): # 做到相對橫坐標為dx的水平線 te.seth(0) te.pendown() te.fd(dx) te.penup() def vertical(dy): # 做到相對縱坐標為dy的垂直線 te.seth(-90) te.pendown() te.fd(dy) te.penup() te.seth(0) def polyline(x1, y1, x2, y2, x3, y3): # 做svg坐標下的折線 te.penup() te.goto(-Width / 2 + x1, Height / 2 - y1) te.pendown() te.goto(-Width / 2 + x2, Height / 2 - y2) te.goto(-Width / 2 + x3, Height / 2 - y3) te.penup() def Curveto(x1, y1, x2, y2, x, y): # 三階貝塞爾曲線到(x,y) te.penup() X_now = te.xcor() + Width / 2 Y_now = Height / 2 - te.ycor() Bezier_3(X_now, Y_now, x1, y1, x2, y2, x, y) global Xh global Yh Xh = x - x2 Yh = y - y2 def curveto_r(x1, y1, x2, y2, x, y): # 三階貝塞爾曲線到相對坐標(x,y) te.penup() X_now = te.xcor() + Width / 2 Y_now = Height / 2 - te.ycor() Bezier_3(X_now, Y_now, X_now + x1, Y_now + y1, X_now + x2, Y_now + y2, X_now + x, Y_now + y) global Xh global Yh Xh = x - x2 Yh = y - y2 def Smooth(x2, y2, x, y): # 平滑三階貝塞爾曲線到(x,y) global Xh global Yh te.penup() X_now = te.xcor() + Width / 2 Y_now = Height / 2 - te.ycor() Bezier_3(X_now, Y_now, X_now + Xh, Y_now + Yh, x2, y2, x, y) Xh = x - x2 Yh = y - y2 def smooth_r(x2, y2, x, y): # 平滑三階貝塞爾曲線到相對坐標(x,y) global Xh global Yh te.penup() X_now = te.xcor() + Width / 2 Y_now = Height / 2 - te.ycor() Bezier_3(X_now, Y_now, X_now + Xh, Y_now + Yh, X_now + x2, Y_now + y2, X_now + x, Y_now + y) Xh = x - x2 Yh = y - y2 te.tracer(10) te.setup(Width, Height, 0, 0) te.pensize(1) te.speed(Speed) te.penup() # 圖層_2 time.sleep(20) te.color("black", "#F2F2F2") # 外套 Moveto(61, 462) te.begin_fill() smooth_r(12, -41, 27, -58) curveto_r(-6, -36, 6, -118, 9, -132) curveto_r(-15, -27, -23, -51, -26, -74) curveto_r(4, -66, 38, -105, 65, -149) Horizontal(486) curveto_r(12, 24, 40, 99, 33, 114) curveto_r(39, 82, 55, 129, 39, 144) smooth_r(-31, 23, -39, 28) smooth_r(-12, 37, -12, 37) lineto(50, 92) Horizontal(445) smooth_r(-29, -38, -31, -46) smooth_r(78, -107, 72, -119) Smooth(355, 178, 340, 176) Smooth(272, 63, 264, 64) smooth_r(-29, 67, -27, 73) Curveto(99, 292, 174, 428, 173, 439) smooth_r(-8, 23, -8, 23) Lineto(61, 462) te.end_fill() Moveto(60.5, 461.5) # 陰影 te.color("black", "#D3DFF0") te.begin_fill() curveto_r(0, 0, 17, -42, 27, -59) curveto_r(-6, -33, 6, -128, 10, -133) curveto_r(-15, -10, -27, -66, -27.285, -75) te.pencolor("#D3DFF0") curveto_r(12.285, 11, 82.963, 156, 82.963, 156) te.pencolor("black") smooth_r(12.322, 75, 19.322, 86) curveto_r(-1, 11, -8, 25, -8, 25) Horizontal(60.5) te.end_fill() Moveto(444.5, 464) te.begin_fill() curveto_r(0, 0, -29, -36, -31, -46) smooth_r(53.59, -82.337, 53.59, -82.337) te.pencolor("#D3DFF0") smooth_r(86.41, -47.663, 96.072, -54.85) Curveto(563.5, 297.5, 570.5, 299.5, 518.5, 334) te.pencolor("black") curveto_r(-2, 16, -12, 33, -12, 37) smooth_r(50, 92, 50, 93) Horizontal(444.5) te.end_fill() Moveto(195, 49) te.begin_fill() te.pencolor("#D3DFF0") polyline(195, 49, 175.5, 106.5, 202.522, 49) te.pencolor("black") Horizontal(195) te.pencolor("#D3DFF0") te.end_fill() Moveto(327.997, 49) te.begin_fill() te.pencolor("#D3DFF0") curveto_r(0, 0, 11.503, 121.087, 13.503, 128.087) curveto_r(11, 2, 54, 37, 54, 37) lineto(-40, -165.087) te.pencolor("black") Horizontal(327.997) te.pencolor("#D3DFF0") te.end_fill() te.pencolor("black") line(94.5, 397.5, 107.5, 373.5) # 皺紋 line(122.5, 317.5, 95.875, 274.699) line(122.5, 341.5, 141.5, 402.5) line(141.5, 409.5, 153.5, 431.5) # line(328,47.712,344,175.977) line(340.023, 49, 360.5, 144) # line(353.5,47.5,395.5,208.5) line(478.5, 95.5, 518.5, 161.5) line(518.5, 332.5, 460.5, 359.5) polyline(506.5, 369.5, 493.5, 402.5, 502.5, 443.5) Moveto(530, 429) curveto_r(4, 16, -5, 33, -5, 33) # 圖層_3 te.color("black", "#2b1d2a") # 外套內側 Moveto(225, 462) te.begin_fill() Horizontal(165) smooth_r(9, -15, 8, -25) curveto_r(-47, -126, 6, -212, 12, -225) Curveto(185, 305, 202, 428, 225, 462) Lineto(225, 462) te.end_fill() Moveto(390, 462) te.begin_fill() curveto_r(10, -23, 34, -180, 35, -222) # !!!227 curveto_r(7, 4, 54, 45, 61, 61) # 61 smooth_r(-73, 101, -72, 118) curveto_r(5, 15, 31, 46, 31, 45) Lineto(390, 462) te.end_fill() # 圖層_4 te.color("black", "#2b1d29") # 外套內側 Moveto(225, 462) te.begin_fill() curveto_r(-28, -50, -40, -166, -40, -250) curveto_r(6, 51, -6, 87, 45, 106) smooth_r(64, 27, 89, 24) smooth_r(49, -18, 56, -20) smooth_r(50, -10, 51, -85) curveto_r(0, 29, -25, 201, -36, 225) Lineto(225, 462) te.end_fill() # 圖層_5 te.color("black", "#3D3D3D") # 衣服 Moveto(225, 462) te.begin_fill() curveto_r(-5, -5, -22, -53, -23, -70) lineto(32, -13) curveto_r(3, -25, 6, -28, 12, -36) smooth_r(13, -12, 16, -12) vertical(-2) curveto_r(45, 20, 64, 14, 94, 1) vertical(2) curveto_r(8, -2, 15, 2, 17, 4) smooth_r(0, 6, -2, 9) curveto_r(10, 10, 10, 29, 11, 33) smooth_r(23, 4, 25, 6) smooth_r(-17, 83, -17, 78) Lineto(225, 462) te.end_fill() # 圖層_6 te.color("black", "#968281") # 脖子 Moveto(262, 329) te.begin_fill() vertical(17) curveto_r(1, 2, 44, 14, 45, 15) smooth_r(3, 12, 3, 12) horizontal(3) vertical(-5) curveto_r(1, -3, 4, -6, 5, -7) lineto(36, -14) curveto_r(1, -1, 3, -16, 2, -17) Curveto(318, 348, 296, 344, 262, 329) te.end_fill() # 圖層_8 te.color("black", "#E7F1FF") # 白色褶皺 Moveto(225, 462) te.begin_fill() lineto(-3, -5) # -3,-3,-3,-5 curveto_r(0, -2, 4, -4, 5, -6) smooth_r(16, 3, 19, -8) smooth_r(0, -7, 0, -11) smooth_r(5, -8, 9, -5) smooth_r(19, -8, 19, -11) smooth_r(6, -7, 6, -7) smooth_r(7, -2, 9, -4) lineto(41, -2) lineto(12, 9) smooth_r(3, 15, 7, 18) smooth_r(15, 4, 17, 4) smooth_r(4, -4, 6, -4) smooth_r(6, 4, 5, 9) smooth_r(0, 9, 0, 9) smooth_r(1, 7, 7, 6) smooth_r(8, 0, 8, 0) lineto(-2, 8) Lineto(225, 462) te.end_fill() te.pensize(2) Moveto(240, 450) smooth_r(0, 9, 3, 12) Moveto(372, 462) curveto_r(-2, -4, -5, -29, -7, -28) te.pensize(1) # 圖層_7 te.color("black", "#A2B8D6") # 衣領 Moveto(262, 331) te.begin_fill() curveto_r(0, 8, -1, 13, 0, 15) smooth_r(43, 14, 45, 15) lineto(3, 12) horizontal(3) smooth_r(-1, -3, 0, -5) lineto(5, -7) lineto(36, -14) curveto_r(1, -1, 2, -12, 2, -15) smooth_r(25, -2, 15, 13) curveto_r(-2, 4, -7, 29, -7, 32) smooth_r(-35, 19, -41, 22) smooth_r(-9, 14, -12, 14) smooth_r(-7, -12, -14, -15) curveto_r(-19, -2, -41, -25, -41, -25) smooth_r(-10, -26, -10, -30) Smooth(255, 332, 262, 331) te.end_fill() Moveto(262, 346) lineto(-12, -6) Moveto(369, 333) curveto_r(2, 4, -6, 10, -15, 14) # 圖層_9 te.color("black", "#151515") # 領結 Moveto(247, 358) te.begin_fill() curveto_r(-5, 3, -8, 20, -6, 23) curveto_r(25, 21, 50, 17, 50, 17) lineto(-23, 64) horizontal(22) smooth_r(1, -13, 2, -16) lineto(13, -50) curveto_r(2, 2, 7, 3, 10, 1) smooth_r(18, 65, 18, 65) horizontal(19) lineto(-24, -65) curveto_r(21, 5, 39, -10, 44, -13) curveto_r(5, -20, 1, -21, 0, -24) curveto_r(-18, -2, -49, 15, -52, 17) smooth_r(-11, -3, -15, -1) Smooth(252, 356, 247, 358) te.end_fill() # 圖層_10 te.color("black", "#A2B8D6") # 衣領(透過領結) Moveto(297, 387) te.begin_fill() lineto(-11, 6) curveto_r(-1, 0, -20, -7, -30, -19) Curveto(259, 373, 297, 385, 297, 387) te.end_fill() Moveto(323, 384) te.begin_fill() lineto(8, 7) lineto(30, -14) curveto_r(1, -1, 5, -6, 4, -7) Smooth(329, 379, 323, 384) te.end_fill() # 圖層_11 te.color("black", "#F3EEEB") # 臉 Moveto(185, 212) te.begin_fill() curveto_r(4, -9, 46, -77, 52, -75) curveto_r(-2, -17, 19, -68, 27, -73) curveto_r(16, 15, 71, 108, 76, 112) smooth_r(76, 53, 86, 60) curveto_r(0, 65, -27, 75, -31, 76) curveto_r(-50, 28, -70, 30, -85, 30) smooth_r(-77, -22, -86, -26) Curveto(180, 302, 186, 228, 185, 212) te.end_fill() # 圖層_12 te.color("black", "#2B1D29") # 頭發 Moveto(189, 202) te.begin_fill() curveto_r(-1, 22, 19, 51, 19, 51) smooth_r(-10, -42, 7, -92) Curveto(212, 168, 196, 189, 189, 202) te.end_fill() Moveto(221, 155) te.begin_fill() curveto_r(-2, 6, 5, 48, 5, 48) smooth_r(18, -28, 20, -48) curveto_r(-5, 24, 4, 43, 7, 50) curveto_r(-10, -49, 3, -72, 13, -106) curveto_r(-2, -7, -3, -32, -3, -35) curveto_r(-17, 18, -27, 71, -27, 71) Lineto(221, 155) te.end_fill() Moveto(264, 64) te.begin_fill() curveto_r(-4, 5, 14, 100, 14, 100) smooth_r(-6, -79, -5, -85) curveto_r(0, 98, 49, 139, 49, 139) smooth_r(8, -50, 3, -65) Smooth(272, 64, 264, 64) te.end_fill() Moveto(342, 176) te.begin_fill() curveto_r(-1, 27, -10, 57, -10, 57) smooth_r(20, -33, 17, -54) Lineto(342, 176) te.end_fill() te.penup() te.begin_fill() polyline(349, 180, 353, 203, 361, 203) polyline(361, 203, 362, 188, 349, 180) te.end_fill() # 圖層_13 te.pensize(2) Moveto(210, 180) # 眉毛 curveto_r(5, -4, 63, 9, 63, 14) Moveto(338, 193) curveto_r(0, -3, 18, -6, 18, -6) te.pensize(1) # 圖層_14 te.color("black", "#D1D1D1") # 眼睛1 te.pensize(2) Moveto(206, 212) te.begin_fill() lineto(15, -7) curveto_r(4, -1, 26, -2, 30, 0) smooth_r(10, 3, 12, 7) te.pencolor("#D1D1D1") te.pensize(1) smooth_r(2, 27, -1, 30) smooth_r(-39, 5, -44, 1) Smooth(206, 212, 206, 212) te.end_fill() Moveto(384, 204) te.begin_fill() te.pencolor("black") te.pensize(2) curveto_r(-3, -1, -18, -1, -28, 1) smooth_r(-9, 6, -10, 9) te.pencolor("#D1D1D1") te.pensize(1) smooth_r(3, 18, 6, 23) smooth_r(38, 6, 40, 4) smooth_r(10, -9, 13, -22) te.pencolor("black") te.pensize(2) Lineto(384, 204) te.end_fill() # 圖層_15 te.color("#0C1631", "#0C1631") # 眼睛2 te.pensize(1) Moveto(216, 206) te.begin_fill() curveto_r(-1, 5, 0, 26, 7, 35) smooth_r(30, 2, 33, 0) smooth_r(5, -31, 2, -34) Smooth(219, 203, 216, 206) te.end_fill() Moveto(354, 207) te.begin_fill() curveto_r(-2, 1, 2, 29, 4, 31) smooth_r(30, 3, 33, 1) smooth_r(6, -24, 4, -27) lineto(-11, -8) Curveto(382, 204, 357, 206, 354, 207) te.end_fill() # 圖層_17 te.color("#F5F5F5", "#F5F5F5") # 眼睛3 Moveto(253, 211) te.begin_fill() curveto_r(-3, 0, -8, 8, 1, 10) Smooth(258, 210, 253, 211) te.end_fill() Moveto(392, 209) te.begin_fill() lineto(4, 3) vertical(4) lineto(-4, 2) Curveto(386, 214, 392, 209, 392, 209) te.end_fill() # 圖層_18 te.color("#352F53", "#352F53") # 眼睛4 Moveto(219, 229) te.begin_fill() smooth_r(2, -5, 6, -4) smooth_r(18, 13, 27, 1) curveto_r(3, 0, 5, 3, 5, 3) vertical(13) Horizontal(224) Lineto(219, 229) te.end_fill() Moveto(357, 227) te.begin_fill() smooth_r(4, -6, 10, -2) smooth_r(10, 13, 19, 1) curveto_r(6, 0, 8, 6, 8, 6) lineto(-2, 9) curveto_r(-12, 3, -29, 0, -32, -2) Smooth(357, 227, 357, 227) te.end_fill() # 圖層_19 te.color("#9A90CB", "#9A90CB") # 眼睛5 Moveto(227, 231) te.begin_fill() curveto_r(-6, 0, -5, 5, -3, 8) smooth_r(24, 2, 27, 0) smooth_r(0, -8, -1, -8) Smooth(234, 231, 227, 231) te.end_fill() Moveto(361, 227) te.begin_fill() curveto_r(2, 18, 26, 14, 30, 6) smooth_r(-1, -3, -2, -4) smooth_r(-15, 9, -24, -4) Curveto(363, 224, 361, 225, 361, 227) te.end_fill() # 圖層_16 te.pencolor("black") # 眼睛(線條) te.pensize(3) # Moveto(206,213) # lineto(14,-8) # curveto_r(3,-1,30,0,33,1) # lineto(10,6) Moveto(225, 215) curveto_r(10, 28, 22, 16, 24, 6) Moveto(365, 219) curveto_r(4, 14, 18, 24, 22, -3) te.pensize(2) line(240.5, 207.5, 227.5, 211.5) line(245.5, 209.5, 227.5, 214.5) line(247.5, 211.5, 227.5, 217.5) line(247.5, 214.5, 229.5, 220.5) line(247.5, 218.5, 230.5, 223.5) line(246.5, 222.5, 232.5, 226.5) line(244.5, 225.5, 234.5, 228.5) line(377.5, 207.5, 367.5, 210.5) line(384.5, 207.5, 366.5, 212.5) line(385.5, 210.5, 366.5, 215.5) line(384.5, 213.5, 366.5, 218.5) line(384.5, 215.5, 367.5, 220.5) line(384.5, 218.5, 368.5, 223.5) # line(383.5,220.5,368.5,225.5) line(382.5, 223.5, 370.5, 227.5) # line(381.5,226.5,373.5,229.5) # 圖層_20 te.pencolor("black") Moveto(309, 270) # 鼻子、嘴 curveto_r(0, 0, 4, 7, 1, 9) line(296.5, 307.5, 303.5, 307.5) Moveto(315, 307) smooth_r(10, -1, 10, 2) te.penup() te.hideturtle() te.update() te.done()
蟒蛇繪制及turtle庫的使用
環境要求
- python3
- 最好有一個ide,我用的是最基本的pycharm
- 這是我根據mooc的教程寫下來的(視頻鏈接在文末),希望能給剛開始學習Python提供一個范例:
- 這篇博文,我們將繪制一條蟒蛇,並解釋turtle的使用。
- 首先我們設計一下蟒蛇的基本形狀,它是一條爬行的蟒蛇而且很有回頭感,並且后面會看到它是可以動的:
- 這就要要用計算機圖形的繪制了,這與一般的簡單的測試程序是不一樣的,因為要涉及到窗口等的出現:
- 全部的代碼:
# python.py import turtle turtle.setup(650,350,200,200) turtle.penup() turtle.fd(-250) turtle.pendown() turtle.pensize(25) turtle.pencolor("purple") turtle.seth(-40) for i in range(4): turtle.circle(40,80) turtle.circle(-40,80) turtle.circle(40,80/2) turtle.fd(40) turtle.circle(16,180) turtle.fd(40 * 2/3) turtle.done() #finish
- 確保代碼無誤,執行后,就可以看到屏幕中出現窗口,並且出現了一個動態的,正在逐漸繪制的蟒蛇:
直到全部完成
- 從代碼中我們可以很明顯的看到使用了turtle這個庫,那么每一行的代表什么意思呢?
- turtle意思是海龜,是一個繪圖庫,是python的標准庫之一(注意標准庫與第三方庫的區別),我們可以叫它海龜庫,這是這個程序最關鍵的地方。
- turtle(海龜)是一種真實的存在,可以理解為:
- 有一只海龜,其實在窗體正中心,在畫布上游走;
- 走過的軌跡形成了繪制的圖形
- 海龜由程序控制,可以變換顏色,改變寬度等
再來看一下一些有趣的示例(使用turtle)
窗體布局
- 在電腦顯示中,左上角的像素坐標是(0,0),這里的turtle統一如此,可以使用setup()函數來社會中窗體大小以及位置
turtle.setup(width,height,startx,starty)
- (后兩個坐標是可選的,指窗體左上角相對於屏幕左上角的坐標)
- 在繪制過程中,setup函數並不是必須的,只有當你需要控制窗體的大小和位置時,采用setup進行限制。
例如:
- 空間坐標體系
- 海龜最開始在畫布的正中心,以此構成絕對坐標系,利用空間坐標和函數,可以改變海龜的位置
- 如最重要的一個函數是
# turtle.goto(x,y)
-
- 它可以讓任意位置的海龜去到達這個位置,例如:
- 運動控制函數
- turtle.forward(d)別名turtle.fd(d)
- d表示行進距離,如果行進距離是負數,表示海龜是倒退着行進的。
- turtle.bd(d)是向海龜的反方向運行
- turtle.circle(r,angle)這是曲線函數,圓心在海龜的左手側距離自己距為r的地方,想要繪制完整圓形,不要寫第二個參數就好
- 角度坐標體系
- 我們可以使用下面函數來改變當前海龜的行進角度
- turtle.seth(angle)
- 這里的angle是將海龜當前的行進方向改變為某一個絕對角度(不是海龜方向,是絕對坐標系中的絕對方向)
- 為了更好改變方向,還可以分別從左右兩個方向改變角度(這個是海龜角度)
- turtle.left(angle)
- turtle.right(angle)
- 要注意的是,改變角度的函數只會改變方向,但是不會自動行進,需要配合其他函數,比如turtle.fd(d)來使用。
- 例如:
- 現在我們再看上面的程序是不是感覺很熟悉了呢,雖然還是有些不是完全懂,別着急,我們慢慢看下面:
- 畫筆控制函數
- 這里將海龜想象為畫筆pen
- 畫筆成對出現
- turtle.penup()別名turtle.pu()抬起畫筆,海龜在飛行
- turtlependown()別名turtle.pd()落下畫筆,海龜在爬行
- 另外:
- turtle.pensize(width)用來設置畫筆寬度。
- turtle.pencolor(color)設置顏色,里面填rgb值。
- 這里要注意要把rge壓成0-1(除以255即可)
- 循環結構與range
- python循環語句最常用方式
- for i(計數變量) in range(循環次數) 后面縮進的代碼將被循環執行
- range()產生循環計數序列
- range(n)
- range(m,n)產生從m到n-1的整數序列
- 看到這個,基本上上面的蟒蛇代碼應該已經能全部看懂了,而且可以自己做出很多改變。這里寫一下注釋:
# python.py import turtle turtle.setup(650,350,200,200)#設置窗體位置和大小 turtle.penup() turtle.fd(-250) turtle.pendown()#海龜飛起落下 這三行相當於海龜的原點改變了 turtle.pensize(25) turtle.pencolor("purple") turtle.seth(-40)#設置剛開始的絕對角度 for i in range(4): turtle.circle(40,80) turtle.circle(-40,80)#4繪制蟒蛇的4個小節 turtle.circle(40,80/2)#繪制少半個弧形 turtle.fd(40) turtle.circle(16,180)#脖子和頭部繪制 turtle.fd(40 * 2/3) turtle.done()#這行代碼使得程序運行后程序不會退出,需要手動關閉窗口 #finish
- 可以把import turtle調庫函數換成from turtle import*,這樣后面就可以不用一直加turtle.了,直接寫函數就ok,只要注意不要重名就可以,或者也可以import turtle as+別名的方式。
視頻學習鏈接: 這是2019下半年的開課,應該每年都會有開課,講的真的很好