使用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()
相关推荐
苦瓜汤补钙9 分钟前
Oracle JDK8 环境配置-Win11
开发语言·数据库·笔记·oracle
overmind11 分钟前
oeasy Python 102 集合_运算_交集_并集_差集_对称差集
开发语言·python
爱吃提升29 分钟前
Python自动驾驶图像识别完整实战教程(OpenCV+YOLOv8,附可直接运行源码)
python·opencv·自动驾驶
研☆香30 分钟前
JavaScript 绘制简单不规则图形:三角形与五角星实战教程
开发语言·javascript·ecmascript
数聚天成DeepSData1 小时前
企业知识库 RAG 数据准备与文档清洗:Dify、RAGFlow、扣子选型指南
开发语言·人工智能·机器学习·自然语言处理·sentinel·cocos2d
我是唐青枫1 小时前
Java SLF4J 实战指南:从日志门面到 Logback、MDC 和链路追踪
java·开发语言·logback
statistican_ABin1 小时前
2026 FIFA 世界杯比赛与球队数据探索性分析
人工智能·python·数据挖掘·数据分析
qetfw1 小时前
yt-dlp:下载公开视频、字幕、封面和音频的实用命令
python·音视频·开源项目·效率工具
aramae1 小时前
C++11:现代C++的里程碑
c语言·开发语言·c++·windows·git·后端
QH_ShareHub1 小时前
GPU 完整运行过程:驱动、CUDA Toolkit、Conda、Python PyTorch 与 R torch
pytorch·python·conda