酷代码 AI
菜单
服务商

帮我写一个python代码,要求:有玫红色玫瑰花绽放,背景是绚丽的七彩爱心烟花

```python import pygame import random import math import sys # 初始化pygame pygame.init() # 设置窗口尺寸 WIDTH, HEIGHT = 1000, 700 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("玫红玫瑰与七彩爱心烟花") # 颜色定义 BACKGROUND = (10, 5, 20) # 深紫色 ROSE_COLOR = (255, 20, 147) # 玫红色 ROSE_CENTER = (255, 105, 180) # 玫瑰中心更浅的玫红色 GREEN = (34, 139, 34) # 叶子绿色 # 烟花粒子类 class FireworkParticle: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = random.randint(2, 4) self.speed = random.uniform(2, 5) 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 # 粒子寿命 self.alpha = 255 # 透明度 def update(self): self.x += self.vx self.y += self.vy self.vy += self.gravity self.life -= 1 self.alpha = int(self.life * 2.55) # 随寿命减少透明度 if self.alpha < 0: self.alpha = 0 def draw(self, surface): if self.life > 0: # 创建临时surface实现透明度效果 temp_surface = pygame.Surface((self.radius * 2, self.radius * 2), pygame.SRCALPHA) pygame.draw.circle(temp_surface, (*self.color, self.alpha), (self.radius, self.radius), self.radius) surface.blit(temp_surface, (self.x - self.radius, self.y - self.radius)) # 爱心烟花类 class HeartFirework: def __init__(self): self.reset() self.particles = [] self.exploded = False def reset(self): self.x = random.randint(100, WIDTH - 100) self.y = HEIGHT + 20 self.speed = random.uniform(3, 6) self.color = random.choice([ (255, 0, 0), # 红 (255, 165, 0), # 橙 (255, 255, 0), # 黄 (0, 255, 0), # 绿 (0, 191, 255), # 蓝 (138, 43, 226), # 紫 (255, 20, 147) # 玫红 ]) self.target_y = random.randint(100, 300) self.exploded = False def update(self): if not self.exploded: self.y -= self.speed if self.y <= self.target_y: self.explode() # 更新所有粒子 for particle in self.particles[:]: particle.update() if particle.life <= 0: self.particles.remove(particle) def explode(self): self.exploded = True # 创建爱心形状的粒子 for i in range(100): t = i / 10.0 # 爱心参数方程 x = 16 * (math.sin(t) 3) y = 13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t) # 缩放和随机化 scale = random.uniform(2, 4) offset_x = random.uniform(-2, 2) offset_y = random.uniform(-2, 2) self.particles.append(FireworkParticle( self.x + x * scale + offset_x, self.y + y * scale + offset_y, self.color )) def draw(self, surface): if not self.exploded: # 绘制上升的烟花 pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), 4) # 绘制尾迹 for i in range(5): pos_y = self.y + i * 3 alpha = 255 - i * 50 if alpha > 0: temp_surface = pygame.Surface((8, 8), pygame.SRCALPHA) pygame.draw.circle(temp_surface, (*self.color, alpha), (4, 4), 2) surface.blit(temp_surface, (int(self.x) - 4, int(pos_y) - 4)) # 绘制所有粒子 for particle in self.particles: particle.draw(surface) def is_dead(self): return self.exploded and len(self.particles) == 0 # 绘制玫瑰花函数 def draw_rose(surface, center_x, center_y, size): # 绘制花蕊 pygame.draw.circle(surface, ROSE_CENTER, (center_x, center_y), int(size * 0.3)) # 绘制花瓣(多层) num_layers = 5 for layer in range(num_layers): layer_radius = size * (0.3 + layer * 0.15) num_petals = 8 + layer * 2 for i in range(num_petals): angle = 2 * math.pi * i / num_petals + layer * 0.3 petal_x = center_x + math.cos(angle) * layer_radius petal_y = center_y + math.sin(angle) * layer_radius # 花瓣大小随层数变化 petal_w = size * (0.4 - layer * 0.05) petal_h = size * (0.3 - layer * 0.04) # 绘制椭圆花瓣 petal_rect = pygame.Rect(0, 0, petal_w, petal_h) petal_rect.center = (petal_x, petal_y) # 创建临时surface实现渐变效果 temp_surface = pygame.Surface((int(petal_w), int(petal_h)), pygame.SRCALPHA) for y in range(int(petal_h)): alpha = int(255 * (1 - abs(y - petal_h/2) / (petal_h/2))) color = (*ROSE_COLOR, alpha) pygame.draw.line(temp_surface, color, (0, y), (int(petal_w), y)) # 旋转花瓣 rotated_petal = pygame.transform.rotate(temp_surface, math.degrees(angle)) rotated_rect = rotated_petal.get_rect(center=(petal_x, petal_y)) surface.blit(rotated_petal, rotated_rect) # 绘制花茎 stem_height = size * 2 stem_points = [ (center_x, center_y + size * 0.5), (center_x - size * 0.1, center_y + stem_height * 0.3), (center_x, center_y + stem_height * 0.6), (center_x + size * 0.1, center_y + stem_height * 0.9), (center_x, center_y + stem_height) ] pygame.draw.lines(surface, GREEN, False, stem_points, 3) # 绘制叶子 leaf_size = size * 0.8 for side in [-1, 1]: # 左右两侧 leaf_x = center_x + side * size * 0.3 leaf_y = center_y + stem_height * 0.7 # 叶子控制点 control_x = leaf_x + side * leaf_size * 0.5 control_y = leaf_y - leaf_size * 0.3 # 绘制贝塞尔曲线叶子(简化) leaf_points = [] for t in range(11): t_normalized = t / 10.0 # 二次贝塞尔曲线 x = (1-t_normalized)2 * leaf_x + 2*(1-t_normalized)*t_normalized * control_x + t_normalized2 * (leaf_x + side * leaf_size * 0.3) y = (1-t_normalized)2 * leaf_y + 2*(1-t_normalized)*t_normalized * control_y + t_normalized2 * (leaf_y + leaf_size * 0.5) leaf_points.append((x, y)) pygame.draw.lines(surface, GREEN, False, leaf_points, 2) # 主函数 def main(): clock = pygame.time.Clock() fireworks = [] last_firework_time = 0 firework_interval = 800 # 毫秒 # 创建玫瑰 rose_x = WIDTH // 2 rose_y = HEIGHT // 2 + 50 rose_size = 60 running = True while running: current_time = pygame.time.get_ticks() # 事件处理 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 # 生成新烟花 if current_time - last_firework_time > firework_interval: fireworks.append(HeartFirework()) last_firework_time = current_time # 随机化下一个烟花间隔 firework_interval = random.randint(500, 1500) # 更新烟花 for firework in fireworks[:]: firework.update() if firework.is_dead(): fireworks.remove(firework) # 绘制 screen.fill(BACKGROUND) # 绘制所有烟花 for firework in fireworks: firework.draw(screen) # 绘制玫瑰 draw_rose(screen, rose_x, rose_y, rose_size) # 绘制 font = pygame.font.SysFont(None, 48) title = font.render("Rose & Fireworks", True, (255, 255, 255)) screen.blit(title, (WIDTH//2 - title.get_width()//2, 20)) pygame.display.flip() clock.tick(60) pygame.quit() sys.exit() if __name__ == "__main__": main() ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 粒子系统的生命周期管理不当可能导致内存泄漏 透明效果处理需要考虑性能优化 参数方程绘制的图形需要仔细调试比例和位置 [2026-01-07 16:54:42 | AI写代码神器 | 2490点数解答]

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