import pygame
import random
import sys
import os

# 初始化pygame
pygame.init()

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

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 50, 50)
GREEN = (40, 180, 40)
BLUE = (30, 120, 220)
GRAY = (100, 100, 100)

# 物理常量
GRAVITY = 0.6
JUMP_POWER = -16
PLAYER_SPEED = 6

# 玩家类
class Player:
    def __init__(self):
        self.width = 30
        self.height = 40
        self.x = WIDTH // 2 - self.width // 2
        self.y = HEIGHT - 100
        self.vx = 0
        self.vy = 0
        self.on_ground = False

    def update(self, platforms):
        # 重力
        self.vy += 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

        self.on_ground = False
        player_rect = pygame.Rect(self.x, self.y, self.width, self.height)
        # 碰撞检测
        for plat in platforms:
            plat_rect = pygame.Rect(plat.x, plat.y, plat.w, plat.h)
            if player_rect.colliderect(plat_rect) and self.vy > 0:
                self.y = plat.y - self.height
                self.vy = 0
                self.on_ground = True

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

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

# 平台类
class Platform:
    def __init__(self, x, y, w=80, 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 create_platforms():
    plats = []
    # 底部起始大平台
    plats.append(Platform(WIDTH//2 - 60, HEIGHT - 60, 120, 16))
    y = HEIGHT - 140
    for _ in range(12):
        x = random.randint(20, WIDTH - 100)
        plats.append(Platform(x, y))
        y -= random.randint(70, 120)
    return plats

def main():
    clock = pygame.time.Clock()
    player = Player()
    platforms = create_platforms()
    camera_y = 0
    highest_y = player.y
    game_over = False

    # ==========修复字体部分==========
    try:
        font = pygame.font.SysFont("simhei", 28)
    except Exception:
        # 黑体加载失败，使用通用默认字体，支持英文显示
        font = pygame.font.Font(None, 28)

    while True:
        screen.fill(BLACK)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
                    if not game_over:
                        player.jump()
                if event.key == pygame.K_r and game_over:
                    # 重新开始
                    player = Player()
                    platforms = create_platforms()
                    camera_y = 0
                    highest_y = player.y
                    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(platforms)

            # 相机跟随向上
            if player.y < HEIGHT // 2:
                diff = HEIGHT // 2 - player.y
                camera_y += diff
                player.y = HEIGHT // 2
                # 移动所有平台
                for p in platforms:
                    p.y += diff

            # 清除屏幕下方平台
            platforms = [p for p in platforms if p.y < HEIGHT + 20]

            # 生成顶部新平台
            top_plat_y = min(p.y for p in platforms)
            while top_plat_y > -50:
                new_y = top_plat_y - random.randint(70, 120)
                new_x = random.randint(10, WIDTH - 90)
                platforms.append(Platform(new_x, new_y))
                top_plat_y = new_y

            # 掉落判定
            if player.y > HEIGHT + 50:
                game_over = True

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

        # 文字渲染（字体异常时中文会变成方框，英文正常）
        height_score = int((highest_y - player.y) / 10)
        try:
            text = font.render(f"高度: {height_score}", True, WHITE)
            screen.blit(text, (10, 10))
        except:
            pass

        if game_over:
            try:
                over_text = font.render("Game Over! Press R to Restart", True, RED)
                screen.blit(over_text, (WIDTH//2 - 150, HEIGHT//2))
            except:
                pass

        pygame.display.flip()
        clock.tick(60)

if __name__ == "__main__":
    main()