import pygameimport randomimport mathimport sys# 初始化pygamepygame.init()# 设置窗口width, height=800, 600screen= pygame.display.set_mode((width, height))pygame.display.set_caption("烟花秀")clock= pygame.time.Clock()# 颜色定义BLACK= (0, 0, 0)WHITE= (255, 255, 255)colors= [ (255, 50, 50), # 红色 (50, 255, 50), # 绿色 (50, 50, 255), # 蓝色 (255, 150, 50), # 橙色 (255, 255, 50), # 黄色 (255, 50, 255), # 紫色 (50, 255, 255), # 青色]# 烟花粒子类classParticle: def__init__(self, x, y, color): self.x=x self.y=y self.color=color self.radius=random.uniform(1.0, 3.0) self.speed=random.uniform(2, 6) self.angle=random.uniform(0, 2*math.pi) self.vx=math.cos(self.angle)*self.speed self.vy=math.sin(self.angle)*self.speed self.gravity=0.1 self.life=100 # 粒子生命周期 defupdate(self): self.x+=self.vx self.y+=self.vy self.vy+=self.gravity self.life-=1 defdraw(self, surface): ifself.life>0: alpha=min(255, self.life*3) # 创建带透明度的颜色 color_with_alpha= (self.color[0], self.color[1], self.color[2], alpha) # 绘制圆形粒子 pygame.draw.circle( surface, color_with_alpha[:3], # 只使用RGB分量 (int(self.x), int(self.y)), int(self.radius) ) defis_dead(self): returnself.life<=0# 烟花类classFirework: def__init__(self, width, height): self.x=random.randint(100, width-100) self.y=height self.color=random.choice(colors) self.speed=random.uniform(3, 6) self.particles= [] self.exploded=False self.target_y=random.randint(50, height//2) defupdate(self): ifnotself.exploded: self.y-=self.speed ifself.y<=self.target_y: self.explode() else: # 更新所有粒子 forparticleinself.particles[:]: # 使用切片创建副本 particle.update() ifparticle.is_dead(): self.particles.remove(particle) defexplode(self): self.exploded=True # 创建爆炸粒子 particle_count=random.randint(50, 150) for_inrange(particle_count): self.particles.append(Particle(self.x, self.y, self.color)) defdraw(self, surface): ifnotself.exploded: # 绘制未爆炸的烟花(上升轨迹) pygame.draw.circle( surface, self.color, (int(self.x), int(self.y)), 3 ) else: # 绘制所有粒子 forparticleinself.particles: particle.draw(surface)# 主程序defmain(): fireworks= [] font= pygame.font.SysFont(None, 24) last_firework_time= pygame.time.get_ticks() running=True whilerunning: current_time= pygame.time.get_ticks() # 事件处理 foreventin pygame.event.get(): ifevent.type== pygame.QUIT: running=False elifevent.type== pygame.KEYDOWN: ifevent.key== pygame.K_SPACE: # 按空格键手动添加烟花 fireworks.append(Firework(width, height)) elifevent.key== pygame.K_ESCAPE: running=False elifevent.type== pygame.MOUSEBUTTONDOWN: # 点击鼠标添加烟花 fireworks.append(Firework(width, height)) # 定时自动添加新烟花(每1-2秒一个) ifcurrent_time-last_firework_time>random.randint(1000, 2000): fireworks.append(Firework(width, height)) last_firework_time=current_time # 更新所有烟花 forfireworkinfireworks[:]: # 使用切片创建副本 firework.update() iffirework.explodedandlen(firework.particles)==0: fireworks.remove(firework) # 绘制 screen.fill(BLACK) # 绘制所有烟花 forfireworkinfireworks: firework.draw(screen) # 绘制提示信息 tips= [ "烟花秀", "点击鼠标或按空格键发射烟花", "ESC键退出", f"当前烟花数量: {len(fireworks)}" ] fori, tipinenumerate(tips): text=font.render(tip, True, WHITE) screen.blit(text, (10, 10+i*25)) # 绘制Happy new Year!2026! xhs_text=font.render("Happy new Year!2026!", True, (255, 100, 100)) screen.blit(xhs_text, (width-200, height-30)) pygame.display.flip() clock.tick(60) # 60 FPS pygame.quit() sys.exit()if __name__ =="__main__": main()