python绘画樱花树
时间: 2023-11-11 18:00:50 浏览: 91
可以使用Python的turtle库来绘制樱花树,以下是一个简单的示例代码:
```python
import turtle
import random
def tree(branchLen, t):
if branchLen > 5:
angle = random.randint(20, 30)
sf = random.uniform(0.6, 0.8)
t.pensize(branchLen / 10)
t.forward(branchLen)
t.right(angle)
tree(branchLen * sf, t)
t.left(angle * 2)
tree(branchLen * sf, t)
t.right(angle)
t.backward(branchLen)
def petal(t):
for i in range(200):
radius = random.randint(20, 50)
x = random.randint(-300, 300)
y = random.randint(-200, 200)
t.penup()
t.goto(x, y)
t.pendown()
red_amount = random.randint(50, 100) / 100.0
blue_amount = random.randint(50, 100) / 100.0
green_amount = random.randint(50, 100) / 100.0
t.fillcolor((red_amount, blue_amount, green_amount))
t.begin_fill()
t.circle(radius)
t.end_fill()
def main():
t = turtle.Turtle()
myWin = turtle.Screen()
t.left(90)
t.up()
t.backward(200)
t.down()
t.color("brown")
tree(100, t)
petal(t)
myWin.exitonclick()
main()
```
阅读全文