Python画樱花树
时间: 2023-12-14 16:29:48 浏览: 145
好的,我可以为您提供一个简单的Python代码来画樱花树。请确保您已经安装了`turtle`模块。
以下是示例代码:
```python
import turtle
import random
# 设置画布
turtle.setup(800, 600)
turtle.bgcolor('pink')
# 画樱花的树干
def draw_tree():
turtle.speed(1)
turtle.penup()
turtle.left(90)
turtle.backward(200)
turtle.pendown()
turtle.color('brown')
turtle.pensize(30)
turtle.forward(100)
turtle.right(45)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.right(45)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
# 画樱花的花瓣
def draw_flower():
turtle.speed(5)
colors = ['pink', 'lightpink', 'hotpink', 'deeppink', 'palevioletred', 'mediumvioletred']
for _ in range(20):
turtle.penup()
x = random.randint(-400, 400)
y = random.randint(0, 400)
z = random.randint(10, 40)
turtle.goto(x, y)
turtle.pendown()
turtle.color(random.choice(colors))
turtle.begin_fill()
turtle.circle(z)
turtle.end_fill()
draw_tree()
draw_flower()
turtle.done()
```
希望这段代码能帮助到您!如果有任何问题,请随时提问。
阅读全文