import pygame
import random
import sys

pygame.init()
WIDTH = 800
HEIGHT = 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("横版闯关 简单纯净版｜5关卡")

# 色彩
SKY = (110, 175, 250)
SKY_TOP = (80, 140, 220)
GROUND_TOP = (70, 160, 50)
GROUND_BOTTOM = (92, 64, 42)
PLAYER_MAIN = (45, 155, 235)
PLAYER_OUTLINE = (25, 90, 140)
SPIKE_COLOR = (40, 40, 40)
FINISH_GREEN = (35, 190, 75)
WHITE = (255, 255, 255)
RED = (220, 30, 30)
YELLOW = (255, 220, 0)
BLACK = (0,0,0)

# 字体（海龟编辑器兼容）
font = pygame.font.Font(None, 40)
small_font = pygame.font.Font(None, 32)

# 玩家参数
pw = 32
ph = 44
speed = 5
gravity = 0.55
jump_power = -14

# ===================== 关卡数据（无怪物，难度柔和） =====================
levels = [
    # 第1关
    {
        "platforms": [
            [0, 440, 220, 60],
            [260, 380, 180, 22],
            [480, 320, 160, 22],
            [660, 260, 140, 22],
        ],
        "spikes": [
            [500, 298, 36, 22],
        ],
        "finish": [720, 216, 48, 44],
        "start_x": 60,
        "start_y": 350
    },
    # 第2关
    {
        "platforms": [
            [0, 440, 180, 60],
            [220, 360, 140, 22],
            [380, 280, 140, 22],
            [540, 360, 140, 22],
            [700, 280, 100, 22],
        ],
        "spikes": [
            [400, 258, 36, 22],
        ],
        "finish": [700, 236, 48, 44],
        "start_x": 40,
        "start_y": 350
    },
    # 第3关
    {
        "platforms": [
            [0, 440, 160, 60],
            [180, 340, 120, 22],
            [320, 240, 120, 22],
            [460, 340, 120, 22],
            [600, 240, 120, 22],
            [720, 160, 80, 22],
        ],
        "spikes": [
            [320, 218, 36, 22],
            [600, 218, 36, 22],
        ],
        "finish": [720, 116, 48, 44],
        "start_x": 30,
        "start_y": 350
    },
    # 第4关
    {
        "platforms": [
            [0, 440, 140, 60],
            [160, 360, 120, 22],
            [300, 280, 120, 22],
            [440, 200, 120, 22],
            [580, 280, 120, 22],
            [700, 200, 100, 22],
        ],
        "spikes": [
            [300, 258, 36, 22],
        ],
        "finish": [700, 156, 48, 44],
        "start_x": 20,
        "start_y": 350
    },
    # 第5关
    {
        "platforms": [
            [0, 440, 120, 60],
            [140, 350, 100, 22],
            [260, 260, 100, 22],
            [380, 340, 100, 22],
            [500, 260, 100, 22],
            [620, 180, 100, 22],
            [740, 100, 60, 22],
        ],
        "spikes": [
            [260, 238, 36, 22],
            [500, 238, 36, 22],
        ],
        "finish": [740, 56, 48, 44],
        "start_x": 15,
        "start_y": 350
    }
]

# 全局游戏状态
current_level = 0
px = 0
py = 0
vx = 0
vy = 0
on_ground = False
can_double_jump = True
game_over = False
win_level = False
game_complete = False
timer = 0

def load_level(lv_idx):
    global px,py,vx,vy,on_ground,can_double_jump,game_over,win_level
    lv = levels[lv_idx]
    px = lv["start_x"]
    py = lv["start_y"]
    vx = 0
    vy = 0
    on_ground = False
    can_double_jump = True
    game_over = False
    win_level = False

load_level(current_level)
clock = pygame.time.Clock()
running = True

