import pygame
import random
import sys
import math

# 初始化 Pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("猫抓老鼠 - 美化版")

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
PINK = (255, 192, 203)
BLUE = (100, 149, 237)      # 猫的主色（矢车菊蓝）
DARK_BLUE = (70, 130, 180)
GRAY = (169, 169, 169)
DARK_GRAY = (105, 105, 105)
GREEN = (144, 238, 144)     # 背景草地
BROWN = (139, 69, 19)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)

# 帧率
clock = pygame.time.Clock()
FPS = 60

# 字体
font = pygame.font.Font(None, 36)

# 猫的属性
CAT_SPEED = 5
cat_x = WIDTH // 2
cat_y = HEIGHT // 2
cat_radius = 30   # 脸的半径

# 老鼠的属性
MOUSE_SPEED_BASE = 3
mouse_speed = MOUSE_SPEED_BASE
mouse_x = random.randint(50, WIDTH - 50)
mouse_y = random.randint(50, HEIGHT - 50)
mouse_dx = random.choice([-1, 1]) * mouse_speed
mouse_dy = random.choice([-1, 1]) * mouse_speed
direction_timer = 0
mouse_radius = 12

# 分数
score = 0

# 随机点缀的小花位置（背景装饰）
flowers = [(random.randint(50, WIDTH-50), random.randint(50, HEIGHT-50)) for _ in range(15)]

def reset_mouse():
    global mouse_x, mouse_y, mouse_dx, mouse_dy
    mouse_x = random.randint(50, WIDTH - 50)
    mouse_y = random.randint(50, HEIGHT - 50)
    mouse_dx = random.choice([-1, 1]) * mouse_speed
    mouse_dy = random.choice([-1, 1]) * mouse_speed

def check_collision():
    # 猫的检测范围用圆形，老鼠也用圆形
    dx = (cat_x - mouse_x) ** 2
    dy = (cat_y - mouse_y) ** 2
    distance = math.sqrt(dx + dy)
    return distance < (cat_radius + mouse_radius)

def draw_cat(screen, x, y):
    """绘制可爱的卡通猫"""
    # 耳朵（两个三角形）
    ear_size = 18
    # 左耳
    pygame.draw.polygon(screen, BLUE, [
        (x - 15, y - 18),
        (x - 28, y - 40),
        (x - 5, y - 30)
    ])
    pygame.draw.polygon(screen, DARK_BLUE, [
        (x - 12, y - 20),
        (x - 24, y - 35),
        (x - 6, y - 28)
    ])
    # 右耳
    pygame.draw.polygon(screen, BLUE, [
        (x + 15, y - 18),
        (x + 28, y - 40),
        (x + 5, y - 30)
    ])
    pygame.draw.polygon(screen, DARK_BLUE, [
        (x + 12, y - 20),
        (x + 24, y - 35),
        (x + 6, y - 28)
    ])
    
    # 脸（大圆）
    pygame.draw.circle(screen, BLUE, (x, y), cat_radius)
    # 内耳颜色点缀
    pygame.draw.circle(screen, PINK, (x - 12, y - 25), 6)
    pygame.draw.circle(screen, PINK, (x + 12, y - 25), 6)
    
    # 眼睛（白色底，黑色瞳孔，高光）
    eye_radius = 8
    # 左眼
    pygame.draw.circle(screen, WHITE, (x - 10, y - 5), eye_radius)
    pygame.draw.circle(screen, BLACK, (x - 8, y - 5), 5)
    pygame.draw.circle(screen, WHITE, (x - 6, y - 7), 2)  # 高光
    # 右眼
    pygame.draw.circle(screen, WHITE, (x + 10, y - 5), eye_radius)
    pygame.draw.circle(screen, BLACK, (x + 12, y - 5), 5)
    pygame.draw.circle(screen, WHITE, (x + 14, y - 7), 2)
    
    # 鼻子（粉色小三角或椭圆）
    pygame.draw.ellipse(screen, PINK, (x - 3, y + 5, 6, 4))
    
    # 嘴巴（两条弧线，用简单的线表示）
    pygame.draw.arc(screen, BLACK, (x - 8, y + 4, 16, 12), 0.2, 1.5, 2)
    pygame.draw.line(screen, BLACK, (x, y + 7), (x, y + 12), 2)
    
    # 胡须（六根线）
    whisker_color = (50, 50, 50)
    # 左边三根
    pygame.draw.line(screen, whisker_color, (x - 15, y + 2), (x - 35, y - 5), 2)
    pygame.draw.line(screen, whisker_color, (x - 15, y + 8), (x - 35, y + 10), 2)
    pygame.draw.line(screen, whisker_color, (x - 13, y + 12), (x - 30, y + 20), 2)
    # 右边三根
    pygame.draw.line(screen, whisker_color, (x + 15, y + 2), (x + 35, y - 5), 2)
    pygame.draw.line(screen, whisker_color, (x + 15, y + 8), (x + 35, y + 10), 2)
    pygame.draw.line(screen, whisker_color, (x + 13, y + 12), (x + 30, y + 20), 2)
    
    # 尾巴（从身体下方伸出来，简单曲线）
    pygame.draw.line(screen, BLUE, (x, y + cat_radius), (x + 30, y + cat_radius + 20), 6)
    pygame.draw.line(screen, DARK_BLUE, (x, y + cat_radius), (x + 28, y + cat_radius + 18), 3)

