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

相关推荐
一朵小花几秒前
Python中with的用法
python
滴_咕噜咕噜6 分钟前
C#基础总结:常用的数据结构
开发语言·数据结构·c#
martian6657 分钟前
【Java高级篇】——第16篇:高性能Java应用优化与调优
java·开发语言·jvm
m0_7482323924 分钟前
基于OpenCV和Python的人脸识别系统_django
python·opencv·django
dme.1 小时前
Python爬虫selenium验证-中文识别点选+图片验证码案例
爬虫·python
东方-教育技术博主1 小时前
wps中zotero插件消失,解决每次都需要重新开问题
python
许苑向上1 小时前
Java八股文(下)
java·开发语言
菜鸟一枚在这1 小时前
深入解析设计模式之单例模式
开发语言·javascript·单例模式
独孤求败Ace1 小时前
第44天:Web开发-JavaEE应用&反射机制&类加载器&利用链&成员变量&构造方法&抽象方法
java·开发语言
计算机-秋大田1 小时前
基于Spring Boot的农产品智慧物流系统设计与实现(LW+源码+讲解)
java·开发语言·spring boot·后端·spring·课程设计