Python画樱花树代码
时间: 2023-11-21 09:10:22 浏览: 96
python画樱花树
以下是Python画樱花树的代码:
```python
import turtle
import random
# 设置画笔
turtle.speed('fastest')
turtle.penup()
turtle.left(90)
turtle.goto(-300, 0)
turtle.pendown()
# 定义绘制分支的函数
def draw_branch(branch_length):
if branch_length > 3:
if branch_length < 50:
turtle.pencolor('green')
turtle.pensize(branch_length / 10)
else:
turtle.pencolor('brown')
turtle.pensize(branch_length / 25)
turtle.forward(branch_length)
angle = random.uniform(22, 30)
turtle.right(angle)
draw_branch(branch_length - random.uniform(10, 15))
turtle.left(2 * angle)
draw_branch(branch_length - random.uniform(10, 15))
turtle.right(angle)
turtle.backward(branch_length)
# 绘制樱花
def draw_sakura():
turtle.hideturtle()
turtle.getcanvas().postscript(file="sakura.eps")
turtle.showturtle()
turtle.bgcolor('DeepSkyBlue')
turtle.speed(10)
# 绘制主干
turtle.left(90)
turtle.forward(200)
# 绘制分支
draw_branch(100)
# 结束绘制
turtle.done()
draw_sakura()
```
注:此代码来源于网络,本人不保证代码效果和版权问题。如果您要使用此代码,请自行核实和处理版权问题。
阅读全文