使用Python的pygame库实现下雪的效果

使用Python的pygame库实现下雪的效果

关于Python中pygame游戏模块的安装使用可见 https://blog.csdn.net/cnds123/article/details/119514520

先给出效果图:

源码如下:

python 复制代码
import pygame
import random

# 初始化pygame
pygame.init()

# 设置屏幕尺寸
width, height = 800, 600
screen = pygame.display.set_mode((width, height))

# 设置雪花属性
snowflakes = []
for i in range(50):
    x = random.randrange(0, width)
    y = random.randrange(0, height)
    speed = random.uniform(1, 3)
    size = random.randint(3, 6)  # 雪花大小参数
    snowflakes.append([x, y, speed, size])

# 循环直到用户关闭窗口
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 填充屏幕颜色
    screen.fill((200, 200, 200))  # (0, 0, 0)黑色;(200, 200, 200)阴天

    # 绘制雪花
    for flake in snowflakes:
        pygame.draw.circle(screen, (255, 255, 255), (int(flake[0]), int(flake[1])), flake[3])  # 使用大小参数绘制雪花
        flake[1] += flake[2]  # 移动雪花
        if flake[1] > height:
            flake[1] = random.randrange(-50, -10)
            flake[0] = random.randrange(0, width)

    # 更新屏幕
    pygame.display.flip()

    # 控制帧率
    pygame.time.Clock().tick(30)

# 退出pygame
pygame.quit()

下面给出改进版

效果图:

使用一张背景图片(我这里文件名:snow_background.jpg),和代码文件放在同一目录下

源码如下:

python 复制代码
import pygame
import random

# 初始化pygame
pygame.init()

# 设置屏幕尺寸
width, height = 800, 600
screen = pygame.display.set_mode((width, height))

# 加载背景图片
background = pygame.image.load('snow_background.jpg')
background = pygame.transform.scale(background, (width, height))

# 设置雪花属性
snowflakes = []
for i in range(50):
    x = random.randrange(0, width)
    y = random.randrange(0, height)
    speed = random.uniform(1, 3)
    size = random.randint(3, 6)  # 雪花大小参数
    snowflakes.append([x, y, speed, size])

# 循环直到用户关闭窗口
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 填充屏幕颜色
    #screen.fill((200, 200, 200))  # (0, 0, 0)黑色;(200, 200, 200)阴天

    # 绘制背景图片
    screen.blit(background, (0, 0))
    

    # 移动雪花并重新绘制
    for flake in snowflakes:
        pygame.draw.circle(screen, (255, 255, 255), (int(flake[0]), int(flake[1])), flake[3])  # 使用大小参数绘制雪花
        flake[1] += flake[2]  # 移动雪花
        if flake[1] > height:
            flake[1] = random.randrange(-50, -10)
            flake[0] = random.randrange(0, width)

    # 更新屏幕
    pygame.display.flip()

    # 控制帧率
    pygame.time.Clock().tick(30)

# 退出pygame
pygame.quit()

附:RGB 颜色表 https://www.codeeeee.com/color/rgb.html

相关推荐
databook8 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室9 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三10 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271114 小时前
Python之语言特点
python
刘立军14 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机17 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机18 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i20 小时前
django中的FBV 和 CBV
python·django
c8i20 小时前
python中的闭包和装饰器
python
这里有鱼汤1 天前
小白必看:QMT里的miniQMT入门教程
后端·python