import pygame
import random
import sys

pygame.init()

WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("是男人上一百层")
clock = pygame.time.Clock()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 100, 255)
GRAY = (150, 150, 150)

PLAYER_WIDTH = 30
PLAYER_HEIGHT = 30
player_x = WIDTH // 2 - PLAYER_WIDTH // 2
player_y = HEIGHT - 100
player_speed = 5
player_vy = 0
gravity = 0.5
jump_strength = -10

PLATFORM_WIDTH = 70
PLATFORM_HEIGHT = 15
platforms = []

# ========== 修复字体 ==========
def get_font(size):
    # 按顺序尝试常见中文字体
    font_names = [
        "SimHei",
        "Microsoft YaHei",
        "PingFang SC",
        "Noto Sans CJK SC",
        "SimSun",
        "STHeiti",
    ]
    for name in font_names:
        try:
            return pygame.font.SysFont(name, size)
        except:
            continue
    # 如果都失败，用默认字体（英文）
    return pygame.font.Font(None, size)

font = get_font(30)      # 显示分数的字体
big_font = get_font(40)  # 显示游戏结束的字体
# ============================

def create_platform(x, y, color=GRAY):
    return pygame.Rect(x, y, PLATFORM_WIDTH, PLATFORM_HEIGHT), color

def init_platforms():
    global platforms
    platforms = []
    platforms.append(create_platform(WIDTH // 2 - PLATFORM_WIDTH // 2, HEIGHT - 40, BLUE))
    for i in range(10):
        x = random.randint(0, WIDTH - PLATFORM_WIDTH)
        y = HEIGHT - 100 - i * 60
        color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
        platforms.append(create_platform(x, y, color))

init_platforms()

player = pygame.Rect(player_x, player_y, PLAYER_WIDTH, PLAYER_HEIGHT)
score = 0
game_over = False

def reset_game():
    global player, platforms, score, game_over, player_vy
    player.x = WIDTH // 2 - PLAYER_WIDTH // 2
    player.y = HEIGHT - 100
    player_vy = 0
    score = 0
    game_over = False
    init_platforms()

running = True
while running:
    clock.tick(60)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r and game_over:
                reset_game()
    
    if not game_over:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            player.x -= player_speed
        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            player.x += player_speed
        
        if player.x < 0:
            player.x = 0
        if player.x + PLAYER_WIDTH > WIDTH:
            player.x = WIDTH - PLAYER_WIDTH
        
        player_vy += gravity
        player.y += player_vy
        
        on_platform = False
        for plat, color in platforms:
            if player.colliderect(plat) and player_vy >= 0:
                if player.bottom - player_vy <= plat.top + 10:
                    player.bottom = plat.top
                    player_vy = jump_strength
                    on_platform = True
                    score += 1
                    break
        
        if player.top > HEIGHT:
            game_over = True
        
        if player.y < HEIGHT // 3:
            diff = HEIGHT // 3 - player.y
            player.y += diff
            for i, (plat, color) in enumerate(platforms):
                new_plat = plat.move(0, diff)
                platforms[i] = (new_plat, color)
                if new_plat.top > HEIGHT:
                    new_x = random.randint(0, WIDTH - PLATFORM_WIDTH)
                    new_y = random.randint(-PLATFORM_HEIGHT, 0)
                    new_color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
                    platforms[i] = (pygame.Rect(new_x, new_y, PLATFORM_WIDTH, PLATFORM_HEIGHT), new_color)
    
    # ---- 绘制 ----
    screen.fill(BLACK)
    
    for plat, color in platforms:
        pygame.draw.rect(screen, color, plat)
        pygame.draw.line(screen, WHITE, plat.topleft, plat.topright, 2)
    
    pygame.draw.rect(screen, RED, player)
    pygame.draw.circle(screen, WHITE, (player.x + 10, player.y + 8), 4)
    pygame.draw.circle(screen, WHITE, (player.x + 20, player.y + 8), 4)
    pygame.draw.circle(screen, BLACK, (player.x + 10, player.y + 8), 2)
    pygame.draw.circle(screen, BLACK, (player.x + 20, player.y + 8), 2)
    
    # 使用修复后的字体显示
    score_text = font.render(f"层数: {score}", True, WHITE)
    screen.blit(score_text, (10, 10))
    
    if game_over:
        overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
        overlay.fill((0, 0, 0, 180))
        screen.blit(overlay, (0, 0))
        over_text = big_font.render("游戏结束", True, RED)
        restart_text = font.render("按 R 重新开始", True, WHITE)
        screen.blit(over_text, (WIDTH // 2 - over_text.get_width() // 2, HEIGHT // 2 - 40))
        screen.blit(restart_text, (WIDTH // 2 - restart_text.get_width() // 2, HEIGHT // 2 + 10))
    
    pygame.display.flip()

pygame.quit()
sys.exit()
