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

2025年一段代码使用python完成过年放烟花写祝福

运行效果如下,也可以自己自定义要祝福的人,虽然做的不太好但是也可以先用用
 

import pygame
import random
import math

# 初始化Pygame
pygame.init()

# 设置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("天总,新年快乐!")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)

# 文字设置
font_path = "C:/Windows/Fonts/simhei.ttf"  # 替换为你的中文字体路径
font = pygame.font.Font(font_path, 74)
text = font.render("天总,新年快乐呦!", True, WHITE)
text_rect = text.get_rect(center=(width // 2, height // 2))

# 烟花粒子类
class Firework:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.particles = []
        self.exploded = False

    def add_particle(self):
        angle = random.uniform(0, 2 * math.pi)
        speed = random.uniform(5, 10)
        particle = {
            'x': self.x,
            'y': self.y,
            'vx': speed * math.cos(angle),
            'vy': -speed * math.sin(angle),
            'life': 50
        }
        self.particles.append(particle)

    def update(self):
        if not self.exploded:
            self.y -= 5
            if self.y < height // 2:
                self.exploded = True
                for _ in range(100):
                    self.add_particle()
        else:
            for particle in self.particles:
                particle['x'] += particle['vx']
                particle['y'] += particle['vy']
                particle['vy'] += 0.1  # 加速度
                particle['life'] -= 1
            self.particles = [p for p in self.particles if p['life'] > 0]

    def draw(self, screen):
        if not self.exploded:
            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 3)
        else:
            for particle in self.particles:
                pygame.draw.circle(screen, self.color, (int(particle['x']), int(particle['y'])), 2)

# 创建烟花列表
fireworks = []

# 主循环
running = True
show_text = True
text_duration = 3000  # 显示文字的时间(毫秒)
start_time = pygame.time.get_ticks()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    current_time = pygame.time.get_ticks()
    elapsed_time = current_time - start_time

    if show_text and elapsed_time >= text_duration:
        show_text = False

    screen.fill(BLACK)

    if show_text:
        screen.blit(text, text_rect)
    else:
        if random.randint(1, 20) == 1:
            fireworks.append(Firework(random.randint(100, width - 100), height, random.choice([RED, GREEN, BLUE, YELLOW])))

        for firework in fireworks:
            firework.update()
            firework.draw(screen)

        fireworks = [fw for fw in fireworks if not (fw.exploded and len(fw.particles) == 0)]

    pygame.display.flip()
    pygame.time.delay(30)

pygame.quit()


http://www.kler.cn/a/541039.html

相关文章:

  • 【动态规划篇】:动态规划解决路径难题--思路,技巧与实例
  • STM32 HAL库 ADC程序(C语言)
  • 数据源和 sqlSessionFactory 配置
  • PySide(PyQT)的 QGraphicsScene 中检测回车键
  • 【算法】【高精度】acwing算法基础 794. 高精度除法
  • STM32 CUBE Can调试
  • 【多模态大模型】系列2:如何用多GPU训练一个非常大的模型(数据/模型/流水线/张量并行、MoE、混合精度训练、压缩、激活重新计算)
  • elementplus 使用日期时间选择器,设置可选范围为前后大于2年且只能选择历史时间不能大于当前时间点
  • DeepSeek大模型的发展的十问十答
  • 蓄电池放电技术革新:引领能源存储新时代
  • Day60_补20250208_图论part5_并查集理论基础|寻找存在的路径
  • C++ 中的 cJSON 解析库:用法、实现及递归解析算法与内存高效管理
  • 基于fpga的数字频率计(论文+源码)
  • 鱼塘钓鱼(多路归并,贪心)
  • PDF密码忘了?三步找回超简单
  • 详解享元模式
  • 数据结构 顺序表及其实现
  • Oracle(OCP和OCM)
  • 微信小程序文件流转base64文件,wx.arrayBufferToBase64()方法已弃用
  • Linux网卡为什么没有对应的设备文件
  • 二叉树的遍历方式和子问题思路
  • MyBatis常见知识点
  • 一、通义灵码插件保姆级教学-IDEA(安装篇)
  • A Normalized Gaussian Wasserstein Distance for Tiny Object Detection(纯翻译)
  • 常见数据结构的C语言定义---《数据结构C语言版》
  • Pdf手册阅读(1)--数字签名篇