import pygame
import random
import sys

# 初始化
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("像素捕鱼达人")
clock = pygame.time.Clock()
FPS = 60

# 颜色
SEA_BLUE = (10, 80, 160)
LIGHT_BLUE = (30, 120, 200)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
ORANGE_FISH = (255, 140, 0)
RED_FISH = (220, 30, 30)
GREEN_FISH = (40, 180, 80)
PURPLE_FISH = (160, 60, 200)
NET_COLOR = (220, 220, 220)
SEAWEED = (20, 130, 60)

# 修复字体，避免SysFont报错
try:
    font = pygame.font.Font(pygame.font.get_default_font(), 28)
    small_font = pygame.font.Font(pygame.font.get_default_font(), 20)
except:
    font = None
    small_font = None

# 鱼类
class Fish:
    def __init__(self):
        self.size = random.randint(18, 45)
        self.speed = random.uniform(1.2, 3.5)
        # 随机方向：左往右 / 右往左
        self.dir = random.choice([1, -1])
        if self.dir == 1:
            self.x = -self.size
        else:
            self.x = WIDTH + self.size
        self.y = random.randint(60, HEIGHT - 80)
        # 随机鱼颜色与分数
        color_list = [
            (ORANGE_FISH, 10),
            (RED_FISH, 25),
            (GREEN_FISH, 15),
            (PURPLE_FISH, 40)
        ]
        self.color, self.score = random.choice(color_list)
        self.rect = pygame.Rect(self.x, self.y, self.size, self.size // 2)

    def update(self):
        self.x += self.speed * self.dir
        self.rect.x = self.x
        self.rect.y = self.y
        # 跑出屏幕重置
        if self.dir == 1 and self.x > WIDTH + 50:
            self.__init__()
        if self.dir == -1 and self.x < -50:
            self.__init__()

    def draw(self):
        w = self.size
        h = self.size // 2
        # 鱼身体椭圆方块
        pygame.draw.ellipse(screen, self.color, (self.x, self.y, w, h))
        # 鱼尾
        tail_x = self.x + w if self.dir == 1 else self.x
        tail_w = w // 4
        pygame.draw.polygon(screen, self.color, [
            (tail_x, self.y),
            (tail_x + tail_w * self.dir, self.y - h//2),
            (tail_x + tail_w * self.dir, self.y + h//2)
        ])
        # 鱼眼
        eye_x = self.x + (w - 6) if self.dir == 1 else self.x + 4
        pygame.draw.circle(screen, BLACK, (eye_x, self.y + h//4), 3)

# 渔网类（跟随鼠标）
class FishingNet:
    def __init__(self):
        self.radius = 35
        self.x, self.y = pygame.mouse.get_pos()

    def update(self):
        self.x, self.y = pygame.mouse.get_pos()

    def draw(self):
        # 圆圈渔网
        pygame.draw.circle(screen, NET_COLOR, (self.x, self.y), self.radius, 3)
        # 交叉网线
        r = self.radius
        pygame.draw.line(screen, NET_COLOR, (self.x - r, self.y - r), (self.x + r, self.y + r), 2)
        pygame.draw.line(screen, NET_COLOR, (self.x - r, self.y + r), (self.x + r, self.y - r), 2)

    def get_rect(self):
        return pygame.Rect(
            self.x - self.radius,
            self.y - self.radius,
            self.radius * 2,
            self.radius * 2
        )

# 海草装饰
def draw_seaweed():
    for i in range(12):
        x = i * 70 + random.randint(-20, 20)
        h = random.randint(40, 100)
        pygame.draw.rect(screen, SEAWEED, (x, HEIGHT - h, 8, h))

# 游戏重置
def reset_game():
    fish_group = [Fish() for _ in range(10)]
    net = FishingNet()
    total_score = 0
    game_time = 90  # 秒
    last_tick = pygame.time.get_ticks()
    return fish_group, net, total_score, game_time, last_tick

def main():
    fish_list, net, score, time_left, tick_start = reset_game()
    running = True

    while running:
        clock.tick(FPS)
        now = pygame.time.get_ticks()
        delta_sec = (now - tick_start) / 1000

        # 倒计时
        if delta_sec >= 1:
            time_left -= 1
            tick_start = now
            if time_left <= 0:
                time_left = 0

        # 事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            # 鼠标点击捕鱼
            if event.type == pygame.MOUSEBUTTONDOWN and time_left > 0:
                net_rect = net.get_rect()
                for fish in fish_list:
                    if net_rect.colliderect(fish.rect):
                        score += fish.score
                        fish.__init__()  # 抓到鱼重新生成

        # 绘制分层海洋渐变背景
        screen.fill(SEA_BLUE)
        pygame.draw.rect(screen, LIGHT_BLUE, (0, 0, WIDTH, HEIGHT//3))
        draw_seaweed()

        if time_left > 0:
            # 更新鱼和渔网
            net.update()
            for fish in fish_list:
                fish.update()
                fish.draw()
            net.draw()

        # UI文字面板
        if font:
            score_text = font.render(f"得分：{score}", True, WHITE)
            screen.blit(score_text, (15, 10))

            time_text = font.render(f"剩余时间：{time_left}s", True, WHITE)
            screen.blit(time_text, (WIDTH - 220, 10))

            if time_left <= 0:
                over_text = font.render("时间到！游戏结束", True, (255, 60, 60))
                restart_text = small_font.render("关闭窗口重新打开再来一局", True, WHITE)
                screen.blit(over_text, (WIDTH//2 - 160, HEIGHT//2 - 40))
                screen.blit(restart_text, (WIDTH//2 - 180, HEIGHT//2 + 10))

        pygame.display.flip()

if __name__ == "__main__":
    main()