def draw_mouse(screen, x, y):
    """绘制可爱的卡通老鼠"""
    # 身体（椭圆）
    body_rect = (x - 15, y - 8, 30, 20)
    pygame.draw.ellipse(screen, GRAY, body_rect)
    pygame.draw.ellipse(screen, DARK_GRAY, body_rect, 2)  # 边缘
    
    # 耳朵（两个圆）
    pygame.draw.circle(screen, GRAY, (x - 8, y - 12), 8)
    pygame.draw.circle(screen, PINK, (x - 8, y - 12), 5)
    pygame.draw.circle(screen, GRAY, (x + 8, y - 12), 8)
    pygame.draw.circle(screen, PINK, (x + 8, y - 12), 5)
    
    # 眼睛（小黑点）
    pygame.draw.circle(screen, BLACK, (x - 5, y - 2), 3)
    pygame.draw.circle(screen, BLACK, (x + 5, y - 2), 3)
    # 眼睛高光
    pygame.draw.circle(screen, WHITE, (x - 4, y - 3), 1)
    pygame.draw.circle(screen, WHITE, (x + 6, y - 3), 1)
    
    # 鼻子（小黑圆）
    pygame.draw.circle(screen, BLACK, (x, y + 2), 3)
    
    # 尾巴（弯曲的长线）
    tail_start = (x - 15, y + 5)
    tail_control1 = (x - 35, y + 15)
    tail_control2 = (x - 45, y + 5)
    tail_end = (x - 35, y - 5)
    # 用多条线段模拟曲线
    points = [tail_start, tail_control1, tail_control2, tail_end]
    pygame.draw.lines(screen, PINK, False, points, 3)

def draw_background():
    """绘制背景（草地、房子、花朵等）"""
    screen.fill(GREEN)
    # 画一些深绿色草丛
    for i in range(0, WIDTH, 30):
        pygame.draw.arc(screen, (0, 100, 0), (i, HEIGHT-30, 60, 40), 3.14, 6.28, 3)
    
    # 画房子（简单的小房子在右上角）
    house_x = WIDTH - 150
    house_y = HEIGHT - 200
    pygame.draw.rect(screen, BROWN, (house_x, house_y, 120, 120))  # 房体
    pygame.draw.polygon(screen, RED, [(house_x-10, house_y), (house_x+60, house_y-50), (house_x+130, house_y)])  # 屋顶
    pygame.draw.rect(screen, YELLOW, (house_x+40, house_y+30, 30, 40))  # 门
    pygame.draw.rect(screen, (135, 206, 235), (house_x+80, house_y+20, 25, 25))  # 窗户
    pygame.draw.line(screen, BLACK, (house_x+92, house_y+20), (house_x+92, house_y+45), 2)
    pygame.draw.line(screen, BLACK, (house_x+80, house_y+32), (house_x+105, house_y+32), 2)
    
    # 画几朵小花
    for fx, fy in flowers:
        pygame.draw.circle(screen, YELLOW, (fx, fy), 4)  # 花心
        for angle in range(0, 360, 60):
            rad = math.radians(angle)
            petal_x = fx + math.cos(rad) * 8
            petal_y = fy + math.sin(rad) * 8
            pygame.draw.circle(screen, WHITE, (int(petal_x), int(petal_y)), 3)
    # 画太阳
    pygame.draw.circle(screen, YELLOW, (60, 60), 40)
    for i in range(0, 360, 30):
        rad = math.radians(i)
        end_x = 60 + math.cos(rad) * 50
        end_y = 60 + math.sin(rad) * 50
        pygame.draw.line(screen, YELLOW, (60, 60), (end_x, end_y), 3)

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 键盘控制猫（WASD 或 方向键）
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] or keys[pygame.K_a]:
        cat_x -= CAT_SPEED
    if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
        cat_x += CAT_SPEED
    if keys[pygame.K_UP] or keys[pygame.K_w]:
        cat_y -= CAT_SPEED
    if keys[pygame.K_DOWN] or keys[pygame.K_s]:
        cat_y += CAT_SPEED

    # 边界限制
    cat_x = max(cat_radius, min(WIDTH - cat_radius, cat_x))
    cat_y = max(cat_radius, min(HEIGHT - cat_radius, cat_y))

    # 老鼠移动逻辑
    direction_timer += 1
    if direction_timer >= 40:  # 每40帧有机会转向
        direction_timer = 0
        if random.random() < 0.7:
            mouse_dx = random.choice([-1, 1]) * mouse_speed
            mouse_dy = random.choice([-1, 1]) * mouse_speed

    mouse_x += mouse_dx
    mouse_y += mouse_dy

    # 边界反弹
    if mouse_x <= mouse_radius or mouse_x >= WIDTH - mouse_radius:
        mouse_dx *= -1
        mouse_x = max(mouse_radius, min(WIDTH - mouse_radius, mouse_x))
    if mouse_y <= mouse_radius or mouse_y >= HEIGHT - mouse_radius:
        mouse_dy *= -1
        mouse_y = max(mouse_radius, min(HEIGHT - mouse_radius, mouse_y))

    # 碰撞检测（猫抓住老鼠）
    if check_collision():
        score += 1
        reset_mouse()
        mouse_speed = min(6, mouse_speed + 0.2)

    # 绘制一切
    draw_background()
    draw_cat(screen, cat_x, cat_y)
    draw_mouse(screen, mouse_x, mouse_y)
    
    # 显示分数
    score_text = font.render(f"得分: {score}", True, BLACK)
    screen.blit(score_text, (10, 10))
    # 操作提示
    tip = font.render("方向键/WASD 移动猫", True, BLACK)
    screen.blit(tip, (10, 50))

    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()
sys.exit()