樱花飘落代码Python
时间: 2023-05-27 07:06:29 浏览: 148
import turtle
import random
# 设置画布大小和背景色
turtle.setup(800, 600)
turtle.bgcolor('#18453B')
# 定义樱花的颜色列表
colors = ['#F7819F', '#F5A9BC', '#EDD1DE', '#F8C471', '#F7DC6F']
# 定义画樱花的函数
def draw_sakura():
turtle.penup()
x = random.randint(-350, 350)
y = random.randint(-250, 250)
turtle.goto(x, y)
turtle.pendown()
turtle.setheading(0)
turtle.pensize(random.uniform(1, 4))
color = random.choice(colors)
turtle.pencolor(color)
turtle.fillcolor(color)
turtle.begin_fill()
turtle.circle(random.uniform(20, 30), 90)
turtle.circle(random.uniform(20, 30), 90)
turtle.right(135)
turtle.circle(random.uniform(20, 30), 90)
turtle.circle(random.uniform(20, 30), 90)
turtle.end_fill()
# 循环画樱花
for i in range(50):
draw_sakura()
# 隐藏画笔并保持窗口不关闭
turtle.hideturtle()
turtle.done()
阅读全文