酷代码 AI
菜单
服务商

帮我写一个python代码,要求 :黑色背景有月亮星星,有烟花不断在绽放

```python import pygame import random import math import sys # 初始化pygame pygame.init() # 设置窗口尺寸 WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("夜空烟花秀") # 颜色定义 BLACK = (0, 0, 0) WHITE = (255, 255, 255) YELLOW = (255, 255, 0) BLUE = (100, 100, 255) GRAY = (200, 200, 200) # 星星类 class Star: def __init__(self): self.x = random.randint(0, WIDTH) self.y = random.randint(0, HEIGHT) self.size = random.uniform(0.1, 1.5) self.brightness = random.randint(150, 255) def draw(self): color = (self.brightness, self.brightness, self.brightness) pygame.draw.circle(screen, color, (int(self.x), int(self.y)), int(self.size)) def twinkle(self): # 星星闪烁效果 if random.random() < 0.05: self.brightness = random.randint(150, 255) # 月亮类 class Moon: def __init__(self): self.x = WIDTH - 150 self.y = 100 self.radius = 50 self.phase = 0.8 # 月相(0-1) def draw(self): # 绘制月亮主体 pygame.draw.circle(screen, YELLOW, (self.x, self.y), self.radius) # 绘制月相阴影 shadow_radius = int(self.radius * (1 - self.phase)) shadow_x = self.x - int(self.radius * (1 - self.phase)) pygame.draw.circle(screen, BLACK, (shadow_x, self.y), shadow_radius) # 烟花粒子类 class FireworkParticle: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = random.uniform(1.5, 3.0) self.vx = random.uniform(-3, 3) self.vy = random.uniform(-3, 3) self.gravity = 0.1 self.life = 100 # 粒子寿命 self.decay = random.uniform(0.8, 1.2) # 衰减速度 def update(self): self.x += self.vx self.y += self.vy self.vy += self.gravity self.life -= self.decay self.radius *= 0.99 # 逐渐缩小 def draw(self): if self.life > 0: alpha = int(self.life * 2.55) # 转换为0-255范围 if alpha > 255: alpha = 255 color_with_alpha = (*self.color, alpha) # 创建临时surface绘制带透明度的粒子 particle_surface = pygame.Surface((int(self.radius * 2), int(self.radius * 2)), pygame.SRCALPHA) pygame.draw.circle(particle_surface, color_with_alpha, (int(self.radius), int(self.radius)), int(self.radius)) screen.blit(particle_surface, (int(self.x - self.radius), int(self.y - self.radius))) def is_dead(self): return self.life <= 0 or self.radius <= 0.1 # 烟花类 class Firework: def __init__(self): self.reset() self.color = ( random.randint(200, 255), random.randint(50, 255), random.randint(50, 255) ) def reset(self): self.x = random.randint(100, WIDTH - 100) self.y = HEIGHT self.speed = random.uniform(3, 6) self.target_y = random.randint(50, HEIGHT // 2) self.exploded = False self.particles = [] self.trail = [] # 尾迹效果 def update(self): if not self.exploded: # 更新尾迹 self.trail.append((self.x, self.y)) if len(self.trail) > 10: self.trail.pop(0) # 上升 self.y -= self.speed # 轻微的水平移动 self.x += random.uniform(-0.5, 0.5) # 检查是否到达爆炸点 if self.y <= self.target_y: self.explode() else: # 更新所有粒子 for particle in self.particles[:]: particle.update() if particle.is_dead(): self.particles.remove(particle) # 如果所有粒子都消失,重置烟花 if len(self.particles) == 0: self.reset() def explode(self): self.exploded = True # 创建爆炸粒子 particle_count = random.randint(80, 150) for _ in range(particle_count): self.particles.append(FireworkParticle(self.x, self.y, self.color)) def draw(self): if not self.exploded: # 绘制尾迹 for i, (trail_x, trail_y) in enumerate(self.trail): alpha = int(255 * (i / len(self.trail))) trail_color = (*self.color, alpha) trail_surface = pygame.Surface((4, 4), pygame.SRCALPHA) pygame.draw.circle(trail_surface, trail_color, (2, 2), 2) screen.blit(trail_surface, (int(trail_x - 2), int(trail_y - 2))) # 绘制烟花主体 pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 3) pygame.draw.circle(screen, WHITE, (int(self.x), int(self.y)), 1) else: # 绘制所有粒子 for particle in self.particles: particle.draw() # 创建星星 stars = [Star() for _ in range(200)] # 创建月亮 moon = Moon() # 创建烟花 fireworks = [Firework() for _ in range(5)] # 主循环 clock = pygame.time.Clock() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False # 填充黑色 screen.fill(BLACK) # 更新和绘制星星 for star in stars: star.twinkle() star.draw() # 绘制月亮 moon.draw() # 更新和绘制烟花 for firework in fireworks: firework.update() firework.draw() # 随机添加新烟花 if random.random() < 0.02 and len(fireworks) < 8: fireworks.append(Firework()) # 移除已经完全消失的烟花 fireworks = [fw for fw in fireworks if len(fw.particles) > 0 or not fw.exploded] # 显示提示文字 font = pygame.font.SysFont(None, 24) text = font.render("按ESC退出 | 夜空烟花秀", True, WHITE) screen.blit(text, (10, HEIGHT - 30)) pygame.display.flip() clock.tick(60) pygame.quit() sys.exit() ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 粒子系统的性能优化需要控制粒子数量,避免内存泄漏 透明效果处理要注意Surface的SRCALPHA标志设置 随机数生成要确保分布均匀,避免烟花效果不自然 [2026-01-08 11:20:13 | AI写代码神器 | 1824点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多