import pygame
import math
import random

pygame.init()

# --- 核心配置 ---
WIDTH, HEIGHT = 1000, 700
FPS = 60

# 颜色定义
C_BG = (30, 30, 40)
C_BUILDING = (100, 100, 120)
C_RUBBLE = (60, 60, 70)
C_FLASH = (255, 255, 200)
C_FIREBALL = (255, 100, 0)
C_SHOCKWAVE = (200, 200, 255)

class Building:
    """建筑物类"""
    def __init__(self, x, y, w, h):
        self.rect = pygame.Rect(x, y, w, h)
        self.destroyed = False

    def draw(self, screen):
        color = C_RUBBLE if self.destroyed else C_BUILDING
        pygame.draw.rect(screen, color, self.rect)

class Explosion:
    """爆炸特效类"""
    def __init__(self, x, y, power):
        self.x = x
        self.y = y
        self.power = power  # 威力等级 (1-10)
        self.max_radius = power * 40
        self.current_radius = 0
        self.flash_timer = 10  # 强光闪烁帧数
        self.active = True

    def update(self, buildings):
        if not self.active:
            return

        # 冲击波扩散
        if self.current_radius < self.max_radius:
            self.current_radius += self.power * 1.5
        
        # 检测建筑物是否在爆炸范围内
        for b in buildings:
            if not b.destroyed:
                # 计算建筑物中心到爆炸中心的距离
                dist = math.sqrt((b.rect.centerx - self.x)**2 + (b.rect.centery - self.y)**2)
                if dist <= self.current_radius:
                    b.destroyed = True

        # 强光闪烁结束且冲击波达到最大后，判定爆炸结束
        if self.flash_timer <= 0 and self.current_radius >= self.max_radius:
            self.active = False

        if self.flash_timer > 0:
            self.flash_timer -= 1

    def draw(self, screen):
        if not self.active:
            return

        # 1. 强光闪烁
        if self.flash_timer > 0:
            overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
            overlay.fill((255, 255, 255, 200))
            screen.blit(overlay, (0, 0))

        # 2. 冲击波圆环
        pygame.draw.circle(screen, C_SHOCKWAVE, (self.x, self.y), int(self.current_radius), 4)
        
        # 3. 火球
        fireball_r = max(5, int(self.max_radius * 0.3))
        pygame.draw.circle(screen, C_FIREBALL, (self.x, self.y), fireball_r)

class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption("Pygame 核弹模拟器 | 左键选位置 | 右键加当量 | 空格引爆")
        self.clock = pygame.time.Clock()
        self.font = pygame.font.SysFont("microsoftyahei", 24)

        self.buildings = []
        self.explosions = []
        self.target_x = 0
        self.target_y = 0
        self.power = 3  # 初始当量

        self._generate_city()

    def _generate_city(self):
        """随机生成城市建筑群"""
        for _ in range(60):
            w = random.randint(30, 70)
            h = random.randint(40, 150)
            x = random.randint(0, WIDTH - w)
            y = HEIGHT - h
            self.buildings.append(Building(x, y, w, h))

    def run(self):
        while True:
            self.clock.tick(FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    return
                if event.type == pygame.MOUSEMOTION:
                    self.target_x, self.target_y = event.pos
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:  # 左键：选定爆炸点
                        self.target_x, self.target_y = event.pos
                    elif event.button == 3:  # 右键：增加当量
                        self.power = min(10, self.power + 1)
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:  # 空格：引爆
                        self.explosions.append(Explosion(self.target_x, self.target_y, self.power))

            # 更新
            for exp in self.explosions:
                exp.update(self.buildings)
            self.explosions = [e for e in self.explosions if e.active]

            # 渲染
            self.screen.fill(C_BG)
            for b in self.buildings:
                b.draw(self.screen)
            for exp in self.explosions:
                exp.draw(self.screen)

            # 绘制准星与UI
            pygame.draw.circle(self.screen, (255, 50, 50), (self.target_x, self.target_y), 10, 2)
            ui_text = self.font.render(f"当前当量: {self.power} | 左键选点 | 右键加当量 | 空格引爆", True, (255, 255, 255))
            self.screen.blit(ui_text, (10, 10))

            pygame.display.flip()

if __name__ == "__main__":
    game = Game()
    game.run()