import pygame
import random

# 初始化
pygame.init()
# 必须初始化font模块（保险加上）
pygame.font.init()
WIDTH, HEIGHT = 480, 700
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, 60, 60)
GREEN = (40, 180, 40)
BLUE = (30, 120, 220)

# 玩家类
class Player:
    def __init__(self):
        self.width = 30
        self.height = 40
        self.x = WIDTH // 2 - self.width // 2
        self.y = HEIGHT - 120
        self.vx = 0
        self.vy = 0
        self.speed = 6
        self.gravity = 0.35
        self.jump_power = -11

    def update(self):
        # 重力
        self.vy += self.gravity
        self.x += self.vx
        self.y += self.vy

        # 左右边界穿墙
        if self.x < -self.width:
            self.x = WIDTH
        if self.x > WIDTH:
            self.x = -self.width

    def jump(self):
        self.vy = self.jump_power

    def draw(self):
        pygame.draw.rect(screen, BLUE, (self.x, self.y, self.width, self.height))

# 平台类
class Platform:
    def __init__(self, x, y, w=70, h=14):
        self.x = x
        self.y = y
        self.w = w
        self.h = h

    def draw(self):
        pygame.draw.rect(screen, GREEN, (self.x, self.y, self.w, self.h))

    def shift(self, dy):
        self.y += dy

# 生成初始平台
def create_platforms():
    plats = []
    # 起始大平台
    plats.append(Platform(WIDTH//2 - 50, HEIGHT - 80, 100, 14))
    y = HEIGHT - 160
    while y > -100:
        px = random.randint(10, WIDTH - 80)
        plats.append(Platform(px, y))
        y -= random.randint(70, 120)
    return plats

player = Player()
platforms = create_platforms()
score = 0
# ==========修复字体行==========
font = pygame.font.Font(pygame.font.get_default_font(), 36)
game_over = False

running = True
while running:
    clock.tick(60)
    screen.fill(BLACK)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if game_over and event.key == pygame.K_r:
                # 重新开局
                player = Player()
                platforms = create_platforms()
                score = 0
                game_over = False

    if not game_over:
        # 左右移动控制
        keys = pygame.key.get_pressed()
        player.vx = 0
        if keys[pygame.K_LEFT]:
            player.vx = -player.speed
        if keys[pygame.K_RIGHT]:
            player.vx = player.speed

        player.update()

        # 平台碰撞，只有下落时才能踩平台
        for p in platforms:
            if (player.vy > 0
                    and player.x + player.width > p.x
                    and player.x < p.x + p.w
                    and player.y + player.height > p.y
                    and player.y + player.height < p.y + p.h + 8):
                player.jump()

        # 镜头上移：玩家到达屏幕上半部分，整体下移场景
        camera_shift = 0
        if player.y < HEIGHT / 3:
            camera_shift = HEIGHT / 3 - player.y
            player.y = HEIGHT / 3
            score += int(camera_shift)
            for plat in platforms:
                plat.shift(camera_shift)

        # 删除屏幕下方平台，同时生成上方新平台
        new_platforms = []
        top_y = min(plat.y for plat in platforms)
        for plat in platforms:
            if plat.y < HEIGHT + 20:
                new_platforms.append(plat)
        platforms = new_platforms
        # 持续生成高处平台
        while top_y > -50:
            new_x = random.randint(10, WIDTH - 80)
            gap = random.randint(70, 120)
            top_y -= gap
            platforms.append(Platform(new_x, top_y))

        # 掉落判定游戏结束
        if player.y > HEIGHT:
            game_over = True

    # 绘制所有平台
    for plat in platforms:
        plat.draw()
    player.draw()

    # 绘制分数
    text = font.render(f"高度: {score}", True, WHITE)
    screen.blit(text, (10, 10))

    if game_over:
        over_text = font.render("游戏结束！按 R 重新开始", True, RED)
        screen.blit(over_text, (WIDTH//2 - 180, HEIGHT//2))

    pygame.display.flip()

pygame.quit()