62 lines
1.5 KiB
Python
62 lines
1.5 KiB
Python
import turtle
|
|
import random
|
|
import time
|
|
#from PIL import Image
|
|
|
|
# 设置屏幕
|
|
screen = turtle.Screen()
|
|
screen.setup(width=800, height=600)
|
|
screen.bgcolor("black") # 背景色可以设置为黑色或其他
|
|
screen.title("Snowfall Animation")
|
|
screen.tracer(0)
|
|
|
|
## 加载 .jpg 图片并转换为 .gif 格式
|
|
#image_path = "dy.jpg"
|
|
#image = Image.open(image_path)
|
|
## 将图片保存为 .gif 格式
|
|
#image.save("dy.gif", "GIF")
|
|
|
|
# 设置背景图为 .gif 格式
|
|
screen.bgpic("dy.gif")
|
|
|
|
# 创建雪花类
|
|
class Snowflake(turtle.Turtle):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.hideturtle()
|
|
self.penup()
|
|
self.speed(0)
|
|
self.goto(random.randint(-400, 400), random.randint(100, 300))
|
|
self.size = random.uniform(0.5, 1.5)
|
|
self.fall_speed = random.uniform(1, 3)
|
|
self.create_snowflake()
|
|
|
|
def create_snowflake(self):
|
|
self.clear()
|
|
self.color("white")
|
|
self.shape("circle") # 设置雪花为圆形
|
|
self.shapesize(self.size)
|
|
self.showturtle()
|
|
|
|
def fall(self):
|
|
new_y = self.ycor() - self.fall_speed
|
|
if new_y < -300:
|
|
# 雪花重新回到顶部
|
|
new_x = random.randint(-400, 400)
|
|
self.goto(new_x, random.randint(100, 300))
|
|
else:
|
|
self.goto(self.xcor(), new_y)
|
|
|
|
# 创建多个雪花
|
|
snowflakes = [Snowflake() for _ in range(50)]
|
|
|
|
# 动画循环
|
|
while True:
|
|
for snowflake in snowflakes:
|
|
snowflake.fall()
|
|
screen.update()
|
|
time.sleep(0.03)
|
|
|
|
# 关闭窗口
|
|
screen.mainloop()
|