import pygame
import sys
import math
import random
import time
from datetime import datetime

# 初始化pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 1200, 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("🚀 星际火箭发射系统")

# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 50, 50)
GREEN = (50, 200, 50)
BLUE = (50, 100, 255)
YELLOW = (255, 255, 100)
ORANGE = (255, 150, 50)
PURPLE = (180, 70, 255)
CYAN = (0, 255, 255)
GRAY = (100, 100, 100)
DARK_GRAY = (50, 50, 50)
LIGHT_GRAY = (200, 200, 200)
SKY_BLUE = (135, 206, 235)
NIGHT_BLUE = (10, 20, 40)
STAR_YELLOW = (255, 255, 200)
DARK_BROWN = (100, 70, 30)
EARTH_BLUE = (30, 60, 120)
EARTH_GREEN = (40, 120, 40)
OCEAN_BLUE = (20, 40, 120)
MOUNTAIN_GRAY = (80, 100, 100)
FOREST_GREEN = (30, 100, 30)
DESERT_BROWN = (180, 150, 100)
SUNSET_ORANGE = (255, 100, 50)
DEEP_SPACE = (5, 5, 15)
NEBULA_PURPLE = (100, 50, 150, 50)
NEBULA_BLUE = (50, 100, 200, 50)
PLANET_COLORS = [
    (220, 100, 50),    # 火星红
    (200, 150, 50),    # 土星黄
    (100, 150, 220),   # 海王星蓝
    (180, 120, 200),   # 天王星紫
    (150, 200, 100),   # 未知行星绿
]

# 字体
title_font = pygame.font.SysFont("microsoftyahei", 42, bold=True)
font_large = pygame.font.SysFont("microsoftyahei", 30)
font_medium = pygame.font.SysFont("microsoftyahei", 26)
font_small = pygame.font.SysFont("microsoftyahei", 22)
font_tiny = pygame.font.SysFont("microsoftyahei", 18)
font_counter = pygame.font.SysFont("arial", 72, bold=True)

