python代码画樱花
时间: 2023-07-03 18:07:57 浏览: 73
以下是使用turtle库绘制樱花的Python代码:
```python
import turtle
import random
# 设置画布
canvas = turtle.Screen()
canvas.bgcolor('black')
canvas.title('樱花')
# 设置画笔
pen = turtle.Turtle()
pen.penup()
pen.speed(0)
pen.hideturtle()
# 定义花瓣
colors = ['pink', 'lightpink', 'hotpink', 'deeppink', 'palevioletred', 'mediumvioletred']
def draw_petal(size):
pen.color(random.choice(colors))
pen.begin_fill()
pen.circle(size)
pen.end_fill()
# 绘制樱花
def draw_cherry_blossom(x, y):
pen.setpos(x, y)
size = random.randint(10, 40)
draw_petal(size)
for i in range(6):
pen.right(60)
draw_petal(size)
# 随机生成樱花
for i in range(50):
x = random.randint(-300, 300)
y = random.randint(-200, 200)
draw_cherry_blossom(x, y)
# 隐藏画笔
pen.hideturtle()
turtle.done()
```
运行代码,即可在画布上看到随机生成的樱花。
阅读全文