def update(self):
# 讓煙火上升並爆炸
if not self.exploded:
self.pos[1] += self.velocity
self.velocity += 0.2 # 重力加速度使速度變慢
if self.velocity >= 0:
self.exploded = True # 當到達頂點時爆炸
self.create_particles()
else:
for particle in self.particles:
particle.update() #更新粒子狀態
self.particles = [p for p in self.particles if p.lifetime > 0] #移除消失粒子
def create_particles(self):
# 隨機生成愛心形狀的粒子
for xi, yi in zip(x, y):
px = self.pos[0] + xi
py = self.pos[1] - yi
self.particles.append(FireworkParticle([px, py], self.color))
def draw(self, screen):
if not self.exploded:
pygame.draw.circle(screen, self.color, (int(self.pos[0]), int(self.pos[1])), 5)
else:
for particle in self.particles:
particle.draw(screen)
import pygame import numpy as np import random
pygame.init()
愛心公式
t = np.linspace(0, 2 * np.pi, 1000)
可以把30替換成不同數字調整愛心大小
x = 16 * np.sin(t) * 3 30 # 調整愛心 y = (13 np.cos(t) - 5 np.cos(2 t) - 2 np.cos(3 t) - np.cos(4 t)) * 30
煙火粒子
class FireworkParticle: def init(self, pos, color): self.pos = pos #位置 self.velocity = [random.uniform(-3, 3), random.uniform(-3, 3)] # 隨機速度 self.color = color self.lifetime = random.randint(30, 60) # 隨機粒子燃燒時間 self.size = random.randint(2, 5)
煙火效果
class Firework: def init(self): self.color = random.choice([(255, 0, 0), (255, 100, 100), (255, 200, 200), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255)]) # 顏色(隨機選擇) self.pos = [random.randint(200, 1000), 950] # 調整煙火的起始位置 self.velocity = random.uniform(-8, -15) # 上升速度 self.particles = [] # 儲存爆炸的煙火例子 self.exploded = False #是否爆炸
視窗大小
screen = pygame.display.set_mode((1200, 1200))
pygame.display.set_caption("Love Fireworks") clock = pygame.time.Clock()
running = True fireworks = [] text_alpha = 0 #文字透明度 alpha_increment = 1 # 文字每幀增加的透明度值 show_text = False #控制文字是否顯示 text_display_time = 180 #文字偵數時間 frames = 0 #跟蹤文字時長
while running: screen.fill((0, 0, 0)) #背景黑色 可更改
pygame.quit()