# 背景系统
class BackgroundSystem:
    def __init__(self):
        self.stars = []
        self.planets = []
        self.nebulas = []
        self.space_stations = []
        self.asteroids = []
        self.satellites = []
        
        # 初始化各种背景元素
        self.init_stars(300)
        self.init_planets(5)
        self.init_nebulas(3)
        self.init_space_stations(2)
        self.init_asteroids(20)
        self.init_satellites(8)
        
        self.time = 0
        self.warp_effect = 0
        self.warp_speed = 0
    
    def init_stars(self, count):
        for _ in range(count):
            star_type = random.choice(['normal', 'blue', 'red', 'binary'])
            if star_type == 'normal':
                color = STAR_YELLOW
                size = random.uniform(0.5, 2.5)
                twinkle_speed = random.uniform(0.5, 2.0)
            elif star_type == 'blue':
                color = (150, 200, 255)
                size = random.uniform(1.0, 3.0)
                twinkle_speed = random.uniform(0.8, 2.5)
            elif star_type == 'red':
                color = (255, 150, 150)
                size = random.uniform(1.5, 4.0)
                twinkle_speed = random.uniform(0.3, 1.5)
            else:  # binary
                color = (255, 200, 150)
                size = random.uniform(2.0, 4.0)
                twinkle_speed = random.uniform(1.0, 3.0)
            
            x = random.uniform(0, WIDTH)
            y = random.uniform(0, HEIGHT)
            z = random.uniform(0.1, 1.0)  # 深度
            speed = random.uniform(0.1, 0.5) * z
            brightness = random.uniform(0.3, 1.0)
            phase = random.uniform(0, 6.28)
            
            self.stars.append({
                'x': x, 'y': y, 'z': z,
                'size': size, 'color': color,
                'twinkle_speed': twinkle_speed,
                'brightness': brightness,
                'speed': speed,
                'phase': phase
            })
    
    def init_planets(self, count):
        for _ in range(count):
            planet_type = random.choice(['gas_giant', 'rocky', 'ice_giant', 'dwarf'])
            if planet_type == 'gas_giant':
                color = random.choice([(220, 180, 100), (200, 150, 100), (180, 200, 150)])
                size = random.uniform(40, 80)
                rings = random.random() > 0.5
            elif planet_type == 'rocky':
                color = random.choice([(150, 100, 50), (120, 100, 80), (180, 120, 60)])
                size = random.uniform(20, 50)
                rings = False
            elif planet_type == 'ice_giant':
                color = random.choice([(150, 200, 220), (100, 180, 200)])
                size = random.uniform(30, 60)
                rings = random.random() > 0.7
            else:  # dwarf
                color = (150, 150, 150)
                size = random.uniform(10, 30)
                rings = False
            
            orbit_radius = random.uniform(200, 600)
            orbit_speed = random.uniform(0.02, 0.08)
            orbit_angle = random.uniform(0, 6.28)
            
            self.planets.append({
                'type': planet_type,
                'color': color,
                'size': size,
                'rings': rings,
                'orbit_radius': orbit_radius,
                'orbit_speed': orbit_speed,
                'orbit_angle': orbit_angle,
                'ring_color': (200, 180, 150) if rings else None
            })
    
    def init_nebulas(self, count):
        for _ in range(count):
            nebula_type = random.choice(['spiral', 'cloud', 'remnant'])
            if nebula_type == 'spiral':
                color1 = NEBULA_PURPLE
                color2 = (150, 50, 100, 30)
                size = random.uniform(300, 500)
            elif nebula_type == 'cloud':
                color1 = NEBULA_BLUE
                color2 = (50, 150, 200, 30)
                size = random.uniform(200, 400)
            else:  # remnant
                color1 = (255, 100, 50, 40)
                color2 = (255, 150, 100, 20)
                size = random.uniform(150, 300)
            
            x = random.uniform(-size/2, WIDTH + size/2)
            y = random.uniform(-size/2, HEIGHT + size/2)
            speed_x = random.uniform(-0.2, 0.2)
            speed_y = random.uniform(-0.2, 0.2)
            
            self.nebulas.append({
                'type': nebula_type,
                'x': x, 'y': y,
                'size': size,
                'color1': color1,
                'color2': color2,
                'speed_x': speed_x,
                'speed_y': speed_y,
                'rotation': random.uniform(0, 6.28)
            })
    
    def init_space_stations(self, count):
        for _ in range(count):
            station_type = random.choice(['wheel', 'modular', 'ring'])
            if station_type == 'wheel':
                size = random.uniform(60, 120)
                color = (180, 180, 200)
            elif station_type == 'modular':
                size = random.uniform(40, 80)
                color = (200, 200, 180)
            else:  # ring
                size = random.uniform(80, 150)
                color = (180, 200, 220)
            
            x = random.uniform(100, WIDTH-100)
            y = random.uniform(100, HEIGHT-100)
            speed_x = random.uniform(-0.5, 0.5)
            speed_y = random.uniform(-0.5, 0.5)
            
            self.space_stations.append({
                'type': station_type,
                'x': x, 'y': y,
                'size': size,
                'color': color,
                'speed_x': speed_x,
                'speed_y': speed_y,
                'rotation': random.uniform(0, 6.28)
            })
    
    def init_asteroids(self, count):
        for _ in range(count):
            x = random.uniform(0, WIDTH)
            y = random.uniform(0, HEIGHT)
            size = random.uniform(5, 20)
            speed_x = random.uniform(-1.5, 1.5)
            speed_y = random.uniform(-1.5, 1.5)
            rotation = random.uniform(0, 6.28)
            rotation_speed = random.uniform(-0.05, 0.05)
            color = (150, 120, 100)
            
            self.asteroids.append({
                'x': x, 'y': y,
                'size': size,
                'speed_x': speed_x,
                'speed_y': speed_y,
                'rotation': rotation,
                'rotation_speed': rotation_speed,
                'color': color
            })
    
    def init_satellites(self, count):
        for _ in range(count):
            satellite_type = random.choice(['comms', 'weather', 'research', 'military'])
            if satellite_type == 'comms':
                color = (200, 200, 200)
                size = 15
            elif satellite_type == 'weather':
                color = (150, 200, 255)
                size = 12
            elif satellite_type == 'research':
                color = (200, 150, 255)
                size = 18
            else:  # military
                color = (255, 150, 150)
                size = 10
            
            x = random.uniform(100, WIDTH-100)
            y = random.uniform(100, HEIGHT-100)
            orbit_speed = random.uniform(0.5, 2.0)
            orbit_radius = random.uniform(30, 80)
            orbit_center_x = x
            orbit_center_y = y
            
            self.satellites.append({
                'type': satellite_type,
                'x': x, 'y': y,
                'color': color,
                'size': size,
                'orbit_speed': orbit_speed,
                'orbit_radius': orbit_radius,
                'orbit_center_x': orbit_center_x,
                'orbit_center_y': orbit_center_y,
                'orbit_angle': random.uniform(0, 6.28)
            })
    
    def update(self, dt, rocket_speed=0):
        self.time += dt
        
        # 更新星星 - 随火箭速度移动
        for star in self.stars:
            star['x'] -= star['speed'] * rocket_speed * 0.01
            star['phase'] += dt * star['twinkle_speed']
            
            if star['x'] < -50:
                star['x'] = WIDTH + 50
            elif star['x'] > WIDTH + 50:
                star['x'] = -50
            if star['y'] < -50:
                star['y'] = HEIGHT + 50
            elif star['y'] > HEIGHT + 50:
                star['y'] = -50
        
        # 更新行星轨道
        for planet in self.planets:
            planet['orbit_angle'] += dt * planet['orbit_speed']
            planet['orbit_radius'] -= dt * rocket_speed * 0.001
            if planet['orbit_radius'] < 50:
                planet['orbit_radius'] = 600
        
        # 更新星云
        for nebula in self.nebulas:
            nebula['x'] += nebula['speed_x'] * dt * 20
            nebula['y'] += nebula['speed_y'] * dt * 20
            nebula['rotation'] += dt * 0.01
            
            if nebula['x'] < -nebula['size']:
                nebula['x'] = WIDTH + nebula['size']
            elif nebula['x'] > WIDTH + nebula['size']:
                nebula['x'] = -nebula['size']
            if nebula['y'] < -nebula['size']:
                nebula['y'] = HEIGHT + nebula['size']
            elif nebula['y'] > HEIGHT + nebula['size']:
                nebula['y'] = -nebula['size']
        
        # 更新空间站
        for station in self.space_stations:
            station['x'] += station['speed_x'] * dt * 10
            station['y'] += station['speed_y'] * dt * 10
            station['rotation'] += dt * 0.02
            
            if station['x'] < -100:
                station['x'] = WIDTH + 100
            elif station['x'] > WIDTH + 100:
                station['x'] = -100
            if station['y'] < -100:
                station['y'] = HEIGHT + 100
            elif station['y'] > HEIGHT + 100:
                station['y'] = -100
        
        # 更新小行星
        for asteroid in self.asteroids:
            asteroid['x'] += asteroid['speed_x'] * dt * 20
            asteroid['y'] += asteroid['speed_y'] * dt * 20
            asteroid['rotation'] += asteroid['rotation_speed']
            
            if asteroid['x'] < -50:
                asteroid['x'] = WIDTH + 50
            elif asteroid['x'] > WIDTH + 50:
                asteroid['x'] = -50
            if asteroid['y'] < -50:
                asteroid['y'] = HEIGHT + 50
            elif asteroid['y'] > HEIGHT + 50:
                asteroid['y'] = -50
        
        # 更新卫星轨道
        for satellite in self.satellites:
            satellite['orbit_angle'] += dt * satellite['orbit_speed']
            satellite['x'] = satellite['orbit_center_x'] + math.cos(satellite['orbit_angle']) * satellite['orbit_radius']
            satellite['y'] = satellite['orbit_center_y'] + math.sin(satellite['orbit_angle']) * satellite['orbit_radius']
        
        # 更新曲速效果
        if rocket_speed > 5000:
            self.warp_speed = min(1.0, (rocket_speed - 5000) / 10000)
        else:
            self.warp_speed = max(0, self.warp_speed - dt)
        
        self.warp_effect = (self.warp_effect + dt * 20) % 100
    
    def draw(self, surface, rocket_altitude):
        # 根据高度决定背景类型
        if rocket_altitude < 10000:
            # 地球附近 - 显示地球和大气
            self.draw_earth_view(surface, rocket_altitude)
        elif rocket_altitude < 100000:
            # 太空 - 显示星星和行星
            self.draw_space_view(surface, rocket_altitude)
        else:
            # 深空 - 显示星云和遥远星系
            self.draw_deep_space_view(surface, rocket_altitude)
        
        # 曲速效果
        if self.warp_speed > 0:
            self.draw_warp_effect(surface)
    
    def draw_earth_view(self, surface, altitude):
        # 深空背景
        surface.fill(DEEP_SPACE)
        
        # 绘制星星
        for star in self.stars:
            twinkle = (math.sin(star['phase']) + 1) * 0.5
            brightness = star['brightness'] * (0.5 + 0.5 * twinkle)
            alpha = int(100 + 155 * (altitude / 10000))
            
            star_surface = pygame.Surface((int(star['size']*4), int(star['size']*4)), pygame.SRCALPHA)
            star_color = (*star['color'][:3], int(alpha * brightness))
            pygame.draw.circle(star_surface, star_color, 
                             (int(star['size']*2), int(star['size']*2)), int(star['size']))
            surface.blit(star_surface, (int(star['x'] - star['size']*2), int(star['y'] - star['size']*2)))
        
        # 绘制地球
        if altitude < 50000:
            earth_size = 200 * (1 - altitude / 50000)
            if earth_size > 10:
                earth_x = WIDTH // 2
                earth_y = HEIGHT - 100
                
                # 地球大气层
                for i in range(3, 0, -1):
                    atmos_size = earth_size + i * 20
                    atmos_surface = pygame.Surface((int(atmos_size*2), int(atmos_size*2)), pygame.SRCALPHA)
                    atmos_color = (100, 150, 255, 20 - i*5)
                    pygame.draw.circle(atmos_surface, atmos_color, 
                                     (int(atmos_size), int(atmos_size)), int(atmos_size))
                    surface.blit(atmos_surface, (earth_x - atmos_size, earth_y - atmos_size))
                
                # 地球本体
                pygame.draw.circle(surface, EARTH_BLUE, (earth_x, earth_y), int(earth_size))
                
                # 陆地
                for _ in range(3):
                    land_x = earth_x + random.randint(-int(earth_size*0.8), int(earth_size*0.8))
                    land_y = earth_y + random.randint(-int(earth_size*0.8), int(earth_size*0.8))
                    land_size = random.randint(int(earth_size*0.2), int(earth_size*0.4))
                    pygame.draw.circle(surface, EARTH_GREEN, (land_x, land_y), land_size)
                
                # 云层
                for _ in range(5):
                    cloud_x = earth_x + random.randint(-int(earth_size*0.6), int(earth_size*0.6))
                    cloud_y = earth_y + random.randint(-int(earth_size*0.6), int(earth_size*0.6))
                    cloud_size = random.randint(int(earth_size*0.1), int(earth_size*0.2))
                    pygame.draw.circle(surface, (255, 255, 255, 150), (cloud_x, cloud_y), cloud_size)
        
        # 绘制卫星
        for satellite in self.satellites:
            if altitude > 10000:  # 在足够高时显示卫星
                satellite_surface = pygame.Surface((satellite['size']*2, satellite['size']*2), pygame.SRCALPHA)
                pygame.draw.circle(satellite_surface, (*satellite['color'], 200), 
                                 (satellite['size'], satellite['size']), satellite['size'])
                
                # 太阳能板
                pygame.draw.rect(satellite_surface, (200, 200, 100, 200),
                               (satellite['size']-2, satellite['size']-10, 4, 20))
                pygame.draw.rect(satellite_surface, (200, 200, 100, 200),
                               (satellite['size']-10, satellite['size']-2, 20, 4))
                
                surface.blit(satellite_surface, 
                           (int(satellite['x'] - satellite['size']), 
                            int(satellite['y'] - satellite['size'])))
    
    def draw_space_view(self, surface, altitude):
        # 深空背景
        surface.fill(DEEP_SPACE)
        
        # 绘制星云
        for nebula in self.nebulas:
            nebula_surface = pygame.Surface((int(nebula['size']*2), int(nebula['size']*2)), pygame.SRCALPHA)
            
            if nebula['type'] == 'spiral':
                # 螺旋星云
                for i in range(5, 0, -1):
                    radius = nebula['size'] * (i/5)
                    alpha = 30 - i*5
                    spiral_surface = pygame.Surface((int(radius*2), int(radius*2)), pygame.SRCALPHA)
                    for a in range(0, 360, 10):
                        angle = math.radians(a)
                        x = radius + math.cos(angle + nebula['rotation']) * radius * 0.7
                        y = radius + math.sin(angle + nebula['rotation']) * radius * 0.7
                        point_radius = radius * 0.1
                        color = (*nebula['color1'][:3], alpha)
                        pygame.draw.circle(spiral_surface, color, (int(x), int(y)), int(point_radius))
                    nebula_surface.blit(spiral_surface, (int(nebula['size']-radius), int(nebula['size']-radius)))
            else:
                # 云状星云
                for _ in range(5):
                    x = nebula['size'] + random.uniform(-nebula['size']*0.8, nebula['size']*0.8)
                    y = nebula['size'] + random.uniform(-nebula['size']*0.8, nebula['size']*0.8)
                    cloud_size = random.uniform(nebula['size']*0.1, nebula['size']*0.3)
                    color = (*nebula['color1'][:3], 30)
                    pygame.draw.circle(nebula_surface, color, (int(x), int(y)), int(cloud_size))
            
            surface.blit(nebula_surface, 
                        (int(nebula['x'] - nebula['size']), 
                         int(nebula['y'] - nebula['size'])))
        
        # 绘制星星
        for star in self.stars:
            twinkle = (math.sin(star['phase']) + 1) * 0.5
            brightness = star['brightness'] * (0.5 + 0.5 * twinkle)
            
            # 根据深度调整大小和亮度
            star_size = star['size'] * (0.5 + 0.5 * star['z'])
            star_brightness = brightness * star['z']
            
            star_surface = pygame.Surface((int(star_size*4), int(star_size*4)), pygame.SRCALPHA)
            star_color = (*star['color'][:3], int(255 * star_brightness))
            pygame.draw.circle(star_surface, star_color, 
                             (int(star_size*2), int(star_size*2)), int(star_size))
            surface.blit(star_surface, (int(star['x'] - star_size*2), int(star['y'] - star_size*2)))
            
            # 为亮星添加光晕
            if star_brightness > 0.7:
                for i in range(2, 0, -1):
                    halo_size = star_size + i * 3
                    halo_surface = pygame.Surface((int(halo_size*2), int(halo_size*2)), pygame.SRCALPHA)
                    halo_color = (*star['color'][:3], 50 - i*20)
                    pygame.draw.circle(halo_surface, halo_color, 
                                     (int(halo_size), int(halo_size)), int(halo_size))
                    surface.blit(halo_surface, 
                               (int(star['x'] - halo_size), 
                                int(star['y'] - halo_size)))
        
        # 绘制行星
        for planet in self.planets:
            if altitude > 30000:  # 在足够高时显示行星
                planet_x = WIDTH // 2 + math.cos(planet['orbit_angle']) * planet['orbit_radius']
                planet_y = HEIGHT // 2 + math.sin(planet['orbit_angle']) * planet['orbit_radius']
                
                # 行星光环
                if planet['rings'] and planet['ring_color']:
                    for i in range(3, 0, -1):
                        ring_size = planet['size'] + i * 20
                        ring_surface = pygame.Surface((int(ring_size*2), int(ring_size*2)), pygame.SRCALPHA)
                        ring_color = (*planet['ring_color'], 100 - i*30)
                        pygame.draw.circle(ring_surface, ring_color, 
                                         (int(ring_size), int(ring_size)), int(ring_size), 3)
                        surface.blit(ring_surface, 
                                   (int(planet_x - ring_size), 
                                    int(planet_y - ring_size)))
                
                # 行星本体
                pygame.draw.circle(surface, planet['color'], 
                                 (int(planet_x), int(planet_y)), int(planet['size']))
                
                # 行星云带
                if planet['type'] == 'gas_giant':
                    for _ in range(3):
                        band_y = random.uniform(-planet['size']*0.8, planet['size']*0.8)
                        band_width = random.uniform(planet['size']*0.1, planet['size']*0.3)
                        pygame.draw.rect(surface, (planet['color'][0]-20, planet['color'][1]-20, planet['color'][2]-20),
                                       (planet_x - planet['size'], planet_y + band_y - band_width/2,
                                        planet['size']*2, band_width))
        
        # 绘制小行星
        for asteroid in self.asteroids:
            if altitude > 20000:  # 在足够高时显示小行星
                asteroid_surface = pygame.Surface((int(asteroid['size']*2), int(asteroid['size']*2)), pygame.SRCALPHA)
                
                # 绘制不规则的小行星形状
                points = []
                for a in range(0, 360, 60):
                    angle = math.radians(a)
                    radius = asteroid['size'] * random.uniform(0.7, 1.3)
                    x = asteroid['size'] + math.cos(angle + asteroid['rotation']) * radius
                    y = asteroid['size'] + math.sin(angle + asteroid['rotation']) * radius
                    points.append((x, y))
                
                pygame.draw.polygon(asteroid_surface, asteroid['color'], points)
                surface.blit(asteroid_surface, 
                           (int(asteroid['x'] - asteroid['size']), 
                            int(asteroid['y'] - asteroid['size'])))
    
    def draw_deep_space_view(self, surface, altitude):
        # 更深的太空背景
        surface.fill((0, 0, 5))
        
        # 绘制远处的星系
        for _ in range(3):
            galaxy_x = random.randint(0, WIDTH)
            galaxy_y = random.randint(0, HEIGHT)
            galaxy_size = random.randint(20, 50)
            
            galaxy_surface = pygame.Surface((galaxy_size*2, galaxy_size*2), pygame.SRCALPHA)
            for _ in range(20):
                star_x = galaxy_size + random.uniform(-galaxy_size*0.8, galaxy_size*0.8)
                star_y = galaxy_size + random.uniform(-galaxy_size*0.8, galaxy_size*0.8)
                star_size = random.uniform(0.5, 1.5)
                pygame.draw.circle(galaxy_surface, (255, 255, 255, 100), 
                                 (int(star_x), int(star_y)), int(star_size))
            surface.blit(galaxy_surface, (galaxy_x - galaxy_size, galaxy_y - galaxy_size))
        
        # 绘制星云
        for nebula in self.nebulas:
            nebula_surface = pygame.Surface((int(nebula['size']*2), int(nebula['size']*2)), pygame.SRCALPHA)
            
            # 超新星遗迹效果
            if nebula['type'] == 'remnant':
                for i in range(10, 0, -1):
                    radius = nebula['size'] * (i/10)
                    alpha = 40 - i*3
                    remnant_surface = pygame.Surface((int(radius*2), int(radius*2)), pygame.SRCALPHA)
                    for _ in range(50):
                        angle = random.uniform(0, 6.28)
                        distance = random.uniform(0, radius)
                        x = radius + math.cos(angle) * distance
                        y = radius + math.sin(angle) * distance
                        particle_size = random.uniform(1, 3)
                        color = (*nebula['color1'][:3], alpha)
                        pygame.draw.circle(remnant_surface, color, (int(x), int(y)), int(particle_size))
                    nebula_surface.blit(remnant_surface, (int(nebula['size']-radius), int(nebula['size']-radius)))
            
            surface.blit(nebula_surface, 
                        (int(nebula['x'] - nebula['size']), 
                         int(nebula['y'] - nebula['size'])))
        
        # 绘制空间站
        for station in self.space_stations:
            if altitude > 150000:  # 在深空显示空间站
                station_surface = pygame.Surface((int(station['size']*2), int(station['size']*2)), pygame.SRCALPHA)
                
                if station['type'] == 'wheel':
                    # 轮状空间站
                    pygame.draw.circle(station_surface, station['color'], 
                                     (int(station['size']), int(station['size'])), int(station['size']), 5)
                    
                    # 辐条
                    for a in range(0, 360, 45):
                        angle = math.radians(a + station['rotation']*180/math.pi)
                        end_x = station['size'] + math.cos(angle) * station['size']
                        end_y = station['size'] + math.sin(angle) * station['size']
                        pygame.draw.line(station_surface, station['color'],
                                       (station['size'], station['size']), (end_x, end_y), 3)
                    
                    # 中心舱
                    pygame.draw.circle(station_surface, (200, 200, 220), 
                                     (int(station['size']), int(station['size'])), int(station['size']*0.3))
                
                elif station['type'] == 'modular':
                    # 模块化空间站
                    modules = 6
                    for i in range(modules):
                        angle = 2 * math.pi * i / modules + station['rotation']
                        module_x = station['size'] + math.cos(angle) * station['size'] * 0.7
                        module_y = station['size'] + math.sin(angle) * station['size'] * 0.7
                        pygame.draw.rect(station_surface, station['color'],
                                       (module_x - 10, module_y - 10, 20, 20))
                        # 连接通道
                        pygame.draw.line(station_surface, (150, 150, 180),
                                       (station['size'], station['size']), (module_x, module_y), 2)
                
                else:  # ring
                    # 环状空间站
                    pygame.draw.circle(station_surface, station['color'], 
                                     (int(station['size']), int(station['size'])), int(station['size']), 8)
                    
                    # 内部结构
                    inner_radius = station['size'] * 0.7
                    for _ in range(8):
                        angle = random.uniform(0, 6.28)
                        x = station['size'] + math.cos(angle) * inner_radius
                        y = station['size'] + math.sin(angle) * inner_radius
                        pygame.draw.circle(station_surface, (200, 200, 220), (int(x), int(y)), 5)
                
                surface.blit(station_surface, 
                           (int(station['x'] - station['size']), 
                            int(station['y'] - station['size'])))
        
        # 绘制高速星星
        speed_factor = altitude / 200000
        for star in self.stars:
            if random.random() < 0.3:  # 只显示部分星星作为高速星星
                # 高速星星有拖尾
                length = star['size'] * 10 * speed_factor
                end_x = star['x'] - length
                end_y = star['y']
                
                # 绘制拖尾
                for i in range(3, 0, -1):
                    tail_length = length * (i/3)
                    tail_surface = pygame.Surface((int(tail_length), int(star['size']*4)), pygame.SRCALPHA)
                    points = [
                        (tail_length, star['size']*2),
                        (0, star['size']*2 - star['size']*i),
                        (0, star['size']*2 + star['size']*i)
                    ]
                    color = (*star['color'][:3], 100 - i*30)
                    pygame.draw.polygon(tail_surface, color, points)
                    surface.blit(tail_surface, (int(end_x), int(star['y'] - star['size']*2)))
                
                # 星星本体
                star_surface = pygame.Surface((int(star['size']*4), int(star['size']*4)), pygame.SRCALPHA)
                star_color = (*star['color'][:3], 255)
                pygame.draw.circle(star_surface, star_color, 
                                 (int(star['size']*2), int(star['size']*2)), int(star['size']))
                surface.blit(star_surface, (int(star['x'] - star['size']*2), int(star['y'] - star['size']*2)))
    
    def draw_warp_effect(self, surface):
        """绘制曲速效果"""
        warp_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
        
        # 中心点
        center_x, center_y = WIDTH // 2, HEIGHT // 2
        
        # 绘制多个同心环
        for i in range(10, 0, -1):
            radius = 50 + i * 20 + self.warp_effect
            ring_width = 3
            
            # 环的颜色和透明度
            hue = (self.warp_effect + i * 20) % 360
            r = int(255 * abs(math.sin(math.radians(hue))))
            g = int(255 * abs(math.sin(math.radians(hue + 120))))
            b = int(255 * abs(math.sin(math.radians(hue + 240))))
            alpha = int(100 * self.warp_speed)
            
            ring_color = (r, g, b, alpha)
            
            # 绘制环
            pygame.draw.circle(warp_surface, ring_color, 
                             (center_x, center_y), int(radius), ring_width)
            
            # 绘制环上的亮点
            for a in range(0, 360, 30):
                angle = math.radians(a + self.warp_effect * 5)
                point_x = center_x + math.cos(angle) * radius
                point_y = center_y + math.sin(angle) * radius
                pygame.draw.circle(warp_surface, (255, 255, 255, alpha), 
                                 (int(point_x), int(point_y)), 3)
        
        # 绘制中心亮点
        center_glow = pygame.Surface((200, 200), pygame.SRCALPHA)
        for i in range(5, 0, -1):
            glow_size = 20 + i * 10
            glow_color = (255, 255, 255, 50 - i*8)
            pygame.draw.circle(center_glow, glow_color, (100, 100), int(glow_size))
        warp_surface.blit(center_glow, (center_x - 100, center_y - 100))
        
        # 绘制光线
        for a in range(0, 360, 45):
            angle = math.radians(a + self.warp_effect * 10)
            length = 200 + math.sin(self.warp_effect * 0.5) * 50
            end_x = center_x + math.cos(angle) * length
            end_y = center_y + math.sin(angle) * length
            
            for i in range(3, 0, -1):
                ray_length = length * (i/3)
                ray_surface = pygame.Surface((int(ray_length), 6), pygame.SRCALPHA)
                points = [
                    (0, 3),
                    (ray_length, 3 - i),
                    (ray_length, 3 + i)
                ]
                ray_color = (200, 200, 255, 100 - i*30)
                pygame.draw.polygon(ray_surface, ray_color, points)
                
                # 旋转并放置光线
                rotated_ray = pygame.transform.rotate(ray_surface, math.degrees(angle))
                warp_surface.blit(rotated_ray, 
                                (center_x - rotated_ray.get_width()//2, 
                                 center_y - rotated_ray.get_height()//2))
        
        surface.blit(warp_surface, (0, 0))

# 粒子类
class Particle:
    def __init__(self, x, y, vx, vy, color, size, lifetime):
        self.x = x
        self.y = y
        self.vx = vx
        self.vy = vy
        self.color = color
        self.size = size
        self.lifetime = lifetime
        self.max_lifetime = lifetime
    
    def update(self, dt):
        self.x += self.vx * dt * 60
        self.y += self.vy * dt * 60
        self.lifetime -= dt
        self.size *= 0.98
    
    def draw(self, surface):
        alpha = int(255 * (self.lifetime / self.max_lifetime))
        if alpha > 0:
            particle_surface = pygame.Surface((int(self.size*2), int(self.size*2)), pygame.SRCALPHA)
            color_with_alpha = (*self.color[:3], alpha)
            pygame.draw.circle(particle_surface, color_with_alpha, 
                             (int(self.size), int(self.size)), int(self.size))
            surface.blit(particle_surface, (self.x - self.size, self.y - self.size))
    
    def is_alive(self):
        return self.lifetime > 0

# 爆炸效果类
class Explosion:
    def __init__(self, x, y, power=1.0):
        self.x = x
        self.y = y
        self.particles = []
        self.create_explosion(power)
    
    def create_explosion(self, power):
        colors = [(255, 255, 100), (255, 150, 0), (255, 50, 0)]
        for _ in range(int(30 * power)):
            angle = random.uniform(0, 2 * math.pi)
            speed = random.uniform(2, 8) * power
            vx = math.cos(angle) * speed
            vy = math.sin(angle) * speed
            color = random.choice(colors)
            size = random.uniform(3, 8) * power
            lifetime = random.uniform(0.5, 1.5)
            self.particles.append(Particle(self.x, self.y, vx, vy, color, size, lifetime))
    
    def update(self, dt):
        for particle in self.particles[:]:
            particle.update(dt)
            if not particle.is_alive():
                self.particles.remove(particle)
    
    def draw(self, surface):
        for particle in self.particles:
            particle.draw(surface)
    
    def is_finished(self):
        return len(self.particles) == 0

# 火箭类
class Rocket:
    def __init__(self):
        self.reset()
    
    def reset(self):
        self.fuel = 100.0
        self.oxygen = 100.0
        self.temperature = 20.0
        self.pressure = 1.0
        self.velocity = 0.0
        self.altitude = 0.0
        self.engine_power = 0.0
        self.launched = False
        self.stage = 1
        self.countdown = 10.0
        self.counting = False
        self.launch_time = None
        self.max_altitude = 0.0
        self.max_speed = 0.0
        self.mission_success = False
        self.abort_mode = False
        self.launch_aborted = False
        
        # 动画状态
        self.launch_phase = 0
        self.launch_timer = 0
        self.shake_intensity = 0
        self.shake_offset = (0, 0)
        self.flash_alpha = 0
        
        # 粒子系统
        self.flame_particles = []
        self.smoke_particles = []
        self.explosions = []
    
    def update(self, dt):
        # 更新爆炸效果
        for explosion in self.explosions[:]:
            explosion.update(dt)
            if explosion.is_finished():
                self.explosions.remove(explosion)
        
        # 倒计时
        if self.counting and not self.launched:
            self.countdown -= dt
            if self.countdown <= 0:
                self.launch()
        
        # 火箭飞行模拟
        if self.launched and not self.abort_mode:
            self.launch_timer += dt
            
            # 燃料消耗
            fuel_consumption = self.engine_power * 0.5
            self.fuel = max(0, self.fuel - fuel_consumption * dt)
            
            # 氧气消耗
            oxygen_consumption = self.engine_power * 0.3
            self.oxygen = max(0, self.oxygen - oxygen_consumption * dt)
            
            # 温度变化
            temp_change = self.engine_power * 10 - 2
            self.temperature += temp_change * dt
            self.temperature = min(300, self.temperature)
            
            # 压力变化
            if self.altitude < 10000:
                self.pressure = 1.0 - (self.altitude / 10000) * 0.9
            else:
                self.pressure = 0.1
            
            # 物理模拟
            thrust = self.engine_power * 1000
            gravity = 9.81
            drag = 0.01 * self.velocity**2
            
            acceleration = (thrust - gravity - drag) / 100
            self.velocity += acceleration * dt
            self.altitude += self.velocity * dt
            
            # 更新最大值
            self.max_altitude = max(self.max_altitude, self.altitude)
            self.max_speed = max(self.max_speed, self.velocity)
            
            # 分级分离
            if self.stage == 1 and self.altitude > 5000 and self.fuel < 30:
                self.stage = 2
                self.fuel = 80.0
                self.oxygen = 80.0
                self.create_stage_separation_effect()
                self.launch_phase = 2
            
            # 进入轨道判定
            if self.altitude > 100000 and self.velocity > 7800:
                self.mission_success = True
                self.launch_phase = 3
            
            # 发动机关闭
            if self.fuel <= 0 or self.oxygen <= 0:
                self.engine_power = 0
            
            # 火焰粒子效果
            if self.engine_power > 0 and self.launch_phase > 0:
                self.create_flame_particles()
                self.create_smoke_particles()
            
            # 屏幕震动效果
            if self.engine_power > 0.5:
                self.shake_intensity = min(10, self.shake_intensity + dt * 5)
            else:
                self.shake_intensity = max(0, self.shake_intensity - dt * 10)
            
            self.shake_offset = (
                random.uniform(-self.shake_intensity, self.shake_intensity),
                random.uniform(-self.shake_intensity, self.shake_intensity)
            )
            
            # 发射阶段控制
            if self.launch_phase == 0 and self.launched:
                self.launch_phase = 1
                self.flash_alpha = 255
        
        # 更新粒子
        for particle in self.flame_particles[:]:
            particle.update(dt)
            if not particle.is_alive():
                self.flame_particles.remove(particle)
        
        for particle in self.smoke_particles[:]:
            particle.update(dt)
            if not particle.is_alive():
                self.smoke_particles.remove(particle)
        
        # 闪光效果衰减
        self.flash_alpha = max(0, self.flash_alpha - dt * 200)
    
    def create_flame_particles(self):
        """创建火焰粒子"""
        for _ in range(int(self.engine_power * 8)):
            angle = random.uniform(-0.2, 0.2)
            speed = random.uniform(3, 6) * self.engine_power
            vx = math.sin(angle) * speed
            vy = math.cos(angle) * speed
            
            # 火焰颜色渐变
            color_phase = random.uniform(0, 1)
            if color_phase < 0.3:
                color = (255, 255, 200)
            elif color_phase < 0.6:
                color = (255, 200, 50)
            else:
                color = (255, 100, 0)
            
            size = random.uniform(2, 5) * self.engine_power
            lifetime = random.uniform(0.3, 0.7)
            
            particle_x = WIDTH//2 + random.randint(-10, 10)
            particle_y = HEIGHT - 100 - self.altitude * 0.001 + random.randint(-5, 5)
            self.flame_particles.append(Particle(particle_x, particle_y, vx, vy, color, size, lifetime))
    
    def create_smoke_particles(self):
        """创建烟雾粒子"""
        for _ in range(int(self.engine_power * 3)):
            vx = random.uniform(-0.3, 0.3)
            vy = random.uniform(1, 2)
            gray = random.randint(100, 180)
            color = (gray, gray, gray)
            size = random.uniform(4, 8)
            lifetime = random.uniform(1, 2)
            
            particle_x = WIDTH//2 + random.randint(-15, 15)
            particle_y = HEIGHT - 100 - self.altitude * 0.001 + random.randint(-10, 10)
            self.smoke_particles.append(Particle(particle_x, particle_y, vx, vy, color, size, lifetime))
    
    def create_stage_separation_effect(self):
        """创建分级分离特效"""
        explosion = Explosion(WIDTH//2, HEIGHT - 100 - self.altitude * 0.001, 0.7)
        self.explosions.append(explosion)
        self.flash_alpha = 200
    
    def launch(self):
        if not self.launched:
            self.launched = True
            self.engine_power = 1.0
            self.launch_phase = 0
            self.launch_time = time.time()
            self.flash_alpha = 255
    
    def increase_power(self):
        if self.launched and self.fuel > 0 and self.oxygen > 0:
            self.engine_power = min(1.5, self.engine_power + 0.1)
    
    def decrease_power(self):
        if self.launched:
            self.engine_power = max(0, self.engine_power - 0.1)
    
    def abort_mission(self):
        if self.launched and not self.abort_mode:
            self.abort_mode = True
            self.engine_power = 0
            self.launch_aborted = True
            explosion = Explosion(WIDTH//2, HEIGHT - 100 - self.altitude * 0.001, 1.2)
            self.explosions.append(explosion)
    
    def start_countdown(self):
        if not self.launched:
            self.counting = True
    
    def draw_rocket(self, surface):
        """绘制火箭"""
        rocket_width = 30
        rocket_x = WIDTH//2
        
        if self.altitude < 1000:
            rocket_y = HEIGHT - 100 - self.altitude * 0.001
        else:
            rocket_y = HEIGHT - 100 - self.altitude * 0.001
        
        scale = 1.0 / (1.0 + self.altitude / 200000)
        scaled_width = int(rocket_width * scale)
        scaled_height = int(200 * scale)
        
        rocket_body = [
            (rocket_x, rocket_y - scaled_height),
            (rocket_x - scaled_width, rocket_y - scaled_height//2),
            (rocket_x - scaled_width//2, rocket_y),
            (rocket_x + scaled_width//2, rocket_y),
            (rocket_x + scaled_width, rocket_y - scaled_height//2)
        ]
        
        temp_factor = min(1.0, self.temperature / 150)
        rocket_color = (
            int(200 + 55 * temp_factor),
            int(200 - 100 * temp_factor),
            int(200 - 200 * temp_factor)
        )
        
        pygame.draw.polygon(surface, rocket_color, rocket_body)
        
        # 火箭头部
        pygame.draw.polygon(surface, RED, [
            (rocket_x, rocket_y - scaled_height),
            (rocket_x - scaled_width//2, rocket_y - scaled_height + scaled_height//4),
            (rocket_x + scaled_width//2, rocket_y - scaled_height + scaled_height//4)
        ])
        
        # 火箭窗口
        pygame.draw.circle(surface, LIGHT_GRAY, 
                          (rocket_x, int(rocket_y - scaled_height//2)), 
                          int(10 * scale))
        
        # 分级标识
        if self.stage == 1:
            stage_color = ORANGE
        else:
            stage_color = GREEN
        
        pygame.draw.rect(surface, stage_color, 
                        (rocket_x - 20 * scale, 
                         rocket_y - scaled_height//3, 
                         40 * scale, 
                         8 * scale))
        
        # 徽标
        logo_text = font_tiny.render("817", True, WHITE)
        surface.blit(logo_text, 
                    (rocket_x - logo_text.get_width()//2, 
                     rocket_y - scaled_height//1.5))
    
    def draw_flame(self, surface):
        """绘制火焰"""
        if self.engine_power > 0 and self.launch_phase > 0:
            rocket_x = WIDTH//2
            
            if self.altitude < 1000:
                rocket_y = HEIGHT - 100 - self.altitude * 0.001
            else:
                rocket_y = HEIGHT - 100 - self.altitude * 0.001
            
            scale = 1.0 / (1.0 + self.altitude / 200000)
            
            flame_height = 50 + self.engine_power * 30
            flame_width = 25 + self.engine_power * 15
            
            for i in range(3):
                layer_height = flame_height * (1 - i * 0.2) * scale
                layer_width = flame_width * (1 - i * 0.2) * scale
                
                flame_points = [
                    (rocket_x - 15 * scale, rocket_y),
                    (rocket_x - layer_width//2, rocket_y + layer_height),
                    (rocket_x, rocket_y + layer_height + 20 * (i+1) * scale),
                    (rocket_x + layer_width//2, rocket_y + layer_height),
                    (rocket_x + 15 * scale, rocket_y)
                ]
                
                if i == 0:
                    flame_color = (255, 255, 200)
                elif i == 1:
                    flame_color = (255, 200, 50)
                else:
                    flame_color = (255, 100, 0)
                
                pygame.draw.polygon(surface, flame_color, flame_points)

# 按钮类
class Button:
    def __init__(self, x, y, width, height, text, color, hover_color, icon=None):
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.color = color
        self.hover_color = hover_color
        self.current_color = color
        self.hovered = False
        self.icon = icon
    
    def draw(self, surface):
        pygame.draw.rect(surface, self.current_color, self.rect, border_radius=8)
        pygame.draw.rect(surface, WHITE, self.rect, 2, border_radius=8)
        
        if self.icon:
            icon_surf = font_medium.render(self.icon, True, WHITE)
            icon_rect = icon_surf.get_rect(center=(self.rect.centerx, self.rect.centery - 10))
            surface.blit(icon_surf, icon_rect)
            
            text_surf = font_small.render(self.text, True, WHITE)
            text_rect = text_surf.get_rect(center=(self.rect.centerx, self.rect.centery + 15))
            surface.blit(text_surf, text_rect)
        else:
            text_surf = font_medium.render(self.text, True, WHITE)
            text_rect = text_surf.get_rect(center=self.rect.center)
            surface.blit(text_surf, text_rect)
    
    def check_hover(self, pos):
        self.hovered = self.rect.collidepoint(pos)
        self.current_color = self.hover_color if self.hovered else self.color
        return self.hovered
    
    def is_clicked(self, pos, event):
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            return self.rect.collidepoint(pos)
        return False

# 仪表盘类
class Gauge:
    def __init__(self, x, y, label, unit, min_val, max_val):
        self.x = x
        self.y = y
        self.label = label
        self.unit = unit
        self.min_val = min_val
        self.max_val = max_val
    
    def draw(self, surface, value, color=None):
        pygame.draw.rect(surface, (30, 40, 50), (self.x, self.y, 200, 100), border_radius=10)
        pygame.draw.rect(surface, BLUE, (self.x, self.y, 200, 100), 2, border_radius=10)
        
        label_surf = font_small.render(self.label, True, WHITE)
        surface.blit(label_surf, (self.x + 10, self.y + 10))
        
        if color is None:
            ratio = (value - self.min_val) / (self.max_val - self.min_val)
            if ratio < 0.2:
                color = RED
            elif ratio < 0.5:
                color = ORANGE
            else:
                color = GREEN
        
        if self.unit == "":
            value_text = f"{value:.0f}"
        else:
            value_text = f"{value:.1f} {self.unit}"
        
        value_surf = font_medium.render(value_text, True, color)
        surface.blit(value_surf, (self.x + 100 - value_surf.get_width()//2, self.y + 40))
        
        bar_width = 180
        bar_height = 15
        bar_x = self.x + 10
        bar_y = self.y + 75
        
        pygame.draw.rect(surface, (20, 30, 40), (bar_x, bar_y, bar_width, bar_height), border_radius=3)
        
        fill_width = (value - self.min_val) / (self.max_val - self.min_val) * bar_width
        fill_width = max(0, min(fill_width, bar_width))
        
        if fill_width > 0:
            pygame.draw.rect(surface, color, (bar_x, bar_y, fill_width, bar_height), border_radius=3)
        
        pygame.draw.rect(surface, (100, 150, 200), (bar_x, bar_y, bar_width, bar_height), 1, border_radius=3)

# 绘制动画文本
def draw_animation_text(surface, rocket):
    if rocket.counting and not rocket.launched and rocket.countdown > 0:
        countdown_text = f"{rocket.countdown:.1f}"
        if rocket.countdown <= 3:
            color = RED
        elif rocket.countdown <= 6:
            color = ORANGE
        else:
            color = GREEN
        
        scale = 1.0 + (1.0 - (rocket.countdown % 1.0)) * 0.5
        countdown_surf = pygame.font.SysFont("arial", 72, bold=True).render(countdown_text, True, color)
        countdown_rect = countdown_surf.get_rect(center=(WIDTH//2, HEIGHT//2))
        surface.blit(countdown_surf, countdown_rect)
        
        label_surf = font_medium.render("发射倒计时", True, WHITE)
        label_rect = label_surf.get_rect(center=(WIDTH//2, HEIGHT//2 + 50))
        surface.blit(label_surf, label_rect)
    
    elif rocket.launch_phase == 1:
        text_surf = font_large.render("点火！", True, ORANGE)
        text_rect = text_surf.get_rect(center=(WIDTH//2, HEIGHT//2))
        surface.blit(text_surf, text_rect)
    
    elif rocket.launch_phase == 2:
        text_surf = font_large.render("第1级分离！", True, YELLOW)
        text_rect = text_surf.get_rect(center=(WIDTH//2, HEIGHT//2))
        surface.blit(text_surf, text_rect)
    
    elif rocket.launch_phase == 3:
        text_surf = font_large.render("成功入轨！", True, GREEN)
        text_rect = text_surf.get_rect(center=(WIDTH//2, HEIGHT//2))
        surface.blit(text_surf, text_rect)
    
    elif rocket.launch_aborted:
        text_surf = font_large.render("任务中止！", True, RED)
        text_rect = text_surf.get_rect(center=(WIDTH//2, HEIGHT//2))
        surface.blit(text_surf, text_rect)

# 创建游戏对象
rocket = Rocket()
background = BackgroundSystem()

# 创建按钮
buttons = [
    Button(50, 650, 180, 60, "启动倒计时", GREEN, (0, 180, 0), "⏰"),
    Button(250, 650, 180, 60, "增大推力", ORANGE, (200, 120, 0), "⬆"),
    Button(450, 650, 180, 60, "减小推力", RED, (180, 0, 0), "⬇"),
    Button(650, 650, 180, 60, "紧急中止", (150, 0, 0), (200, 0, 0), "🚨"),
    Button(850, 650, 180, 60, "重置系统", BLUE, (60, 140, 200), "🔄"),
    Button(1050, 650, 120, 60, "日夜", DARK_GRAY, (100, 100, 150), "🌙")
]

# 创建仪表盘
gauges = [
    Gauge(50, 120, "燃料", "%", 0, 100),
    Gauge(280, 120, "氧气", "%", 0, 100),
    Gauge(510, 120, "温度", "°C", 0, 300),
    Gauge(740, 120, "压力", "atm", 0, 1),
    Gauge(50, 250, "速度", "m/s", 0, 10000),
    Gauge(280, 250, "高度", "km", 0, 200),
    Gauge(510, 250, "发动机功率", "%", 0, 150),
    Gauge(740, 250, "飞行阶段", "", 1, 2)
]

# 主循环
clock = pygame.time.Clock()
running = True

while running:
    dt = clock.tick(60) / 1000.0
    mouse_pos = pygame.mouse.get_pos()
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
        for button in buttons:
            if button.is_clicked(mouse_pos, event):
                if button.text == "启动倒计时":
                    rocket.start_countdown()
                elif button.text == "增大推力":
                    rocket.increase_power()
                elif button.text == "减小推力":
                    rocket.decrease_power()
                elif button.text == "紧急中止":
                    rocket.abort_mission()
                elif button.text == "重置系统":
                    rocket.reset()
                elif button.text == "日夜":
                    pass
        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and not rocket.launched:
                rocket.start_countdown()
            elif event.key == pygame.K_UP and rocket.launched:
                rocket.increase_power()
            elif event.key == pygame.K_DOWN and rocket.launched:
                rocket.decrease_power()
            elif event.key == pygame.K_a and rocket.launched:
                rocket.abort_mission()
            elif event.key == pygame.K_r:
                rocket.reset()
    
    # 更新所有对象
    rocket.update(dt)
    background.update(dt, rocket.velocity)
    
    for button in buttons:
        button.check_hover(mouse_pos)
    
    # 创建渲染表面
    screen_surface = pygame.Surface((WIDTH, HEIGHT))
    
    # 绘制背景
    background.draw(screen_surface, rocket.altitude)
    
    # 绘制粒子效果
    for particle in rocket.flame_particles:
        particle.draw(screen_surface)
    
    for particle in rocket.smoke_particles:
        particle.draw(screen_surface)
    
    for explosion in rocket.explosions:
        explosion.draw(screen_surface)
    
    # 绘制火箭
    rocket.draw_rocket(screen_surface)
    rocket.draw_flame(screen_surface)
    
    # 绘制UI背景
    pygame.draw.rect(screen_surface, (20, 30, 50, 200), (0, 0, WIDTH, 80))
    pygame.draw.line(screen_surface, CYAN, (0, 80), (WIDTH, 80), 2)
    
    # 绘制标题
    title = title_font.render("🚀 星际火箭发射系统", True, YELLOW)
    screen_surface.blit(title, (WIDTH//2 - title.get_width()//2, 15))
    
    subtitle = font_small.render("实验中学 817班 奚悠然", True, LIGHT_GRAY)
    screen_surface.blit(subtitle, (WIDTH//2 - subtitle.get_width()//2, 55))
    
    # 状态信息
    if rocket.launch_aborted:
        status_text = "任务中止"
        status_color = RED
    elif rocket.mission_success:
        status_text = "任务成功 - 轨道运行中"
        status_color = GREEN
    elif rocket.launched:
        status_text = f"飞行中 - 第{rocket.stage}级"
        status_color = YELLOW
    elif rocket.counting:
        status_text = f"发射准备 - 倒计时: {rocket.countdown:.1f}秒"
        status_color = ORANGE
    else:
        status_text = "准备发射"
        status_color = GREEN
    
    status_surf = font_large.render(f"状态: {status_text}", True, status_color)
    screen_surface.blit(status_surf, (20, HEIGHT-210))
    
    if rocket.launched:
        flight_time = time.time() - rocket.launch_time if rocket.launch_time else 0
        time_text = f"飞行时间: {flight_time:.1f}秒"
    else:
        time_text = "等待发射指令"
    
    time_surf = font_medium.render(time_text, True, LIGHT_GRAY)
    screen_surface.blit(time_surf, (20, HEIGHT-170))
    
    altitude_text = f"高度: {rocket.altitude:,.0f}米"
    velocity_text = f"速度: {rocket.velocity:,.0f}米/秒"
    
    altitude_surf = font_medium.render(altitude_text, True, WHITE)
    velocity_surf = font_medium.render(velocity_text, True, WHITE)
    
    screen_surface.blit(altitude_surf, (WIDTH - altitude_surf.get_width() - 20, HEIGHT-210))
    screen_surface.blit(velocity_surf, (WIDTH - velocity_surf.get_width() - 20, HEIGHT-170))
    
    controls = "空格:启动倒计时  ↑:增大推力  ↓:减小推力  A:紧急中止  R:重置"
    controls_surf = font_tiny.render(controls, True, LIGHT_GRAY)
    screen_surface.blit(controls_surf, (WIDTH//2 - controls_surf.get_width()//2, HEIGHT-140))
    
    # 绘制仪表盘
    gauge_values = [
        rocket.fuel,
        rocket.oxygen,
        rocket.temperature,
        rocket.pressure,
        rocket.velocity,
        rocket.altitude / 1000,
        rocket.engine_power * 100,
        rocket.stage
    ]
    
    for i, gauge in enumerate(gauges):
        gauge.draw(screen_surface, gauge_values[i])
    
    for button in buttons:
        button.draw(screen_surface)
    
    draw_animation_text(screen_surface, rocket)
    
    if rocket.flash_alpha > 0:
        flash_surf = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
        flash_surf.fill((255, 255, 255, int(rocket.flash_alpha)))
        screen_surface.blit(flash_surf, (0, 0))
    
    if rocket.shake_intensity > 0:
        shake_x, shake_y = rocket.shake_offset
        screen.blit(screen_surface, (shake_x, shake_y))
    else:
        screen.blit(screen_surface, (0, 0))
    
    pygame.display.flip()

pygame.quit()
sys.exit()