python雪花飘落
时间: 2023-11-10 10:03:44 浏览: 170
可以使用Python的turtle库实现雪花飘落的效果。以下是一个简单的示例代码:
```python
import turtle
import random
# 设置画笔
turtle.speed(0)
turtle.penup()
turtle.hideturtle()
# 雪花形状
turtle.getscreen().register_shape("snowflake", ((-5,-5), (-5,5), (5,5), (5,-5), (0,0)))
# 雪花列表
snowflakes = []
# 创建雪花函数
def create_snowflake():
x = random.randint(-turtle.window_width()//2, turtle.window_width()//2)
y = turtle.window_height()//2 + 50
snowflake = turtle.Turtle(shape="snowflake")
snowflake.color("white")
snowflake.penup()
snowflake.setpos(x, y)
snowflake.speed = random.randint(1, 5)
snowflakes.append(snowflake)
# 循环绘制雪花
while True:
create_snowflake()
for flake in snowflakes:
y = flake.ycor()
y -= flake.speed
flake.sety(y)
# 雪花超出屏幕则删除
if flake.ycor() < -turtle.window_height()//2:
flake.clear()
flake.hideturtle()
snowflakes.remove(flake)
# 点击关闭窗口
turtle.exitonclick()
```
阅读全文