用python制作一个樱花树落樱花的代码
时间: 2024-01-22 16:21:36 浏览: 63
Python画樱花树代码
5星 · 资源好评率100%
好的,以下是一个简单的樱花树落樱花的 Python 代码:
```python
import turtle
import random
# 设置画布大小和背景色
canvas = turtle.Screen()
canvas.setup(800, 600)
canvas.bgcolor("#f7d9ea")
# 创建樱花树干
trunk = turtle.Turtle()
trunk.penup()
trunk.goto(-100, -300)
trunk.pendown()
trunk.color("#8b5a2b")
trunk.pensize(30)
trunk.setheading(90)
trunk.forward(200)
# 创建樱花树枝
branch1 = turtle.Turtle()
branch1.penup()
branch1.goto(-100, -100)
branch1.pendown()
branch1.color("#8b5a2b")
branch1.pensize(20)
branch1.setheading(60)
branch1.forward(100)
branch2 = branch1.clone()
branch2.setheading(120)
branch2.forward(100)
# 创建樱花花瓣
petals = []
colors = ["#fff0f5", "#ffe4e1", "#ffb6c1", "#db7093", "#c71585"]
for i in range(50):
petal = turtle.Turtle()
petal.penup()
petal.shape("circle")
petal.color(random.choice(colors))
petal.shapesize(random.randint(1, 3))
petal.setheading(random.randint(0, 360))
petal.goto(random.randint(-400, 400), random.randint(100, 300))
petals.append(petal)
# 让樱花花瓣落下
while True:
for petal in petal:
petal.sety(petal.ycor() - random.randint(1, 5))
if petal.ycor() < -300:
petal.sety(random.randint(100, 300))
turtle.done()
```
运行代码后,将会出现一个樱花树和落下的樱花花瓣。你可以根据需要对代码进行修改和调整来满足你的需求。
阅读全文