while running:
    timer += 1
    screen.fill(SKY)
    clock.tick(60)
    keys = pygame.key.get_pressed()
    lv_data = levels[current_level]

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                if game_complete:
                    current_level = 0
                    game_complete = False
                    load_level(current_level)
                else:
                    load_level(current_level)
            if not game_over and not win_level and not game_complete:
                if event.key == pygame.K_UP:
                    if on_ground:
                        vy = jump_power
                        on_ground = False
                    elif can_double_jump:
                        vy = jump_power
                        can_double_jump = False

    if not game_over and not win_level and not game_complete:
        # 玩家移动
        vx = 0
        if keys[pygame.K_LEFT]:
            vx = -speed
        if keys[pygame.K_RIGHT]:
            vx = speed
        vy += gravity
        px += vx
        py += vy

        player_rect = pygame.Rect(px, py, pw, ph)
        on_ground = False
        # 平台碰撞
        for plat in lv_data["platforms"]:
            pr = pygame.Rect(*plat)
            if player_rect.colliderect(pr):
                if vy > 0 and player_rect.bottom - vy <= pr.top + 4:
                    py = pr.top - ph
                    vy = 0
                    on_ground = True
                    can_double_jump = True

        # 左右边界
        if px < 0:
            px = 0
        if px > WIDTH - pw:
            px = WIDTH - pw

        # 掉落深渊
        if py > HEIGHT:
            game_over = True

        # 尖刺碰撞
        for s in lv_data["spikes"]:
            sr = pygame.Rect(*s)
            if player_rect.colliderect(sr):
                game_over = True

        # 到达终点
        finish_rect = pygame.Rect(*lv_data["finish"])
        if player_rect.colliderect(finish_rect):
            win_level = True

    # 关卡切换
    if win_level and not game_complete:
        current_level += 1
        if current_level >= len(levels):
            game_complete = True
        else:
            pygame.time.delay(600)
            load_level(current_level)

    # ========== 绘制平台 ==========
    for p in lv_data["platforms"]:
        x,y,w,h = p
        pygame.draw.rect(screen, GROUND_BOTTOM, (x,y+4,w,h-4), border_radius=4)
        pygame.draw.rect(screen, GROUND_TOP, (x,y,w,6), border_radius=4)

    # ========== 绘制三角尖刺 ==========
    for s in lv_data["spikes"]:
        x,y,w,h = s
        point1 = (x, y+h)
        point2 = (x+w//2, y)
        point3 = (x+w, y+h)
        pygame.draw.polygon(screen, SPIKE_COLOR, [point1, point2, point3])


    # ========== 绘制玩家小人 ==========
    pygame.draw.rect(screen, PLAYER_MAIN, (px, py, pw, ph), border_radius=8)
    pygame.draw.rect(screen, PLAYER_OUTLINE, (px, py, pw, ph), 2, border_radius=8)
    eye_y = py + 10
    pygame.draw.circle(screen, WHITE, (px+9, eye_y), 4)
    pygame.draw.circle(screen, WHITE, (px+23, eye_y), 4)
    pygame.draw.circle(screen, BLACK, (px+10, eye_y), 2)
    pygame.draw.circle(screen, BLACK, (px+24, eye_y), 2)

    # ========== UI文字面板 ==========
    panel_rect = pygame.Rect(8, 8, 170, 36)
    pygame.draw.rect(screen, (0,0,0,120), panel_rect, border_radius=6)
    pygame.draw.rect(screen, WHITE, panel_rect, 1, border_radius=6)
    level_text = small_font.render(f"关卡 {current_level+1}/{len(levels)}", True, WHITE)
    screen.blit(level_text, (15, 12))

    # 弹窗提示
    if game_complete:
        overlay = pygame.Surface((WIDTH, HEIGHT))
        overlay.set_alpha(140)
        overlay.fill(BLACK)
        screen.blit(overlay, (0,0))
        txt = font.render("全部通关！按R重新开始", True, YELLOW)
        screen.blit(txt, txt.get_rect(center=(WIDTH//2, HEIGHT//2)))
    elif win_level:
        overlay = pygame.Surface((WIDTH, HEIGHT))
        overlay.set_alpha(100)
        overlay.fill(BLACK)
        screen.blit(overlay, (0,0))
        txt = font.render("关卡完成！进入下一关...", True, WHITE)
        screen.blit(txt, txt.get_rect(center=(WIDTH//2, HEIGHT//2)))
    elif game_over:
        overlay = pygame.Surface((WIDTH, HEIGHT))
        overlay.set_alpha(100)
        overlay.fill(BLACK)
        screen.blit(overlay, (0,0))
        txt = font.render("失败！按R重试本关", True, RED)
        screen.blit(txt, txt.get_rect(center=(WIDTH//2, HEIGHT//2)))

    pygame.display.update()

pygame.quit()
sys.exit()