使用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()
相关推荐
见叶之秋8 分钟前
C++ 泛型世界的两块拼图:容器适配器与仿函数
java·开发语言
逗脑IDE28 分钟前
零基础学ESP32:RFID无线射频卡——让ESP32拥有“刷卡“能力!
开发语言·网络·人工智能·python·esp32·硬件开发
小师兄吃牛肉32 分钟前
C语言篇:内联函数
c语言·开发语言
协皓家具41 分钟前
2026年广州岛台吧台椅厂家有啥新变化
大数据·运维·python·devops
yuniko-n1 小时前
【Java】字符串相关坑点
java·开发语言·python
用户019027581611 小时前
如何用 Python 统计 A 股连续涨停(几连板)?
python
外收内放1 小时前
Python基础语法练习题(拓展1)
python·游戏
jimy11 小时前
虚函数vtable,链接时看到构造函数,linker把符号地址写入vtable(三)
开发语言·c++
H_unique1 小时前
Chat2Excel:接口自动化测试
python·测试工具·自动化
ss2731 小时前
AI全栈实战 | 3.3-02 Python 并发:有 GIL 为什么还用多线程,asyncio 和 JS 事件循环同源不同味
开发语言·javascript·python