import pygame
import random
import sys

# 初始化pygame
pygame.init()
# 窗口设置
WIDTH, HEIGHT = 900, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("捕鱼达人小游戏")
clock = pygame.time.Clock()
FPS = 60

# 颜色定义
WHITE = (255, 255, 255)
BLUE = (10, 40, 120)
RED = (255, 60, 60)
YELLOW = (255, 220, 0)
GREEN = (60, 220, 60)

# ==========【修复字体部分，解决报错】==========
try:
    # 优先尝试黑体
    font = pygame.font.SysFont("SimHei", 32)
except:
    try:
        # 备选微软雅黑
        font = pygame.font.SysFont("msyh", 32)
    except:
        # 终极兜底：使用pygame内置默认字体（不依赖系统中文字体）
        font = pygame.font.Font(None, 32)
# ==========================================

# ---------------------- 游戏对象类 ----------------------
class Fish:
    def __init__(self):
        self.width = random.randint(40, 90)
        self.height = int(self.width * 0.6)
        # 从屏幕左右两侧随机出生
        if random.random() > 0.5:
            self.x = -self.width
            self.speed_x = random.uniform(1.2, 3.0)
        else:
            self.x = WIDTH
            self.speed_x = -random.uniform(1.2, 3.0)
        self.y = random.randint(80, HEIGHT - 120)
        self.color = random.choice([YELLOW, GREEN, WHITE])
        self.hp = random.randint(1, 3)  # 血量，血量越高分值越高

    def update(self):
        self.x += self.speed_x

    def draw(self):
        pygame.draw.ellipse(screen, self.color, (self.x, self.y, self.width, self.height))
        # 鱼眼睛
        eye_x = self.x + self.width * 0.8 if self.speed_x > 0 else self.x + self.width * 0.2
        pygame.draw.circle(screen, (0, 0, 0), (int(eye_x), int(self.y + self.height/2)), 4)

    def off_screen(self):
        return self.x < -100 or self.x > WIDTH + 100

class Bullet:
    def __init__(self, start_x, start_y, target_x, target_y):
        self.x = start_x
        self.y = start_y
        # 计算方向向量
        dx = target_x - start_x
        dy = target_y - start_y
        dist = (dx**2 + dy**2)**0.5
        speed = 8
        self.vx = dx / dist * speed
        self.vy = dy / dist * speed
        self.radius = 6

    def update(self):
        self.x += self.vx
        self.y += self.vy

    def draw(self):
        pygame.draw.circle(screen, YELLOW, (int(self.x), int(self.y)), self.radius)

    def out_range(self):
        return self.x < 0 or self.x > WIDTH or self.y < 0 or self.y > HEIGHT

# 炮台
cannon_x = WIDTH // 2
cannon_y = HEIGHT - 70
score = 0

fish_list = []
bullet_list = []
spawn_timer = 0

# 主游戏循环
running = True
while running:
    dt = clock.tick(FPS) / 1000.0
    screen.fill(BLUE)

    # 事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        # 鼠标点击发射炮弹
        if event.type == pygame.MOUSEBUTTONDOWN:
            mx, my = pygame.mouse.get_pos()
            bullet = Bullet(cannon_x, cannon_y - 20, mx, my)
            bullet_list.append(bullet)

    # 生成鱼
    spawn_timer += dt
    if spawn_timer > 1.2:
        fish_list.append(Fish())
        spawn_timer = 0

    # 更新鱼群
    for fish in fish_list[:]:
        fish.update()
        if fish.off_screen():
            fish_list.remove(fish)

    # 更新子弹
    for bullet in bullet_list[:]:
        bullet.update()
        if bullet.out_range():
            bullet_list.remove(bullet)

    # 碰撞检测：子弹 vs 鱼
    for bullet in bullet_list[:]:
        for fish in fish_list[:]:
            # 椭圆简易碰撞
            fx, fy, fw, fh = fish.x, fish.y, fish.width, fish.height
            cx, cy = bullet.x, bullet.y
            if fx < cx < fx + fw and fy < cy < fy + fh:
                fish.hp -= 1
                bullet_list.remove(bullet)
                if fish.hp <= 0:
                    score += fw // 10
                    fish_list.remove(fish)
                break

    # 绘制所有物体
    for fish in fish_list:
        fish.draw()
    for bullet in bullet_list:
        bullet.draw()

    # 绘制炮台
    pygame.draw.rect(screen, RED, (cannon_x - 35, cannon_y, 70, 35))
    pygame.draw.rect(screen, (180, 20, 20), (cannon_x - 8, cannon_y - 40, 16, 45))

    # 绘制分数
    score_text = font.render(f"得分：{score}", True, WHITE)
    screen.blit(score_text, (20, 15))

    pygame.display.flip()

pygame.quit()
sys.exit()