import pygame
import random

# 初始化Pygame
pygame.init()

# 游戏窗口设置
WIDTH, HEIGHT = 480, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("是男人就上一百层")

# 颜色定义
WHITE = (255, 255, 255)
BLUE = (0, 120, 215)
RED = (255, 60, 60)
BLACK = (0, 0, 0)

# 玩家参数（优化跳跃手感）
player_size = 30
player_x = WIDTH // 2 - player_size // 2
player_y = HEIGHT // 2
player_speed = 7
jump_speed = -18   # 跳更高
gravity = 0.6      # 下落更慢
player_vel_y = 0

# 楼层参数（缩短间距、加宽平台更好踩）
platform_width = 95
platform_height = 15
platforms = []

score = 0
max_floor = 0
game_over = False

font = pygame.font.SysFont(None, 40)
small_font = pygame.font.SysFont(None, 30)

# 生成密集楼层
def create_platforms():
    platforms.clear()
    platforms.append([WIDTH//2 - platform_width//2, HEIGHT-40, platform_width, platform_height])
    # 生成更多、间距更小的平台
    for _ in range(15):
        x = random.randint(0, WIDTH - platform_width)
        y = random.randint(-200, HEIGHT - 80)
        platforms.append([x, y, platform_width, platform_height])

create_platforms()

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

while running:
    screen.fill(WHITE)
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if game_over and event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                player_x = WIDTH//2 - player_size//2
                player_y = HEIGHT//2
                player_vel_y = 0
                score = 0
                max_floor = 0
                game_over = False
                create_platforms()
            if event.key == pygame.K_q:
                running = False

    if not game_over:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player_x > 0:
            player_x -= player_speed
        if keys[pygame.K_RIGHT] and player_x < WIDTH - player_size:
            player_x += player_speed

        # 物理运动
        player_vel_y += gravity
        player_y += player_vel_y

        # 碰撞跳跃
        player_rect = pygame.Rect(player_x, player_y, player_size, player_size)
        for plat in platforms:
            plat_rect = pygame.Rect(*plat)
            if player_rect.colliderect(plat_rect) and player_vel_y > 0:
                player_vel_y = jump_speed
                score += 1

        # 镜头上移
        if player_y < HEIGHT // 2:
            offset = HEIGHT // 2 - player_y
            player_y = HEIGHT // 2
            for p in platforms:
                p[1] += offset
            current_floor = score // 6
            if current_floor > max_floor:
                max_floor = current_floor

        # 刷新下方平台，保证密集
        for p in platforms:
            if p[1] > HEIGHT:
                p[0] = random.randint(0, WIDTH - platform_width)
                p[1] = random.randint(-80, -20)  # 生成距离更近

        if player_y > HEIGHT:
            game_over = True

    # 绘制
    for p in platforms:
        pygame.draw.rect(screen, BLUE, p)
    pygame.draw.rect(screen, RED, (player_x, player_y, player_size, player_size))

    screen.blit(font.render(f"楼层：{max_floor}", True, BLACK), (10, 10))
    if game_over:
        screen.blit(font.render("游戏结束！", True, RED), (WIDTH//2-80, HEIGHT//2-40))
        screen.blit(small_font.render("R重玩 Q退出", True, BLACK), (WIDTH//2-90, HEIGHT//2+10))

    pygame.display.update()

pygame.quit()