使用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()
相关推荐
草莓熊Lotso14 小时前
【LangChain】输出解析器全解:让大模型输出从 “聊天” 变 “机器可读”
服务器·数据库·python·langchain·pip
卷无止境14 小时前
Python 脚本工程化:从"能跑就行"到"生产级代码"
后端·python
卷无止境14 小时前
Python 函数式编程:从思想到实践
后端·python
人间凡尔赛1 天前
Next.js 16 生产级实战:Cache Components + View Transitions 完整指南
开发语言·javascript·ecmascript
满怀冰雪1 天前
06-自动微分入门:用 Paddle 计算梯度
人工智能·python·深度学习·paddle
稚南城才子,乌衣巷风流1 天前
函数:编程中的核心概念
开发语言·前端·javascript
阿米亚波1 天前
【C++】流式数据输入处理(不完全整理)
开发语言·c++·笔记
大模型码小白1 天前
JAVA 集合框架进阶:List 与 Set 的深度解析与实战
java·开发语言·人工智能·windows·语言模型·list·ai编程
皓月斯语1 天前
【C++基础】三目运算符
开发语言·数据结构·c++
初願致夕霞1 天前
C++懒汉单例设计详解
服务器·开发语言·c++