当前位置: 首页 > news >正文

Python生日祝福烟花

1. 实现效果

在这里插入图片描述

2. 素材加载

2个图片和3个音频
shoot(已去底).jpg

flower.jpg

shoot_image = pygame.image.load('shoot(已去底).jpg') # 加载拼接的发射图像
flower_image = pygame.image.load('flower.jpg')      # 加载拼接的烟花图 烟花不好去底
# 调整图像的像素为原图的1/2 因为图像相对于界面来说有些大
shoot_image = pygame.transform.scale(shoot_image, (shoot_image.get_size()[0]/2 ,shoot_image.get_size()[1]/2))
flower_image = pygame.transform.scale(flower_image, (flower_image.get_size()[0]/2 ,flower_image.get_size()[1]/2))
# 音频、音效
shoot_sound = pygame.mixer.Sound('shoot.mp3')
bomb_sound = pygame.mixer.Sound('bomb1.mp3')
bg_music = pygame.mixer.Sound('bg.mp3')

图像分块显示

# 每个部分的宽度
num_parts = 10  # 有10个shoot图
num_parts1 = 13 # 13个烟花
shoot_part_width = shoot_width // num_parts
flower_part_width = flower_width // num_parts1

在这里插入图片描述

3. 烟花发射

发射shoot图从底部往上,快到顶部变烟花flower,然后下落一段距离,最后消失。

在这里插入图片描述

4. 画面定格

处理鼠标点击事件以暂停和恢复动画
在这里插入图片描述

5. 完整代码

