使用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()
相关推荐
Wellijohn1 小时前
我把公司内部的 12 个 CRUD 接口交给了 AI Agent,然后省出了两周
人工智能·python·mcp
布莱克6051 小时前
Swift 方法详解:实例方法、类型方法与 mutating 机制
服务器·开发语言·ios·ssh·swift·方法
2601_967980112 小时前
python pytest怎样测试代码
python·pytest·测试·文档·断言
伞伞悦读3 小时前
【第92期】逻辑回归的分数概率与阈值的实验与排查
人工智能·python·深度学习·机器学习
Risehuxyc3 小时前
PHP能不能在参数里放匿名函数
开发语言·php
码云社区3 小时前
Java家政服务系统源码:人员技能、服务范围与多商家业务落地
java·开发语言·家政服务·家政小程序·家政系统
ch6233 小时前
🚀 从零构建本地文档智能审查 Agent:Vibe Coding 实战指南
python
FRing034 小时前
vue3干货总结3(defineOptions(Vue3.3)、defineModel(Vue3.4)、Pinia)
开发语言·前端·vue.js·vue3
摇滚侠4 小时前
《On Java 中文版 基础卷》阅读笔记 接口 18
java·笔记·python
Thanks_ks5 小时前
AI Agent 开发入门:用 Function Calling 让大模型学会 “动手干活”,附订单查询机器人实战
python·ai·大模型·openai·agent·通义千问·mcp