使用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

相关推荐
databook2 小时前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风3 小时前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风3 小时前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei19 小时前
python 抽象基类
python
用户83562907805119 小时前
Python 实现 PPT 转 HTML
后端·python
zone77391 天前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone77391 天前
005:RAG 入门-LangChain读取表格数据
后端·python·agent
树獭非懒2 天前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
唐叔在学习2 天前
就算没有服务器,我照样能够同步数据
后端·python·程序员
曲幽2 天前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama