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

# 颜色定义
SKY_BLUE = (100, 180, 255)
SEA_COLOR = (15, 60, 120)
DEEP_SEA = (8, 30, 70)
WHITE = (255, 255, 255)
RED = (220, 40, 40)
GREEN = (40, 200, 80)
BLACK = (0, 0, 0)

# 解决字体报错重点修改处
try:
    # windows优先尝试黑体
    font = pygame.font.SysFont("SimHei", 22)
except:
    try:
        font = pygame.font.SysFont("Microsoft YaHei", 22)
    except:
        # 全部失败使用默认字体（中文会变成方框，游戏逻辑正常运行）
        font = pygame.font.Font(None, 22)


# 潜水员类
class Diver:
    def __init__(self):
        self.x = 120
        self.y = 80
        self.w = 36
        self.h = 48
        self.speed = 4
        self.oxygen = 100
        self.max_oxygen = 100
        self.dir = 1  # 1右 -1左

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a] or keys[pygame.K_LEFT]:
            self.x -= self.speed
            self.dir = -1
        if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
            self.x += self.speed
            self.dir = 1
        if keys[pygame.K_w] or keys[pygame.K_UP]:
            self.y -= self.speed
        if keys[pygame.K_s] or keys[pygame.K_DOWN]:
            self.y += self.speed

        # 边界限制
        self.x = max(0, min(WIDTH - self.w, self.x))
        self.y = max(0, min(HEIGHT - self.h, self.y))

        # 氧气消耗（水下持续消耗，回到海面恢复）
        if self.y > 120:
            self.oxygen -= 0.04
        else:
            self.oxygen += 0.06
        self.oxygen = max(0, min(self.max_oxygen, self.oxygen))

    def draw(self):
        # 身体
        pygame.draw.ellipse(screen, (30, 130, 190), (self.x, self.y, self.w, self.h))
        # 头
        pygame.draw.circle(screen, (220, 220, 220), (self.x+self.w//2, self.y+8), 14)
        # 氧气瓶
        tank_x = self.x if self.dir == 1 else self.x+self.w-10
        pygame.draw.rect(screen, (80,80,80), (tank_x, self.y+18, 10, 24))

    def get_rect(self):
        return pygame.Rect(self.x, self.y, self.w, self.h)

# 鱼类类
class Fish:
    def __init__(self):
        self.reset()

    def reset(self):
        self.x = random.randint(100, WIDTH-100)
        self.y = random.randint(140, HEIGHT-60)
        self.w = random.randint(22, 42)
        self.h = self.w // 2
        self.speed = random.uniform(1.2, 2.8)
        self.dir = random.choice([-1, 1])
        self.color = (random.randint(60,220), random.randint(60,220), random.randint(60,220))
        self.caught = False

    def update(self):
        if self.caught:
            return
        self.x += self.speed * self.dir
        if self.x < 0 or self.x > WIDTH:
            self.dir *= -1

    def draw(self):
        if self.caught:
            return
        pygame.draw.ellipse(screen, self.color, (self.x, self.y, self.w, self.h))
        # 鱼尾
        tail_x = self.x if self.dir == -1 else self.x + self.w
        pygame.draw.polygon(screen, self.color, [
            (tail_x, self.y+self.h//2),
            (tail_x - 12*self.dir, self.y),
            (tail_x - 12*self.dir, self.y+self.h)
        ])

    def get_rect(self):
        return pygame.Rect(self.x, self.y, self.w, self.h)

# 生成鱼群
fish_list = [Fish() for _ in range(12)]
diver = Diver()
score = 0
game_over = False

# 主循环
running = True
while running:
    clock.tick(FPS)
    screen.fill(SKY_BLUE)

    # 海面分界线
    sea_line_y = 120
    pygame.draw.rect(screen, SEA_COLOR, (0, sea_line_y, WIDTH, HEIGHT-sea_line_y))

    # 事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
            # 点击捕捉鱼
            mouse_pos = pygame.mouse.get_pos()
            diver_rect = diver.get_rect()
            for fish in fish_list:
                if fish.caught:
                    continue
                if diver_rect.colliderect(fish.get_rect()):
                    fish.caught = True
                    score += 10
                    pygame.time.set_timer(pygame.USEREVENT, 1200)
                    break
        if event.type == pygame.USEREVENT:
            # 捕捉后刷新鱼
            for fish in fish_list:
                if fish.caught:
                    fish.reset()

    if not game_over:
        diver.update()
        for fish in fish_list:
            fish.update()

    # 氧气耗尽判定
    if diver.oxygen <= 0:
        game_over = True

    # 绘制潜水员
    diver.draw()
    # 绘制鱼
    for fish in fish_list:
        fish.draw()

    # UI绘制
    # 氧气条背景
    pygame.draw.rect(screen, BLACK, (20, 20, 204, 24))
    oxygen_color = GREEN if diver.oxygen > 30 else RED
    pygame.draw.rect(screen, oxygen_color, (22, 22, diver.oxygen*2, 20))
    screen.blit(font.render("氧气", True, WHITE), (230, 20))

    score_text = font.render(f"捕获分数：{score}", True, WHITE)
    screen.blit(score_text, (20, 55))

    tip_text = font.render("WASD移动 | 靠近鱼点击鼠标捕捉 | 回到海面恢复氧气", True, WHITE)
    screen.blit(tip_text, (20, HEIGHT - 35))

    if game_over:
        over_text = font.render("氧气耗尽！游戏结束", True, RED)
        screen.blit(over_text, (WIDTH//2 - 110, HEIGHT//2))

    pygame.display.flip()

pygame.quit()
sys.exit()