import pygame
import random

# --- 初始化 ---
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("打地鼠 - 识别数据类型")
clock = pygame.time.Clock()

# 颜色定义
BG_COLOR = (245, 245, 220) # 米色背景
HOLE_COLOR = (60, 60, 60)
MOLE_COLOR = (210, 105, 30) # 地鼠棕色
TEXT_COLOR = (255, 255, 255)
UI_BG = (30, 30, 40)

FONT = pygame.font.SysFont("arial", 32)
BIG_FONT = pygame.font.SysFont("arial", 60)

# 题库：包含数值和它的正确类型
DATA_QUESTIONS = [
    ("100", "int"), ("-50", "int"), ("0", "int"),
    ("3.14", "float"), ("0.001", "float"), ("-9.8", "float"),
    ('"Hello"', "str"), ("'Python'", "str"), ('"123"', "str"),
    ("True", "bool"), ("False", "bool"),
    ("[1, 2]", "list"), ("['a', 'b']", "list"),
    ("{'key': 1}", "dict"),
]

class Mole:
    def __init__(self, x, y, mole_type):
        self.x = x
        self.y = y
        self.type = mole_type
        self.rect = pygame.Rect(x, y + 40, 100, 100) # 碰撞区域
        self.active = False
        self.timer = 0
        self.max_time = 120 # 停留时间

    def pop_up(self):
        self.active = True
        self.timer = 0

    def update(self):
        if self.active:
            self.timer += 1
            if self.timer > self.max_time:
                self.active = False

    def draw(self, surface):
        if not self.active:
            return
        # 画地鼠身体
        offset = max(0, (self.max_time - self.timer) // 5) # 简单的上下动画
        draw_y = self.y + offset
        pygame.draw.ellipse(surface, MOLE_COLOR, (self.x, draw_y, 100, 100))
        # 画眼睛
        pygame.draw.circle(surface, TEXT_COLOR, (self.x + 30, draw_y + 30), 10)
        pygame.draw.circle(surface, TEXT_COLOR, (self.x + 70, draw_y + 30), 10)
        # 画类型标签
        text = FONT.render(self.type, True, TEXT_COLOR)
        surface.blit(text, (self.x + 15, draw_y + 40))

class Game:
    def __init__(self):
        self.score = 0
        self.lives = 3
        self.current_question = ""
        self.correct_type = ""
        
        # 创建三个地鼠洞位
        self.holes = [
            Mole(100, 350, "int"),
            Mole(350, 350, "float"),
            Mole(600, 350, "str")
        ]
        # 额外补充 bool/list/dict 的逻辑可以后续扩展，这里先聚焦基础三巨头
        
        self.spawn_new_round()

    def spawn_new_round(self):
        # 随机选一道题
        q, t = random.choice(DATA_QUESTIONS)
        self.current_question = q
        self.correct_type = t
        
        # 让对应的地鼠冒出来
        for mole in self.holes:
            mole.active = False
            if mole.type == t:
                mole.pop_up()

    def handle_click(self, pos):
        mx, my = pos
        for mole in self.holes:
            if mole.active and mole.rect.collidepoint(mx, my):
                if mole.type == self.correct_type:
                    self.score += 10
                    self.spawn_new_round()
                else:
                    self.lives -= 1
                    mole.active = False # 打错了地鼠躲回去

    def update(self):
        for mole in self.holes:
            mole.update()
        if self.lives <= 0:
            pass # 游戏结束逻辑

    def draw(self, surface):
        surface.fill(BG_COLOR)
        
        # 画题目区域
        pygame.draw.rect(surface, UI_BG, (0, 0, WIDTH, 200))
        question_text = BIG_FONT.render(f"这个数据的类型是？ -> {self.current_question}", True, (255, 255, 0))
        q_rect = question_text.get_rect(center=(WIDTH//2, 100))
        surface.blit(question_text, q_rect)

        # 画洞和地鼠
        for mole in self.holes:
            # 画洞
            pygame.draw.ellipse(surface, HOLE_COLOR, (mole.x, mole.y + 80, 100, 40))
            mole.draw(surface)

        # 画分数和生命
        score_text = FONT.render(f"得分: {self.score}", True, (0, 0, 0))
        lives_text = FONT.render(f"生命: {'❤️' * self.lives}", True, (255, 0, 0))
        surface.blit(score_text, (20, 220))
        surface.blit(lives_text, (WIDTH - 150, 220))

        if self.lives <= 0:
            over_text = BIG_FONT.render("GAME OVER", True, (255, 0, 0))
            screen.blit(over_text, (WIDTH//2 - 150, HEIGHT//2))

# --- 主程序 ---
game = Game()
running = True

while running:
    clock.tick(60)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if game.lives > 0:
                game.handle_click(event.pos)

    game.update()
    game.draw(screen)
    pygame.display.flip()

pygame.quit()