最近翻到一篇知乎,上面有不少用Python(大多是turtle库)绘制的树图,感觉很漂亮,我整理了一下,挑了一些我觉得不错的代码分享给大家(这些我都测试过,确实可以生成喔~)
重中之重,就是将这些代码封装成.exe文件,在别人的电脑没有pycharm的时候也可以运行展示效果
第一种圣诞树
效果(有绘制过程)
代码
from turtle import *
import timesetup(500, 500, startx=None, starty=None)
speed(0)
pencolor("pink")
pensize(10)
penup()
hideturtle()
goto(0, 150)
showturtle()
pendown()
shape(name="classic")
# 1
seth(-120)
for i in range(10):fd(12)right(2)
penup()
goto(0, 150)
seth(-60)
pendown()
for i in range(10):fd(12)left(2)
seth(-150)
penup()
fd(10)
pendown()
for i in range(5):fd(10)right(15)
seth(-150)
penup()
fd(8)
pendown()
for i in range(5):fd(10)right(15)
seth(-155)
penup()
fd(5)
pendown()
for i in range(5):fd(7)right(15)
# 2
penup()
goto(-55, 34)
pendown()
seth(-120)
for i in range(10):fd(8)right(5)penup()
goto(50, 35)
seth(-60)
pendown()
for i in range(10):fd(8)left(5)
seth(-120)
penup()
fd(10)
seth(-145)
pendown()
for i in range(5):fd(10)right(15)
penup()
fd(10)
seth(-145)
pendown()
for i in range(5):fd(12)right(15)
penup()
fd(8)
seth(-145)
pendown()
for i in range(5):fd(10)right(15)
penup()
seth(-155)
fd(8)
pendown()
for i in range(5):fd(11)right(15)
# 3
penup()
goto(-100, -40)
seth(-120)
pendown()
for i in range(10):fd(6)right(3)
penup()
goto(80, -39)
seth(-50)
pendown()
for i in range(10):fd(6)left(3)
seth(-155)
penup()
fd(10)
pendown()
for i in range(5):fd(8)right(10)
penup()
fd(8)
seth(-145)
pendown()
for i in range(7):fd(8)right(10)
penup()
fd(8)
seth(-145)
pendown()
for i in range(7):fd(7)right(10)
penup()
fd(8)
seth(-145)
pendown()
for i in range(7):fd(7)right(10)
penup()
fd(8)
seth(-140)
pendown()
for i in range(7):fd(6)right(10)# 4
penup()
goto(-120, -95)
seth(-130)
pendown()
for i in range(7):fd(10)right(5)
penup()
goto(100, -95)
seth(-50)
pendown()
for i in range(7):fd(10)left(5)
penup()
seth(-120)
fd(10)
seth(-155)
pendown()
for i in range(6):fd(8)right(10)
penup()
seth(-160)
fd(10)
seth(-155)
pendown()
for i in range(6):fd(8)right(10)
penup()
seth(-160)
fd(10)
seth(-155)
pendown()
for i in range(6):fd(8)right(10)
penup()
seth(-160)
fd(10)
seth(-160)
pendown()
for i in range(6):fd(8)right(10)
penup()
seth(-160)
fd(10)
seth(-160)
pendown()
for i in range(6):fd(8)right(10)
penup()
seth(-160)
fd(10)
seth(-165)
pendown()
for i in range(5):fd(10)right(11)
# 5
penup()
goto(-70, -165)
seth(-85)
pendown()
for i in range(3):fd(5)left(3)
penup()
goto(70, -165)
seth(-95)
pendown()
for i in range(3):fd(5)right(3)
seth(-170)
penup()
fd(10)
pendown()
pendown()
for i in range(10):fd(12)right(2)
# 6
penup()
goto(70, -165)
pendown()
seth(-90)
pensize(8)
pencolor("#de8891")
circle(-20, 90)penup()
goto(30, -185)
pendown()
seth(-180)
pensize(8)
pencolor("#de8891")
fd(40)penup()
goto(-5, -170)
pendown()
seth(-180)
pensize(8)
pencolor("#de8891")
fd(35)def guest(x, y, z):penup()goto(x, y)seth(-z)pendown()for angel in range(5):fd(10)right(10)def guet(x, y, z):penup()goto(x, y)seth(-z)pendown()for angel in range(5):fd(10)left(10)def qu(x, y, z):penup()goto(x, y)seth(-z)pendown()for angel in range(5):fd(6)right(10)seth(-150)fd(20)# 树枝
guest(-70, -150, 160)
guest(100, -150, 160)
guet(110, -110, 50)
guest(160, -140, 150)
qu(80, -120, 180)
guest(70, -85, 165)
guest(-40, -85, 165)
guet(90, -50, 50)
guest(130, -80, 150)
pencolor("pink")
qu(-40, -60, 180)
pencolor('#de8891')
qu(80, -30, 180)
pencolor("pink")
qu(40, 10, 180)
pencolor("#de8891")
guest(-60, 30, 120)
guest(-20, -20, 150)
guet(45, 40, 60)
guest(-30, 40, 170)
guest(-30, 110, 115)
guet(40, 90, 60)
guest(80, 50, 160)
pencolor("#de8891")def hdj(x, y):penup()goto(x, y)seth(80)pendown()pensize(2)circle(5)seth(10)fd(15)seth(120)fd(20)seth(240)fd(20)seth(180)fd(20)seth(-60)fd(20)seth(50)fd(20)seth(-40)fd(30)seth(-130)fd(5)seth(135)fd(30)seth(-60)fd(30)seth(-150)fd(6)seth(110)fd(30)def uit(x, y):penup()goto(x, y)pendown()pensize(2)circle(5)seth(-10)fd(15)seth(90)fd(15)seth(200)fd(15)seth(160)fd(15)seth(-90)fd(15)seth(10)fd(15)seth(-60)fd(20)seth(-180)fd(5)seth(110)fd(20)seth(-90)fd(20)seth(-180)fd(6)seth(70)fd(15)hideturtle()def yut(x, y, z):penup()goto(x, y)pendown()seth(z)for po in range(5):fd(4)left(36)def ytu(x, y, z):penup()goto(x, y)pendown()seth(z)for kk in range(5):fd(4)left(36)# 小蝴蝶结
seth(0)
uit(40, -160)
hdj(-80, -120)
yut(-67, -115, 120)
yut(-86, -123, 150)
hdj(40, -50)
yut(52, -45, 130)
yut(34, -55, 160)
seth(0)
uit(-20, -60)
ytu(-4, -60, 100)
ytu(-20, -60, 120)
hdj(-30, 20)
yut(-15, 25, 130)
yut(-40, 20, 180)
uit(30, 70)
ytu(45, 70, 100)
ytu(30, 70, 120)# 大蝴蝶结
pencolor("#f799e6")
pensize(5)
penup()
seth(0)
goto(0, 150)
pendown()
circle(10)
seth(-15)
fd(40)
seth(90)
fd(40)
seth(200)
fd(40)
seth(160)
fd(40)
seth(-90)
fd(40)
seth(15)
fd(40)
seth(-70)
pencolor("#f799e6")
pensize(4)
fd(40)
seth(-180)
fd(10)
seth(100)
fd(40)
seth(-100)
fd(40)
seth(-180)
fd(10)
seth(70)
fd(40)
penup()
seth(0)
goto(0, 130)
pencolor("pink")
pendown()def iou(x, y, z):penup()goto(x, y)pencolor("#f799e6")pendown()seth(z)for po in range(10):fd(4)left(18)seth(0)
iou(35, 145, 100)
iou(-7, 145, 110)
pencolor("red")
pensize(7)
penup()
goto(-35, 135)
pendown()# 圣诞帽
seth(-20)
pensize(2)
penup()
goto(-30, -120)
pencolor("black")
pendown()
fillcolor("red")
fd(30)
circle(4, 180)
fd(30)
circle(4, 180)
penup()
goto(-25, -115)
seth(75)
pendown()
begin_fill()
for i in range(5):fd(6)right(20)
seth(-10)
for i in range(5):fd(8)right(15)
seth(145)
for i in range(5):fd(5)left(2)
seth(90)
for i in range(5):fd(1)left(2)
seth(-90)
for i in range(4):fd(4)right(6)
seth(161)
fd(30)
end_fill()
pensize(1)
pencolor("black")def koc(x, y, size):pensize(2)pencolor("black")penup()goto(x, y)pendown()begin_fill()fillcolor("yellow")for i in range(5):left(72)fd(size)right(144)fd(size)end_fill()# 星星
seth(-15)
koc(-120, -70, 10)
seth(10)
koc(100, -20, 10)
seth(-10)
koc(10, 40, 10)
seth(30)
koc(-80, 60, 10)
koc(100, -150, 10)
koc(-140, -150, 10)
koc(20, 120, 10)# 袜子
seth(-20)
pensize(2)
penup()
goto(-20, 80)
pencolor("black")
pendown()
fillcolor("red")
fd(25)
circle(4, 180)
fd(25)
circle(4, 180)
penup()
goto(-15, 80)
pendown()
begin_fill()
fillcolor("red")
seth(-120)
fd(20)
seth(150)
fd(5)
circle(7, 180)
fd(15)
circle(5, 90)
fd(30)
seth(160)
fd(18)
end_fill()
penup()
seth(0)
goto(100, -230)
pendown()
write("Merry Christmas", align="right", font=("方正黄草简体", 24, "bold"))
done()
加名字的话就把下面代码的注释Merry Christmas后面或者讲这句话一起改掉都可以
第二种圣诞树
圣诞树 (动态生成效果)代码:
from turtle import *
import random
import timen = 100.0speed("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: returnforward(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)
第三种樱花树
动态生成樱花
效果图(这个是动态的):
实现代码
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) # 6t.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.25pencolor(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.5pencolor(n,n*0.8,n*0.8)circle(3)left(90)#添加0.3倍的飘落叶子if(random()>0.7):pu()#飘落t = heading()an = -40 +random()*40setheading(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.5pencolor(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.25pencolor(t, t, t)pensize(n / 4)forward(l)if n > 0:b = random() * 15 + 10c = random() * 15 + 10d = 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.5pencolor(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()
下面就是这波的重中之重,就是将这些代码封装成.exe文件,在别人的电脑没有pycharm的时候也可以运行展示效果。
pip install pyinstaller
下载pyinstaller就可以转换成.exe文件了
进入代码的路径,终端输入下面代码
pyinstaller -F --onefile ***.py
***是文件名字
py转exe
之后会生成一个dist的文件夹,那个便可以直接运行
然后这个exe文件可以直接发给别人,没有解释器也可以看到效果哦
感兴趣的话就赶紧尝试一下吧!
哥哥姐姐们麻烦随手一个赞赞