专栏导读

-
🌸 欢迎来到Python办公自动化专栏—Python处理办公问题,解放您的双手
-
-
-
-
📕 此外还有python基础专栏:请点击——>Python基础学习专栏求订阅
-
文章作者技术和水平有限,如果文中出现错误,希望大家能指正🙏
-
❤️ 欢迎各位佬关注! ❤️
背景介绍
-
大家好!今天我们要用Python的Pygame库来创建一个美丽的雪花飘飘效果。这个项目不仅能让我们学习基本的游戏编程概念,还能创造出漂亮的视觉效果,非常适合冬天氛围哦!
-
我们将创建一个窗口,里面有120片随机飘落的雪花,每片雪花都有自己独特的运动轨迹和透明度,营造出真实的雪花飘落效果。
库的安装
库 | 用途 | 安装 |
---|
pygame | 界面设计 | pip install pygame -i https://pypi.tuna.tsinghua.edu.cn/simple/ |
代码解析
1. 初始化设置
-
这里我们导入了必要的库,并设置了窗口大小为800x600像素,标题为"雪花飘飘 ❄️"。
import pygame
import random
import math
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("雪花飘飘 ❄️")
clock = pygame.time.Clock()
2. 雪花类
Snowflake类定义了每片雪花的属性:
-
初始位置(x,y) - 随机分布在屏幕上方
-
半径 - 2到5像素不等
-
下落速度 - 1到2.5不等
-
摆动幅度和角度 - 使雪花左右飘动
-
透明度 - 150到255之间
class Snowflake:
def __init__(self):
self.reset()
def reset(self):
self.x = random.uniform(0, WIDTH)
self.y = random.uniform(-HEIGHT, 0)
self.radius = random.uniform(2, 5)
self.speed_y = random.uniform(1, 2.5)
self.amplitude = random.uniform(10, 20)
self.angle = random.uniform(0, 2 * math.pi)
self.angle_speed = random.uniform(0.01, 0.03)
self.opacity = random.randint(150, 255)
update()方法控制雪花的运动:
-
垂直下落
-
水平方向轻微摆动(使用正弦函数)
-
当雪花落出屏幕底部时,重置到顶部
def update(self):
self.y += self.speed_y
self.angle += self.angle_speed
self.x += math.sin(self.angle) * 0.5
if self.y > HEIGHT:
self.reset()
draw()方法绘制半透明的雪花,使用SRCALPHA创建支持透明度的表面。
def draw(self, surface):
snow_color = (255, 255, 255)
snow_surface = pygame.Surface((10, 10), pygame.SRCALPHA)
pygame.draw.circle(snow_surface, (255, 255, 255, self.opacity), (5, 5), int(self.radius))
surface.blit(snow_surface, (int(self.x), int(self.y)))
3. 创建雪花和主循环
-
创建120片雪花
-
主循环不断更新和绘制所有雪花
-
背景设置为深蓝色
-
帧率控制在60FPS
snowflakes = [Snowflake() for _ in range(120)]
running = True
while running:
screen.fill((30, 30, 60))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for snow in snowflakes:
snow.update()
snow.draw(screen)
pygame.display.flip()
clock.tick(60)
完整代码
import pygame
import random
import math
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("雪花飘飘 ❄️")
clock = pygame.time.Clock()
class Snowflake:
def __init__(self):
self.reset()
def reset(self):
self.x = random.uniform(0, WIDTH)
self.y = random.uniform(-HEIGHT, 0)
self.radius = random.uniform(2, 5)
self.speed_y = random.uniform(1, 2.5)
self.amplitude = random.uniform(10, 20)
self.angle = random.uniform(0, 2 * math.pi)
self.angle_speed = random.uniform(0.01, 0.03)
self.opacity = random.randint(150, 255)
def update(self):
self.y += self.speed_y
self.angle += self.angle_speed
self.x += math.sin(self.angle) * 0.5
if self.y > HEIGHT:
self.reset()
def draw(self, surface):
snow_color = (255, 255, 255)
snow_surface = pygame.Surface((10, 10), pygame.SRCALPHA)
pygame.draw.circle(snow_surface, (255, 255, 255, self.opacity), (5, 5), int(self.radius))
surface.blit(snow_surface, (int(self.x), int(self.y)))
snowflakes = [Snowflake() for _ in range(120)]
running = True
while running:
screen.fill((30, 30, 60))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for snow in snowflakes:
snow.update()
snow.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
总结
-
希望对初学者有帮助
-
致力于办公自动化的小小程序员一枚
-
希望能得到大家的【一个免费关注】!感谢
-
求个 🤞 关注 🤞
-
-
求个 ❤️ 喜欢 ❤️
-
-
求个 👍 收藏 👍
-