import pygame
import random
import sys

pygame.init()
WIDTH, HEIGHT = 900, 450
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("机甲横版跑酷 | 123456切换机甲")

clock = pygame.time.Clock()
FPS = 60

# 6套机甲配色：(机身颜色, 发光描边, 机甲名称)
MECH_COLORS = [
    [(60, 60, 70), (0, 180, 255), "1号突击机甲"],
    [(40, 45, 55), (255, 80, 0), "2号重装机甲"],
    [(70, 50, 60), (220, 0, 120), "3号侦察机甲"],
    [(30, 35, 40), (100, 255, 100), "4号工程机甲"],
    [(50, 50, 50), (220, 220, 220), "5号钛合金机甲"],
    [(22, 22, 40), (255, 220, 0), "6号雷霆机甲"]
]

# 全局配色
GROUND_COLOR = (25, 25, 30)
OBSTACLE_COLOR = (160, 0, 0)
TEXT_WHITE = (240, 240, 240)

# 文字绘制工具
def draw_text(text, size, color, x, y):
    font = pygame.font.SysFont("simhei", size)
    surf = font.render(text, True, color)
    screen.blit(surf, (x, y))

# 机甲玩家类
class MechPlayer:
    def __init__(self):
        self.w = 48
        self.h = 68
        self.x = 120
        self.y = HEIGHT - self.h - 50
        self.vy = 0
        self.jump_pow = -17
        self.gravity = 0.9
        self.on_ground = True
        self.move_speed = 6
        self.mech_idx = 0  # 默认1号机甲

    def jump(self):
        if self.on_ground:
            self.vy = self.jump_pow

    def update(self):
        # 重力物理
        self.vy += self.gravity
        self.y += self.vy
        ground_y = HEIGHT - 50 - self.h
        if self.y >= ground_y:
            self.y = ground_y
            self.vy = 0
            self.on_ground = True
        else:
            self.on_ground = False

    def switch_mech(self, num):
        # 数字1→索引0，2→1 ... 6→5
        idx_map = {1:0, 2:1, 3:2, 4:3, 5:4, 6:5}
        if num in idx_map:
            self.mech_idx = idx_map[num]

    def draw(self):
        body, glow, name = MECH_COLORS[self.mech_idx]
        # 机甲发光外框
        pygame.draw.rect(screen, glow, (self.x-3, self.y-3, self.w+6, self.h+6))
        # 机甲主体机身
        pygame.draw.rect(screen, body, (self.x, self.y, self.w, self.h))
        # 驾驶舱发光窗口
        pygame.draw.rect(screen, glow, (self.x+10, self.y+12, 28, 22))

# 机械障碍物
class MetalBlock:
    def __init__(self, speed):
        self.w = random.randint(32, 50)
        self.h = random.randint(35, 70)
        self.x = WIDTH
        self.y = HEIGHT - 50 - self.h
        self.speed = speed

    def update(self):
        self.x -= self.speed

    def draw(self):
        pygame.draw.rect(screen, OBSTACLE_COLOR, (self.x, self.y, self.w, self.h))
        pygame.draw.rect(screen, (80,0,0), (self.x+4, self.y+4, self.w-8, self.h-8))

    def is_out(self):
        return self.x + self.w < 0

# 绘制机械地面
def draw_ground():
    pygame.draw.rect(screen, GROUND_COLOR, (0, HEIGHT-50, WIDTH, 50))
    # 地面机甲纹路
    for i in range(0, WIDTH, 60):
        pygame.draw.line(screen, (60,60,70), (i, HEIGHT-50), (i, HEIGHT), 2)

# 游戏主循环
def run_game():
    mech = MechPlayer()
    blocks = []
    score = 0
    game_over = False
    spawn_timer = 0
    base_speed = 7

    while True:
        screen.fill((8,8,12))  # 深空暗色背景
        clock.tick(FPS)

        # 事件捕获
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if not game_over:
                    # 跳跃
                    if event.key == pygame.K_SPACE:
                        mech.jump()
                    # 1~6切换机甲
                    if event.key == pygame.K_1:
                        mech.switch_mech(1)
                    elif event.key == pygame.K_2:
                        mech.switch_mech(2)
                    elif event.key == pygame.K_3:
                        mech.switch_mech(3)
                    elif event.key == pygame.K_4:
                        mech.switch_mech(4)
                    elif event.key == pygame.K_5:
                        mech.switch_mech(5)
                    elif event.key == pygame.K_6:
                        mech.switch_mech(6)
                # R重新开局
                if event.key == pygame.K_r:
                    run_game()

        if not game_over:
            # 左右移动
            keys = pygame.key.get_pressed()
            if keys[pygame.K_LEFT] and mech.x > 0:
                mech.x -= mech.move_speed
            if keys[pygame.K_RIGHT] and mech.x < WIDTH - mech.w:
                mech.x += mech.move_speed

            mech.update()

            # 难度随分数提升
            current_speed = base_speed + score // 12
            spawn_gap = max(45, 90 - score // 8)
            spawn_timer += 1
            if spawn_timer > spawn_gap:
                blocks.append(MetalBlock(current_speed))
                spawn_timer = 0

            # 更新障碍物、计分
            for b in blocks[:]:
                b.update()
                if b.is_out():
                    blocks.remove(b)
                    score += 1

            # 碰撞检测
            mech_rect = pygame.Rect(mech.x, mech.y, mech.w, mech.h)
            for b in blocks:
                b_rect = pygame.Rect(b.x, b.y, b.w, b.h)
                if mech_rect.colliderect(b_rect):
                    game_over = True

        # 绘制所有元素
        draw_ground()
        mech.draw()
        for b in blocks:
            b.draw()

        # UI界面文字
        body, glow, name = MECH_COLORS[mech.mech_idx]
        draw_text(f"分数：{score}", 26, TEXT_WHITE, 15, 12)
        draw_text(f"当前机甲：{name}", 22, glow, 15, 45)
        draw_text("1/2/3/4/5/6切换机甲 | 空格跳跃 | ←→移动 | R重开", 18, (150,150,150), 15, 80)

        # 游戏结束提示
        if game_over:
            draw_text("机甲损毁！按R重新出击", 52, (255,30,30), WIDTH//2-260, HEIGHT//2-30)

        pygame.display.flip()

if __name__ == "__main__":
    run_game()