使用Python写一颗樱花树
时间: 2023-04-07 18:04:22 浏览: 121
用python绘制樱花树
很高兴回答你的问题!以下是使用Python编写樱花树的代码:
```python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('pink')
# 定义绘制樱花的函数
def draw_sakura(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
# 绘制花瓣
petal_color = random.choice(['pink', 'lightpink', 'hotpink'])
turtle.fillcolor(petal_color)
turtle.begin_fill()
turtle.circle(10, 60)
turtle.left(120)
turtle.circle(10, 60)
turtle.end_fill()
# 绘制花蕊
turtle.penup()
turtle.goto(x, y+10)
turtle.pendown()
turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.circle(5)
turtle.end_fill()
# 绘制樱花树干
turtle.penup()
turtle.goto(-200, -200)
turtle.pendown()
turtle.pensize(20)
turtle.setheading(90)
turtle.color('brown')
turtle.forward(200)
# 绘制樱花
for i in range(50):
x = random.randint(-300, 300)
y = random.randint(-100, 200)
draw_sakura(x, y)
# 隐藏画笔
turtle.hideturtle()
turtle.done()
```
希望这个代码能够帮助你绘制出美丽的樱花树!
阅读全文