import pygame
import math
import random
import time

pygame.init()
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("Advanced Bullet Tracking System")
clock = pygame.time.Clock()

# 颜色定义
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 120, 255)
YELLOW = (255, 255, 0)
PURPLE = (180, 0, 255)
ORANGE = (255, 165, 0)

class AdvancedBullet:
    def __init__(self, x, y, target_x, target_y, bullet_type="NORMAL"):
        self.x = x
        self.y = y
        self.start_x = x
        self.start_y = y
        self.target_x = target_x
        self.target_y = target_y
        self.speed = 8
        self.active = True
        self.trail = []
        self.max_trail_length = 20
        self.bullet_type = bullet_type
        self.creation_time = time.time()
        
        # 根据子弹类型设置属性
        if bullet_type == "HOMING":
            self.color = PURPLE
            self.radius = 8
            self.speed = 6
            self.homing_strength = 0.3
        elif bullet_type == "PREDICTIVE":
            self.color = ORANGE
            self.radius = 7
            self.speed = 7
            self.prediction_factor = 1.5
        elif bullet_type == "SNIPER":
            self.color = YELLOW
            self.radius = 6
            self.speed = 15
        else:  # NORMAL
            self.color = RED
            self.radius = 5
            self.speed = 8

    def update(self, target_x, target_y, target_speed_x=0, target_speed_y=0):
        if not self.active:
            return False

        # 添加轨迹点
        self.trail.append((self.x, self.y))
        if len(self.trail) > self.max_trail_length:
            self.trail.pop(0)

        # 根据子弹类型更新移动逻辑
        if self.bullet_type == "HOMING":
            self.homing_move(target_x, target_y)
        elif self.bullet_type == "PREDICTIVE":
            self.predictive_move(target_x, target_y, target_speed_x, target_speed_y)
        else:
            self.straight_move(target_x, target_y)

        # 检查是否命中目标
        distance = math.sqrt((self.x - target_x)**2 + (self.y - target_y)**2)
        if distance < 30:  # 命中范围
            self.active = False
            return True
        
        # 子弹生命周期（防止无限飞行）
        if time.time() - self.creation_time > 10:  # 10秒后消失
            self.active = False
            
        return False

    def straight_move(self, target_x, target_y):
        # 直线飞向目标
        dx = target_x - self.x
        dy = target_y - self.y
        distance = math.sqrt(dx*dx + dy*dy)
        
        if distance > 0:
            self.x += dx / distance * self.speed
            self.y += dy / distance * self.speed

    def homing_move(self, target_x, target_y):
        # 制导导弹式移动
        dx = target_x - self.x
        dy = target_y - self.y
        distance = math.sqrt(dx*dx + dy*dy)
        
        if distance > 0:
            # 基础移动
            self.x += dx / distance * self.speed
            self.y += dy / distance * self.speed
            
            # 制导修正
            if distance < 200:  # 近距离增强制导
                self.x += dx / distance * self.speed * self.homing_strength
                self.y += dy / distance * self.speed * self.homing_strength

    def predictive_move(self, target_x, target_y, target_speed_x, target_speed_y):
        # 预测目标移动位置
        distance = math.sqrt((target_x - self.x)**2 + (target_y - self.y)**2)
        time_to_target = distance / self.speed
        
        # 预测目标位置
        predicted_x = target_x + target_speed_x * time_to_target * self.prediction_factor
        predicted_y = target_y + target_speed_y * time_to_target * self.prediction_factor
        
        # 飞向预测位置
        dx = predicted_x - self.x
        dy = predicted_y - self.y
        distance_pred = math.sqrt(dx*dx + dy*dy)
        
        if distance_pred > 0:
            self.x += dx / distance_pred * self.speed
            self.y += dy / distance_pred * self.speed

    def draw(self, surface):
        if not self.active:
            return
            
        # 绘制轨迹
        for i, (trail_x, trail_y) in enumerate(self.trail):
            alpha = int(255 * (i / len(self.trail)))
            trail_color = (*self.color[:3], alpha)
            trail_radius = max(1, int(self.radius * (i / len(self.trail))))
            pygame.draw.circle(surface, trail_color, (int(trail_x), int(trail_y)), trail_radius)
        
        # 绘制子弹
        pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), self.radius)
        
        # 绘制子弹类型标识
        font = pygame.font.Font(None, 20)
        type_text = font.render(self.bullet_type, True, WHITE)
        surface.blit(type_text, (int(self.x) - 15, int(self.y) - 20))

class Target:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.radius = 25
        self.speed_x = 0
        self.speed_y = 0
        self.color = GREEN
        self.hit_count = 0
        self.last_hit_time = 0
        self.hit_effect = 0

    def update(self, keys):
        # 保存上一帧位置用于速度计算
        o
        
        
        
        辞酱辞酱辞酱辞酱辞酱辞酱