祝大家2024年天天开心,遇见心软的财神爷!!!

        过了今晚就是2024年啦!!!相信很多小伙伴现在已经和朋友在一起跨年了。首先祝你们新年快乐,万事如意!愿你们在2024年里勇往直前,收获成功和幸福,事业有成,身体健康,家庭美满!

        但是现在很多地方都禁止烟花燃放,跨年没有烟花总感觉缺少一点味道~为了实现在家和朋友看烟花的愿望~这篇文章给大家带来2024跨年烟花代码,让大家都能在家和朋友一起看自己编写的烟花,快快收藏起来吧!

import pygame
import random
import math

# 初始化pygame
pygame.init()

# 设置屏幕大小
screen = pygame.display.set_mode((800, 600))

# 设置标题
pygame.display.set_caption("跨年烟花 By-到点就睡觉")

# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# 加载背景音乐
pygame.mixer.music.load("凤凰传奇 - 好运来.mp3")
pygame.mixer.music.play(-1)  # -1表示循环播放

# 定义烟花类
class Firework:
    def __init__(self):
        self.x = random.randint(0, 800)
        self.y = 600
        self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        self.speed = random.randint(1, 5)
        self.angle = random.uniform(0, 2 * math.pi)
        self.life = 100

    def update(self):
        self.y -= self.speed
        self.life -= 1
        if self.life <= 0:
            self.explode()

    def draw(self):
        pygame.draw.circle(screen, self.color, (self.x, int(self.y)), 5)

    def explode(self):
        for i in range(10):
            particle = Particle(self.x, self.y, self.color)
            particles.append(particle)

# 定义粒子类
class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.speed = random.uniform(1, 3)
        self.angle = random.uniform(0, 2 * math.pi)
        self.life = 100

    def update(self):
        self.x += math.cos(self.angle) * self.speed
        self.y -= math.sin(self.angle) * self.speed
        self.life -= 1
        if self.life <= 0:
            return True
        return False

    def draw(self):
        pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 2)

# 创建烟花列表
fireworks = []

# 创建粒子列表
particles = []

# 游戏主循环
running = True
while running:
    screen.fill(BLACK)

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

    if random.random() < 0.01:
        fireworks.append(Firework())

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

    for particle in particles[:]:
        if particle.update():
            particles.remove(particle)
        else:
            particle.draw()
    font = pygame.font.Font(None, 36)
    text = font.render("Welcome To 2024", True, WHITE)
    screen.blit(text, (300, 250))

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

pygame.quit()

以下就是演示效果图啦

最后,祝大家元旦快乐!!!

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

到点就困告

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值