import pygame
import random
import math

# 初始化Pygame
pygame.init()

# 游戏窗口设置
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("捕鱼游戏")

# 颜色定义
WHITE = (255, 255, 255)
BLUE = (0, 100, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
DARK_BLUE = (0, 0, 139)

# 游戏时钟
clock = pygame.time.Clock()
FPS = 60

class Fish:
    def __init__(self, x, y, fish_type="small"):
        self.x = x
        self.y = y
        self.fish_type = fish_type
        
        # 根据鱼类类型设置属性
        if fish_type == "small":
            self.size = 20
            self.speed = 2
            self.score = 10
            self.color = YELLOW
        elif fish_type == "medium":
            self.size = 30
            self.speed = 1.5
            self.score = 20
            self.color = ORANGE
        elif fish_type == "large":
            self.size = 40
            self.speed = 1
            self.score = 30
            self.color = RED
        
        # 随机移动方向
        self.direction = random.uniform(0, 2 * math.pi)
        self.change_direction_timer = random.randint(30, 90)
        self.timer = 0
        
    def move(self):
        self.timer += 1
        
        # 随机改变方向
        if self.timer >= self.change_direction_timer:
            self.direction = random.uniform(0, 2 * math.pi)
            self.change_direction_timer = random.randint(30, 90)
            self.timer = 0
        
        # 移动鱼
        self.x += math.cos(self.direction) * self.speed
        self.y += math.sin(self.direction) * self.speed
        
        # 边界检查，让鱼在窗口内反弹
        if self.x < self.size:
            self.x = self.size
            self.direction = math.pi - self.direction
        elif self.x > WINDOW_WIDTH - self.size:
            self.x = WINDOW_WIDTH - self.size
            self.direction = math.pi - self.direction
        
        if self.y < self.size:
            self.y = self.size
            self.direction = -self.direction
        elif self.y > WINDOW_HEIGHT - self.size:
            self.y = WINDOW_HEIGHT - self.size
            self.direction = -self.direction
    
    def draw(self, screen):
        # 绘制鱼的身体
        pygame.draw.ellipse(screen, self.color, 
                           (self.x - self.size, self.y - self.size//2, 
                            self.size * 2, self.size))
        
        # 绘制鱼的眼睛
        eye_x = self.x + self.size//2 + 5
        eye_y = self.y - self.size//4
        pygame.draw.circle(screen, WHITE, (eye_x, eye_y), self.size//4)
        pygame.draw.circle(screen, DARK_BLUE, (eye_x + 1, eye_y), self.size//6)
        
        # 绘制鱼尾巴
        tail_points = [
            (self.x - self.size - 10, self.y - self.size//2),
            (self.x - self.size, self.y),
            (self.x - self.size - 10, self.y + self.size//2)
        ]
        pygame.draw.polygon(screen, self.color, tail_points)
    
    def check_click(self, mouse_pos):
        # 检查鼠标点击是否在鱼上
        distance = math.sqrt((mouse_pos[0] - self.x)**2 + (mouse_pos[1] - self.y)**2)
        return distance < self.size

class Net:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.visible = False
        self.animation_timer = 0
        self.size = 40
    
    def set_position(self, x, y):
        self.x = x
        self.y = y
        self.visible = True
        self.animation_timer = 10
    
    def update(self):
        if self.animation_timer > 0:
            self.animation_timer -= 1
        else:
            self.visible = False
    
    def draw(self, screen):
        if self.visible:
            # 绘制网的效果
            alpha = self.animation_timer / 10 * 255
            for i in range(3):
                radius = self.size - i * 10
                if radius > 0:
                    pygame.draw.circle(screen, WHITE, (int(self.x), int(self.y)), 
                                     radius, 2)

class Game:
    def __init__(self):
        self.fishes = []
        self.net = Net()
        self.score = 0
        self.combo = 0
        self.max_combo = 0
        self.missed = 0
        self.game_time = 60  # 游戏时间60秒
        self.start_time = pygame.time.get_ticks()
        self.font = pygame.font.Font(None, 36)
        self.small_font = pygame.font.Font(None, 24)
        
        # 生成初始鱼群
        self.spawn_fish(10)
    
    def spawn_fish(self, count):
        types = ["small", "medium", "large"]
        weights = [0.6, 0.3, 0.1]  # 小鱼出现概率更高
        
        for _ in range(count):
            x = random.randint(100, WINDOW_WIDTH - 100)
            y = random.randint(100, WINDOW_HEIGHT - 100)
            fish_type = random.choices(types, weights=weights)[0]
            self.fishes.append(Fish(x, y, fish_type))
    
    def handle_click(self, mouse_pos):
        self.net.set_position(mouse_pos[0], mouse_pos[1])
        
        caught = False
        for fish in self.fishes[:]:
            if fish.check_click(mouse_pos):
                self.score += fish.score
                self.fishes.remove(fish)
                caught = True
        
        if caught:
            self.combo += 1
            self.max_combo = max(self.max_combo, self.combo)
            # 生成新鱼
            self.spawn_fish(1)
        else:
            self.combo = 0
            self.missed += 1
    
    def update(self):
        # 更新所有鱼的位置
        for fish in self.fishes:
            fish.move()
        
        # 更新网
        self.net.update()
        
        # 保持鱼的数量
        if len(self.fishes) < 8:
            self.spawn_fish(2)
        
        # 检查游戏时间
        elapsed_time = (pygame.time.get_ticks() - self.start_time) / 1000
        if elapsed_time >= self.game_time:
            return False
        return True
    
    def draw(self, screen):
        # 绘制海洋背景
        screen.fill(BLUE)
        
        # 绘制气泡效果
        for i in range(20):
            bubble_x = (pygame.time.get_ticks() // 10 + i * 40) % WINDOW_WIDTH
            bubble_y = (pygame.time.get_ticks() // 20 + i * 30) % WINDOW_HEIGHT
            pygame.draw.circle(screen, WHITE, (bubble_x, bubble_y), 2, 1)
        
        # 绘制所有鱼
        for fish in self.fishes:
            fish.draw(screen)
        
        # 绘制网
        self.net.draw(screen)
        
        # 绘制UI
        elapsed_time = (pygame.time.get_ticks() - self.start_time) / 1000
        time_left = max(0, self.game_time - elapsed_time)
        
        score_text = self.font.render(f"分数: {self.score}", True, WHITE)
        time_text = self.font.render(f"时间: {int(time_left)}", True, WHITE)
        combo_text = self.small_font.render(f"连击: {self.combo}", True, WHITE)
        missed_text = self.small_font.render(f"失误: {self.missed}", True, WHITE)
        
        screen.blit(score_text, (10, 10))
        screen.blit(time_text, (10, 50))
        screen.blit(combo_text, (10, 90))
        screen.blit(missed_text, (10, 120))
        
        # 如果游戏结束，显示结果
        if time_left <= 0:
            game_over_text = self.font.render("游戏结束!", True, WHITE)
            final_score_text = self.font.render(f"最终分数: {self.score}", True, WHITE)
            max_combo_text = self.small_font.render(f"最大连击: {self.max_combo}", True, WHITE)
            
            screen.blit(game_over_text, (WINDOW_WIDTH//2 - 100, WINDOW_HEIGHT//2 - 60))
            screen.blit(final_score_text, (WINDOW_WIDTH//2 - 120, WINDOW_HEIGHT//2 - 20))
            screen.blit(max_combo_text, (WINDOW_WIDTH//2 - 80, WINDOW_HEIGHT//2 + 20))

def main():
    game = Game()
    running = True
    
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:  # 左键点击
                    game.handle_click(event.pos)
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:  # 按R键重新开始
                    game = Game()
                elif event.key == pygame.K_ESCAPE:  # 按ESC键退出
                    running = False
        
        # 更新游戏状态
        if not game.update():
            # 游戏结束，等待玩家重新开始或退出
            pass
        
        # 绘制游戏画面
        game.draw(screen)
        pygame.display.flip()
        
        # 控制帧率
        clock.tick(FPS)
    
    pygame.quit()

if __name__ == "__main__":
    main()