使用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()
相关推荐
清弦墨客12 分钟前
【蓝桥杯】43697.机器人塔
python·蓝桥杯·程序算法
※DX3906※1 小时前
cpp实战项目—string类的模拟实现
开发语言·c++
wjs20241 小时前
Nginx 安装配置指南
开发语言
美味小鱼1 小时前
实践Rust:编写一个猜数字游戏
开发语言·游戏·rust
Dr.勿忘2 小时前
C#面试常考随笔8:using关键字有哪些用法?
开发语言·unity·面试·c#·游戏引擎
RZer2 小时前
Hypium+python鸿蒙原生自动化安装配置
python·自动化·harmonyos
dal118网工任子仪2 小时前
92,[8] 攻防世界 web Web_php_wrong_nginx_config
开发语言·php
wjs20242 小时前
SQLite Update 语句详解
开发语言
加油,旭杏2 小时前
【go语言】接口
开发语言·后端·golang
xianwu5433 小时前
反向代理模块jmh
开发语言·网络·数据库·c++·mysql