import pygame
import random
import math

# 初始化
pygame.init()
WIDTH, HEIGHT = 900, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("挖矿模拟器 | 高清强化版")
clock = pygame.time.Clock()
FPS = 60

# === 颜色体系 ===
BG_TOP = (22, 26, 34)
BG_BOTTOM = (55, 60, 70)
CAVE_DARK = (15, 18, 24)
ORE_MAIN = (160, 100, 50)
ORE_DARK = (70, 40, 18)
ORE_LIGHT = (200, 150, 90)
ORE_GOLD = (255, 215, 0)
GOLD_COLOR = (255, 220, 60)
RED_HP = (220, 30, 30)
GREEN_HP = (50, 200, 70)
WHITE_TEXT = (245, 245, 245)
GRAY_BTN = (120, 125, 135)
GRAY_BTN_HOVER = (170, 175, 185)
BLUE_INFO = (70, 180, 255)
BLACK_OUTLINE = (0, 0, 0)
SHINE_COLOR = (255, 255, 255)

# === 字体兜底 ===
try:
    font_large = pygame.font.Font("simhei.ttf", 48)
    font_mid = pygame.font.Font("simhei.ttf", 30)
    font_btn = pygame.font.Font("simhei.ttf", 24)
except:
    font_large = pygame.font.SysFont("Microsoft YaHei", 48)
    font_mid = pygame.font.SysFont("Microsoft YaHei", 30)
    font_btn = pygame.font.SysFont("Microsoft YaHei", 24)

# === 游戏数据 ===
gold = 0
damage = 1
crit_rate = 0.05
auto_mine = False
auto_cooldown = 0

# 矿石
ore_hp = 120
ore_max_hp = 120
ore_rect = pygame.Rect(340, 180, 220, 220)

# 升级按钮
btn_dmg = pygame.Rect(50, 520, 220, 70)
btn_crit = pygame.Rect(310, 520, 220, 70)
btn_auto = pygame.Rect(570, 520, 220, 70)

cost_dmg = 18
cost_crit = 28
cost_auto = 140

# 防爆搓
click_cd = 0
CLICK_CD_LIMIT = 10

# 粒子与特效
particles = []
hit_effects = []
crit_flash = 0
shake_x = 0
shake_y = 0

# === 绘制高质量渐变背景 ===
def draw_gradient_bg():
    for y in range(HEIGHT):
        ratio = y / HEIGHT
        r = int(BG_TOP[0] * (1 - ratio) + BG_BOTTOM[0] * ratio)
        g = int(BG_TOP[1] * (1 - ratio) + BG_BOTTOM[1] * ratio)
        b = int(BG_TOP[2] * (1 - ratio) + BG_BOTTOM[2] * ratio)
        pygame.draw.line(screen, (r, g, b), (0, y), (WIDTH, y))

# === 绘制地底岩层背景 ===
def draw_cave_layers():
    for i in range(5):
        y = 220 + i * 90
        w1 = 120 + i * 35
        w2 = WIDTH - 120 - i * 35
        color = (
            CAVE_DARK[0] + i * 6,
            CAVE_DARK[1] + i * 7,
            CAVE_DARK[2] + i * 9
        )
        pygame.draw.rect(screen, color, (w1, y, w2 - w1, 80))

