python樱花代码
时间: 2023-11-13 21:55:15 浏览: 68
Python樱花树代码
以下是 Python 实现樱花代码的示例:
```python
import turtle
import random
# 设置画布大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('pink')
# 定义樱花的形状
def sakura():
turtle.begin_fill()
turtle.circle(10, 180)
turtle.circle(25, 110)
turtle.left(50)
turtle.forward(15)
turtle.right(90)
turtle.forward(15)
turtle.left(45)
turtle.circle(35, 180)
turtle.left(45)
turtle.forward(15)
turtle.right(90)
turtle.forward(30)
turtle.circle(20, 180)
turtle.left(90)
turtle.end_fill()
# 定义樱花的颜色
colors = ['pink', 'lightpink', 'hotpink', 'deeppink', 'palevioletred', 'mediumvioletred']
# 随机生成樱花的位置和颜色
for i in range(50):
x = random.randint(-400, 400)
y = random.randint(-250, 250)
color = random.choice(colors)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color(color)
sakura()
# 隐藏画笔
turtle.hideturtle()
# 点击关闭窗口
turtle.exitonclick()
```
阅读全文