import pygame
import random
import sys

# ===================== 全局配置 =====================
WIDTH, HEIGHT = 800, 600  # 窗口尺寸
FPS = 60
# 颜色定义
WHITE = (255, 255, 255)
BLUE_DEEP = (10, 30, 80)
RED = (255, 50, 50)
YELLOW = (255, 220, 0)
GREEN = (60, 220, 80)

# 初始化pygame
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("乐乐捕鱼小游戏")
clock = pygame.time.Clock()
font = pygame.font.SysFont("simhei", 24)  # 中文黑体

# ===================== 鱼类类 =====================
class Fish:
    def __init__(self):
        # 随机位置、大小、速度、颜色、分数
        self.x = random.randint(0, WIDTH)
        self.y = random.randint(50, HEIGHT - 50)
        self.size = random.randint(20, 50)
        self.speed_x = random.randint(2, 5) * random.choice([-1, 1])
        self.speed_y = random.randint(-1, 1)
        # 三种鱼对应不同分数
        fish_type = random.choice(["red", "yellow", "green"])
        if fish_type == "red":
            self.color = RED
            self.score = 10
        elif fish_type == "yellow":
            self.color = YELLOW
            self.score = 20
        else:
            self.color = GREEN
            self.score = 5

    def update(self):
        # 移动
        self.x += self.speed_x
        self.y += self.speed_y
        # 碰到左右墙壁反向
        if self.x <= 0 or self.x >= WIDTH - self.size:
            self.speed_x *= -1
        # 上下小幅反弹
        if self.y <= 0 or self.y >= HEIGHT - self.size:
            self.speed_y *= -1

    def draw(self):
        # 画圆形小鱼
        pygame.draw.ellipse(screen, self.color, (self.x, self.y, self.size, self.size//2))
        # 鱼眼睛白点
        eye_x = self.x + self.size * 0.7
        eye_y = self.y + self.size * 0.25
        pygame.draw.circle(screen, WHITE, (int(eye_x), int(eye_y)), 3)

    def hit(self, net_x, net_y, net_radius):
        # 判断渔网是否命中鱼（距离碰撞检测）
        fish_center_x = self.x + self.size / 2
        fish_center_y = self.y + self.size / 4
        distance = ((fish_center_x - net_x)**2 + (fish_center_y - net_y)**2)**0.5
        return distance < net_radius

# ===================== 渔网类 =====================
class Net:
    def __init__(self):
        self.x = WIDTH // 2
        self.y = HEIGHT // 2
        self.radius = 40  # 渔网半径
        self.is_cast = False  # 是否撒网
        self.cast_time = 0
        self.cast_duration = 20  # 撒网持续帧数

    def follow_mouse(self, mx, my):
        if not self.is_cast:
            self.x = mx
            self.y = my

    def cast_net(self):
        # 点击撒网
        self.is_cast = True
        self.cast_time = self.cast_duration

    def update(self):
        if self.is_cast:
            self.cast_time -= 1
            # 撒网期间渔网变大再收缩
            scale = self.cast_time / self.cast_duration
            self.radius = int(40 + 30*(1-scale))
            if self.cast_time <= 0:
                self.is_cast = False
                self.radius = 40

    def draw(self):
        # 画空心渔网圆圈
        pygame.draw.circle(screen, WHITE, (self.x, self.y), self.radius, 2)

# ===================== 游戏主逻辑 =====================
def main():
    score = 0
    fish_list = [Fish() for _ in range(12)]  # 生成12条鱼
    fishing_net = Net()

    running = True
    while running:
        clock.tick(FPS)
        screen.fill(BLUE_DEEP)  # 深海背景

        # 事件监听
        mx, my = pygame.mouse.get_pos()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            # 鼠标左键点击撒网
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                if not fishing_net.is_cast:
                    fishing_net.cast_net()

        # 更新渔网
        fishing_net.follow_mouse(mx, my)
        fishing_net.update()

        # 更新所有鱼
        for fish in fish_list:
            fish.update()
            fish.draw()

        # 撒网时检测捕鱼碰撞
        if fishing_net.is_cast and fishing_net.cast_time == fishing_net.cast_duration - 1:
            new_fish = []
            for fish in fish_list:
                if fish.hit(fishing_net.x, fishing_net.y, fishing_net.radius):
                    score += fish.score  # 抓到加分
                else:
                    new_fish.append(fish)
            # 被捕走的鱼重新生成新鱼
            catch_count = len(fish_list) - len(new_fish)
            for _ in range(catch_count):
                new_fish.append(Fish())
            fish_list = new_fish

        # 绘制渔网
        fishing_net.draw()

        # 绘制分数文字
        score_text = font.render(f"当前得分：{score}", True, WHITE)
        screen.blit(score_text, (10, 10))

        pygame.display.flip()

    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()