为什么要画樱花
今天,我在路边看到了一颗樱花树。从侧面看去他是异常的美丽。
所以我们今天就用turtle来画樱花树吧!
樱花树
- 为什么要画樱花
- 所需模块
- 樱花树的躯干
- 掉落的花般
- 运行部分
- 总代码
所需模块
需要的库
import turtle import random import time
需要的模块
turtle.setheading() #快速绘图 turtle.forward() #向当前画笔方向移动distance像素长 turtle.backward() #向当前画笔相反方向移动distance像素长度 turtle.left() #逆时针移动 turtle.right() #顺时针移动 turtle.penup() #移动时不绘制图形,提起笔,用于另起一个地方绘制时用 turtle.pendown() #移动时绘制图形,缺省时也为绘制 turtle.fillcolor() #绘制图形的填充颜色 turtle.end_fill() #填充完成 turtle.begin_fill() #准备开始填充图形 turtle.circle() #以给定半径画圆 turtle.goto() #将画笔移动到坐标为x,y的位置 turtle.tracer() #追踪运行轨迹 turtle.hideturtle() #隐藏箭头显示 turtle.pensize() #绘制图形时的宽度
樱花树的躯干
# 画樱花的躯干(60,turtle) def Tree(branch, turtle): time.sleep(0.0005) if branch > 3: if 8 <= branch <= 12: if random.randint(0, 2) == 0: turtle.color('snow') else: turtle.color('lightcoral') turtle.pensize(branch / 3) elif branch < 8: if random.randint(0, 1) == 0: turtle.color('snow') else: turtle.color('lightcoral') turtle.pensize(branch / 2) else: turtle.color('sienna') turtle.pensize(branch / 10) # 6 turtle.forward(branch) a = 1.5 * random.random() turtle.right(20 * a) b = 1.5 * random.random() Tree(branch - 10 * b,turtle) turtle.left(40 * a) Tree(branch - 10 * b, turtle) turtle.right(20 * a) turtle.up() turtle.backward(branch) turtle.down()
掉落的花般
# 掉落的花瓣 def Petal(m, t): for i in range(m): a = 200 - 400 * random.random() b = 10 - 20 * random.random() turtle.up() turtle.forward(b) turtle.left(90) turtle.forward(a) turtle.down() turtle.color('lightcoral') # 淡珊瑚色 turtle.circle(1) turtle.up() turtle.backward(a) turtle.right(90) turtle.backward(b)
运行部分
turtle.hideturtle() # 隐藏画笔 turtle.getscreen().tracer(5, 0) #w.screensize(bg='wheat') wheat小麦 turtle.left(90) turtle.up() turtle.backward(150) turtle.down() turtle.color('sienna') # 树干 Tree(60, turtle) # 掉落的花瓣 Petal(200, turtle) w.exitonclick()
总代码
import turtle import random import time # 画樱花的躯干(60,turtle) def Tree(branch, turtle): time.sleep(0.0005) if branch > 3: if 8 <= branch <= 12: if random.randint(0, 2) == 0: turtle.color('snow') else: turtle.color('lightcoral') turtle.pensize(branch / 3) elif branch < 8: if random.randint(0, 1) == 0: turtle.color('snow') else: turtle.color('lightcoral') turtle.pensize(branch / 2) else: turtle.color('sienna') turtle.pensize(branch / 10) # 6 turtle.forward(branch) a = 1.5 * random.random() turtle.right(20 * a) b = 1.5 * random.random() Tree(branch - 10 * b,turtle) turtle.left(40 * a) Tree(branch - 10 * b, turtle) turtle.right(20 * a) turtle.up() turtle.backward(branch) turtle.down() # 掉落的花瓣 def Petal(m, t): for i in range(m): a = 200 - 400 * random.random() b = 10 - 20 * random.random() turtle.up() turtle.forward(b) turtle.left(90) turtle.forward(a) turtle.down() turtle.color('lightcoral') # 淡珊瑚色 turtle.circle(1) turtle.up() turtle.backward(a) turtle.right(90) turtle.backward(b) turtle.hideturtle() # 隐藏画笔 turtle.getscreen().tracer(5, 0) #w.screensize(bg='wheat') wheat小麦 turtle.left(90) turtle.up() turtle.backward(150) turtle.down() turtle.color('sienna') # 树干 Tree(60, turtle) # 掉落的花瓣 Petal(200, turtle) w.exitonclick()
c/c++开发分享樱花树地址:https://blog.csdn.net/skywalle/article/details/107582390
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/598513.html