# === 绘制高清矿石 ===
def draw_ore():
    x, y, w, h = ore_rect.x + shake_x, ore_rect.y + shake_y, ore_rect.width, ore_rect.height

    # 矿石阴影
    pygame.draw.rect(screen, ORE_DARK, (x + 6, y + 6, w, h), border_radius=18)

    # 矿石主体
    ore_surface = pygame.Surface((w, h), pygame.SRCALPHA)
    pygame.draw.rect(ore_surface, ORE_MAIN, (0, 0, w, h), border_radius=16)

    # 高光
    pygame.draw.rect(ore_surface, ORE_LIGHT, (0, 0, w, h // 5), border_top_left_radius=16, border_top_right_radius=16)
    pygame.draw.rect(ore_surface, ORE_LIGHT, (0, 0, w // 5, h), border_top_left_radius=16, border_bottom_left_radius=16)

    # 暗部
    pygame.draw.rect(ore_surface, ORE_DARK, (w - w // 4, 0, w // 4, h), border_top_right_radius=16, border_bottom_right_radius=16)
    pygame.draw.rect(ore_surface, ORE_DARK, (0, h - h // 4, w, h // 4), border_bottom_left_radius=16, border_bottom_right_radius=16)

    # 金色矿脉纹理
    for _ in range(8):
        rx = random.randint(10, w - 10)
        ry = random.randint(10, h - 10)
        pygame.draw.circle(ore_surface, ORE_GOLD, (rx, ry), random.randint(2, 5))

    # 裂纹
    for _ in range(5):
        rx = random.randint(20, w - 20)
        ry = random.randint(20, h - 20)
        pygame.draw.line(ore_surface, ORE_DARK, (rx, ry), (rx + random.randint(-25, 25), ry + random.randint(-25, 25)), 3)

    screen.blit(ore_surface, (x, y))

    # 描边
    pygame.draw.rect(screen, BLACK_OUTLINE, (x, y, w, h), 5, border_radius=16)

    # 暴击闪光
    if crit_flash > 0:
        s = pygame.Surface((w, h), pygame.SRCALPHA)
        s.fill((255, 255, 255, 80))
        screen.blit(s, (x, y))

# === 绘制发光血条 ===
def draw_hp_bar():
    x, y = ore_rect.x + shake_x, ore_rect.y + shake_y - 28
    w = ore_rect.width

    pygame.draw.rect(screen, BLACK_OUTLINE, (x - 3, y - 3, w + 6, 18), 3)
    pygame.draw.rect(screen, RED_HP, (x, y, w, 12))

    hp_now_w = w * (ore_hp / ore_max_hp)
    pygame.draw.rect(screen, GREEN_HP, (x, y, hp_now_w, 12))

    # 发光边缘
    pygame.draw.rect(screen, (100, 255, 100), (x, y, hp_now_w, 12), 1)

# === 生成粒子 ===
def spawn_particles(cx, cy):
    for _ in range(18):
        angle = random.uniform(0, math.pi * 2)
        speed = random.uniform(2, 5)
        particles.append({
            "x": cx,
            "y": cy,
            "vx": math.cos(angle) * speed,
            "vy": math.sin(angle) * speed - 2,
            "size": random.randint(4, 8),
            "life": 50,
            "color": random.choice([ORE_LIGHT, ORE_GOLD, ORE_DARK])
        })

# === 生成打击特效 ===
def spawn_hit_effect(cx, cy, is_crit):
    hit_effects.append({
        "x": cx,
        "y": cy,
        "radius": 10,
        "max_radius": 35 if is_crit else 25,
        "life": 30,
        "color": ORE_GOLD if is_crit else SHINE_COLOR
    })

# === 绘制按钮 ===
def draw_button(rect, text_str, price):
    mx, my = pygame.mouse.get_pos()
    color = GRAY_BTN_HOVER if rect.collidepoint(mx, my) else GRAY_BTN

    pygame.draw.rect(screen, color, rect, border_radius=14)
    pygame.draw.rect(screen, BLACK_OUTLINE, rect, 4, border_radius=14)

    txt = font_btn.render(f"{text_str} {price}金", True, BLACK_OUTLINE)
    screen.blit(txt, (rect.centerx - txt.get_width() // 2, rect.centery - txt.get_height() // 2))

# === 主循环 ===
running = True
while running:
    dt = clock.tick(FPS)
    mx, my = pygame.mouse.get_pos()

    # 背景
    draw_gradient_bg()
    draw_cave_layers()

    # 冷却
    if click_cd > 0:
        click_cd -= 1
    if crit_flash > 0:
        crit_flash -= 1
    shake_x = int(random.randint(-2, 2) * random.random() * 3) if crit_flash > 0 else 0
    shake_y = int(random.randint(-2, 2) * random.random() * 3) if crit_flash > 0 else 0

    # 事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            if ore_rect.collidepoint(mx, my) and click_cd <= 0:
                click_cd = CLICK_CD_LIMIT
                hit_dmg = damage
                is_crit = random.random() < crit_rate

                if is_crit:
                    hit_dmg *= 2
                    crit_flash = 12

                ore_hp -= hit_dmg
                spawn_hit_effect(ore_rect.centerx, ore_rect.centery, is_crit)

                if ore_hp <= 0:
                    reward = ore_max_hp // 4
                    gold += reward
                    ore_hp = ore_max_hp
                    spawn_particles(ore_rect.centerx, ore_rect.centery)

            if btn_dmg.collidepoint(mx, my) and gold >= cost_dmg:
                gold -= cost_dmg
                damage += 1
                cost_dmg = int(cost_dmg * 1.45)

            if btn_crit.collidepoint(mx, my) and gold >= cost_crit:
                gold -= cost_crit
                crit_rate += 0.045
                cost_crit = int(cost_crit * 1.55)

            if btn_auto.collidepoint(mx, my) and not auto_mine and gold >= cost_auto:
                gold -= cost_auto
                auto_mine = True

    # 自动挖矿
    if auto_mine:
        auto_cooldown += 1
        if auto_cooldown >= 50:
            auto_cooldown = 0
            auto_dmg = damage
            is_crit = random.random() < crit_rate

            if is_crit:
                auto_dmg *= 2
                crit_flash = 12

            ore_hp -= auto_dmg
            spawn_hit_effect(ore_rect.centerx, ore_rect.centery, is_crit)

            if ore_hp <= 0:
                gold += ore_max_hp // 4
                ore_hp = ore_max_hp
                spawn_particles(ore_rect.centerx, ore_rect.centery)

    # 更新粒子
    for p in particles[:]:
        p["x"] += p["vx"]
        p["y"] += p["vy"]
        p["vy"] += 0.15
        p["life"] -= 1
        p["size"] = max(1, p["size"] - 0.05)
        pygame.draw.circle(screen, p["color"], (int(p["x"]), int(p["y"])), int(p["size"]))
        if p["life"] <= 0:
            particles.remove(p)

    # 更新打击特效
    for eff in hit_effects[:]:
        eff["radius"] += 3
        eff["life"] -= 1
        alpha = int(255 * (eff["life"] / 30))
        s = pygame.Surface((eff["radius"] * 2, eff["radius"] * 2), pygame.SRCALPHA)
        pygame.draw.circle(s, (*eff["color"], alpha), (eff["radius"], eff["radius"]), eff["radius"], 3)
        screen.blit(s, (eff["x"] - eff["radius"], eff["y"] - eff["radius"]))
        if eff["life"] <= 0:
            hit_effects.remove(eff)

    # 绘制矿石和血条
    draw_ore()
    draw_hp_bar()

    # 信息面板
    text_gold = font_large.render(f"💰 金币：{gold}", True, GOLD_COLOR)
    screen.blit(text_gold, (30, 20))

    text_dmg = font_mid.render(f"⛏️ 镐子伤害：{damage}", True, WHITE_TEXT)
    screen.blit(text_dmg, (30, 80))

    text_crit = font_mid.render(f"⚡ 暴击率：{round(crit_rate * 100, 1)}%", True, WHITE_TEXT)
    screen.blit(text_crit, (30, 125))

    text_auto = font_mid.render(f"🤖 自动挖矿：{'已开启' if auto_mine else '未解锁'}", True, BLUE_INFO)
    screen.blit(text_auto, (30, 170))

    # 按钮
    draw_button(btn_dmg, "强化镐头", cost_dmg)
    draw_button(btn_crit, "提升暴击", cost_crit)
    draw_button(btn_auto, "开启自动挖矿", cost_auto)

    # 鼠标镐子
    pygame.draw.line(screen, (220, 220, 220), (mx, my), (mx + 20, my - 20), 5)
    pygame.draw.circle(screen, (180, 140, 90), (mx + 20, my - 20), 8)

    pygame.display.update()

pygame.quit()