过了今晚就是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()
以下就是演示效果图啦
最后,祝大家元旦快乐!!!