import pygame
import random

# ========== 设置 ==========
WIDTH = 600
HEIGHT = 650
FPS = 60

# 颜色
SKY = (100, 200, 100)
GROUND = (92, 64, 51)
HOLE_COLOR = (30, 20, 10)
WHITE = (255,255,255)
RED = (255, 40, 40)
BLACK = (0,0,0)

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("打地鼠")
clock = pygame.time.Clock()
font = pygame.font.Font(None, 40)

# 地洞位置 3行3列
hole_positions = [
    (100, 180), (300, 180), (500, 180),
    (100, 340), (300, 340), (500, 340),
    (100, 500), (300, 500), (500, 500)
]
HOLE_RADIUS = 70

class Mole:
    def __init__(self, pos):
        self.x, self.y = pos
        self.height = 0   # 探出高度 0完全藏起来
        self.max_h = 55
        self.show = False
        self.timer = 0
        self.hit = False

    def pop_up(self):
        self.show = True
        self.timer = random.randint(60, 120)
        self.hit = False

    def update(self):
        if self.show:
            if self.height < self.max_h:
                self.height += 4
            self.timer -= 1
            if self.timer <= 0:
                self.show = False
        else:
            if self.height > 0:
                self.height -= 5

    def draw(self):
        # 地洞
        pygame.draw.circle(screen, HOLE_COLOR, (self.x, self.y), HOLE_RADIUS)
        # 地鼠
        if self.height > 0:
            y_top = self.y - self.height
            # 脑袋
            pygame.draw.circle(screen, (150,110,80), (self.x, y_top), 38)
            # 耳朵
            pygame.draw.circle(screen, (120,80,60), (self.x-28, y_top-22), 14)
            pygame.draw.circle(screen, (120,80,60), (self.x+28, y_top-22), 14)
            # 眼睛
            if not self.hit:
                pygame.draw.circle(screen, BLACK, (self.x-14, y_top-6),5)
                pygame.draw.circle(screen, BLACK, (self.x+14, y_top-6),5)
            else:
                # 击中变晕眼
                pygame.draw.line(screen, BLACK, (self.x-18, y_top-8), (self.x-8, y_top), 3)
                pygame.draw.line(screen, BLACK, (self.x+8, y_top-8), (self.x+18, y_top), 3)

    def check_hit(self, mx, my):
        if not self.show or self.hit:
            return False
        dist = ((mx - self.x)**2 + (my - (self.y - self.height))**2)**0.5
        if dist < 40:
            self.hit = True
            self.timer = 15
            return True
        return False

# 创建所有地鼠
moles = [Mole(p) for p in hole_positions]
score = 0
time_left = 60
game_running = True
last_spawn = 0
spawn_interval = 90

# 隐藏默认鼠标，自定义锤子
pygame.mouse.set_visible(False)

running = True
while running:
    clock.tick(FPS)
    screen.fill(SKY)
    # 草地底色
    pygame.draw.rect(screen, GROUND, (0, 120, WIDTH, HEIGHT-120))

    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 game_running:
            for m in moles:
                if m.check_hit(mx, my):
                    score += 10

    if game_running:
        last_spawn +=1
        if last_spawn > spawn_interval:
            last_spawn = 0
            available = [m for m in moles if not m.show]
            if available:
                random.choice(available).pop_up()

        # 更新地鼠
        for m in moles:
            m.update()

        # 倒计时简易逻辑
        time_text = font.render(f"时间: {time_left}", True, WHITE)
        scr_text = font.render(f"分数: {score}", True, WHITE)
        screen.blit(time_text, (30,30))
        screen.blit(scr_text, (420,30))
    else:
        over_text = font.render("游戏结束！", True, RED)
        final = font.render(f"最终得分：{score}", True, WHITE)
        screen.blit(over_text, (WIDTH//2-100, HEIGHT//2-60))
        screen.blit(final, (WIDTH//2-110, HEIGHT//2))

    # 绘制地鼠（连带地洞）
    for m in moles:
        m.draw()

    # 绘制锤子光标
    pygame.draw.rect(screen, (130,90,40), (mx-18, my-32, 12, 35))
    pygame.draw.circle(screen, (80,80,80), (mx-12, my-36), 16)

    pygame.display.update()

pygame.quit()