使用Python来下一场雪

具体效果:(大雪缓缓下落)

完整代码:

复制代码
import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置窗口
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("下雪动画")

# 定义雪花类
class Snowflake:
    def __init__(self):
        self.x = random.randint(0, width)
        self.y = random.randint(0, height)
        self.size = random.randint(2, 5)
        self.speed = random.uniform(1, 3)

    def fall(self):
        self.y += self.speed
        if self.y > height:
            self.y = 0
            self.x = random.randint(0, width)

    def draw(self):
        pygame.draw.circle(screen, (255, 255, 255), (self.x, self.y), self.size)

# 创建雪花列表
snowflakes = [Snowflake() for _ in range(100)]

# 主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0, 0, 0))  # 填充背景
    for snowflake in snowflakes:
        snowflake.fall()
        snowflake.draw()

    pygame.display.flip()
    pygame.time.delay(30)

pygame.quit()
相关推荐
Never_Satisfied7 分钟前
在c#中,使用windows自带功能将文件夹打包为ZIP
开发语言·windows·c#
hnxaoli23 分钟前
win10程序(十六)通达信参数清洗器
开发语言·python·小程序·股票·炒股
电饭叔38 分钟前
文本为 “ok”、前景色为白色、背景色为红色,且点击后触发 processOK 回调函数的 tkinter 按钮
开发语言·python
雷电法拉珑1 小时前
财务数据批量采集
linux·前端·python
Never_Satisfied2 小时前
在c#中,string.replace会替换所有满足条件的子字符串,如何只替换一次
开发语言·c#
shangjian0073 小时前
Python基础-With关键字
python
Demon_Hao3 小时前
JAVA快速对接三方支付通道标准模版
java·开发语言
zchxzl3 小时前
亲测2026京津冀可靠广告展会
大数据·人工智能·python
xyq20243 小时前
C# 判断语句详解与应用
开发语言