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

相关推荐
你好潘先生5 小时前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
Agent_大师5 小时前
WebSocket 行情重连成功,K线缺口不会自动消失
python
荣码5 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
copyer_xyf5 小时前
FastAPI 如何连接 MySQL
后端·python
apocelipes19 小时前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户83562907805120 小时前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent1 天前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m6251 天前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python
SelectDB2 天前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码2 天前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python