# 2024-12-01 Python简单的生日祝福烟花
import pygame # pip install pygame
import random
pygame.init()               # 初始化 pygame
WIDTH, HEIGHT = 750, 500    # 设置窗口尺寸
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("生日烟花祝福") # 窗口标题
#bg_image = pygame.image.load('bg.jpg')
shoot_image = pygame.image.load('shoot(已去底).jpg') # 加载拼接的发射图像
flower_image = pygame.image.load('flower.jpg')      # 加载拼接的烟花图 烟花不好去底
# 调整图像的像素为原图的1/2 因为图像相对于界面来说有些大
shoot_image = pygame.transform.scale(shoot_image, (shoot_image.get_size()[0]/2 ,shoot_image.get_size()[1]/2))
flower_image = pygame.transform.scale(flower_image, (flower_image.get_size()[0]/2 ,flower_image.get_size()[1]/2))
# 音频、音效
shoot_sound = pygame.mixer.Sound('shoot.mp3')
bomb_sound = pygame.mixer.Sound('bomb1.mp3')
bg_music = pygame.mixer.Sound('bg.mp3')
bg_music.play(-1) # 播放背景音乐
# 获取图像的宽度和高度
shoot_width, shoot_height = shoot_image.get_size()
flower_width, flower_height = flower_image.get_size()
# 每个部分的宽度
num_parts = 10  # 有10个shoot图
num_parts1 = 13 # 13个烟花
shoot_part_width = shoot_width // num_parts
flower_part_width = flower_width // num_parts1
class Firework: # 定义烟花类def __init__(self, x):self.x = x              # 使用提供的x坐标self.shoot_y = HEIGHT   # shoot初始y坐标在底部self.flower_y = 0       # flower初始位置上方self.shoot_active = Trueself.flower_active = Falseself.shoot_part_index = random.randint(0, num_parts - 1)    # 随机选择shoot的部分索引self.flower_part_index = random.randint(0, num_parts - 1)   # 随机选择flower的部分索引self.drop_height = 10                                       # 下落的高度(在达到顶部后下落一点)self.is_dropping = False                                    # 是否开始下落self.alpha = 255                                            # 透明度控制def shoot(self):if self.shoot_active:# 绘制随机选择的 shoot 图像部分screen.blit(shoot_image, (self.x, self.shoot_y),(self.shoot_part_index * shoot_part_width, 0, shoot_part_width, shoot_height))self.shoot_y -= 5  # 更新 shoot 的位置 向上移动if self.shoot_y >= shoot_height:shoot_sound.play() # 播放发射音效if self.shoot_y <= 20:  # 当 shoot 达到接近窗口顶部时,触发下落self.shoot_active = Falseself.flower_active = Trueself.flower_y = self.shoot_y + self.drop_height  # flower 下落一点self.is_dropping = True  # 标记为开始下落if self.flower_active:   # 绘制随机选择的 flower 图像部分bomb_sound.play()flower_surface = pygame.Surface((flower_part_width, flower_height), pygame.SRCALPHA)  # 创建带透明度的 surfaceflower_surface.blit(flower_image, (0, 0),(self.flower_part_index * flower_part_width, 0, flower_part_width, flower_height))flower_surface.set_alpha(self.alpha) # 设置透明度screen.blit(flower_surface, (self.x, self.flower_y))if self.is_dropping:self.flower_y += 1  # flower 向下移动if self.alpha > 0:self.alpha -= 5  # 逐步降低透明度else:self.flower_active = False  # 透明度为0时消失
clock = pygame.time.Clock()
fireworks = []
running = True
paused = False  # 初始化暂停状态为 False
font = pygame.font.Font(None, 74)   # 设置字体, 使用默认字体, 大小为74
text_color = (255, 192, 203)        # 文字颜色粉红色
birthday_text = font.render("Happy Birthday", True, text_color)        # 文本"生日快乐"
text_rect = birthday_text.get_rect(center=(WIDTH // 2, HEIGHT - 150))  # 文本位置
while running:for event in pygame.event.get():if event.type == pygame.QUIT:running = False# 处理鼠标点击事件以暂停和恢复动画if event.type == pygame.MOUSEBUTTONDOWN:paused = not paused  # 切换暂停状态if not paused:# 每隔一段时间生成新烟花if random.random() < 0.01:  # 随机生成烟花 and len(fireworks) < 10# 随机选择 X 坐标,避免与已有烟花重叠new_firework_x = random.randint(0, WIDTH - shoot_part_width)overlap = any(abs(new_firework_x - firework.x) < shoot_part_width for firework in fireworks)if not overlap:     # 如果没有重叠,添加新烟花fireworks.append(Firework(new_firework_x))screen.fill((0, 0, 0))  # 填充黑色背景 #screen.blit(bg_image, (0, 0))for firework in fireworks:firework.shoot()screen.blit(birthday_text, text_rect) # 绘制生日文本pygame.display.flip()clock.tick(60) # 控制更新画面的速度
pygame.quit()
http://www.lryc.cn/news/509973.html

相关文章:

  • Ubuntu环境 nginx.conf详解(二)
  • shardingsphere分库分表项目实践4-sql解析sql改写
  • mysql数据库中,一棵3层的B+树,假如数据节点大小是1k,那这棵B+可以存多少条记录(2100万的由来)
  • Git 操作全解:从基础命令到高级操作的实用指南
  • 华院计算参与项目再次被《新闻联播》报道
  • 从一次线上故障聊聊接口自动化测试
  • Element-ui的使用教程 基于HBuilder X
  • Chapter 03 复合数据类型-1
  • 【Python知识】Python面向对象编程知识
  • CSharp: Oracle Stored Procedure query table
  • “协同过滤技术实战”:网上书城系统的设计与实现
  • Dhatim FastExcel 读写 Excel 文件
  • YOLO11全解析:从原理到实战,全流程体验下一代目标检测
  • 深度学习领域的主要神经网络架构综述
  • 【Nginx系列】---Nginx配置tcp转发
  • Java抽象工厂+单例模式
  • 后端接口设计
  • Docker部署Sentinel
  • 真实环境下实车运行,新能源汽车锂离子电池数据集
  • 【求职面试】驾照的种类
  • centos权限大集合,覆盖多种权限类型,解惑权限后有“. + t s”问题!
  • AI Agent案例全解析:百度营销智能体(8/30)
  • hive常用函数有哪些
  • 【Python高级353】python实现多线程版本的TCP服务器
  • 分布式调度框架学习笔记
  • SpringCloudAlibaba技术栈-Nacos
  • Redis篇--常见问题篇4--大Key(Big Key,什么是大Key,影响及使用建议)
  • 谷歌浏览器 Chrome 提示:此扩展程序可能很快将不再受支持
  • WebRTC服务质量(10)- Pacer机制(02) RoundRobinPacketQueue
  • 数据库的数据被清除了,该如何恢复?