import pygame
import random

# -------------------------- 初始化设置 --------------------------
pygame.init()
WIDTH, HEIGHT = 480, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Jump Up 100 Floors")
clock = pygame.time.Clock()
FPS = 60

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 50, 50)
GREEN = (50, 200, 50)
BLUE = (50, 120, 255)

# -------------------------- 游戏对象类 --------------------------
class Player:
    def __init__(self):
        self.width = 30
        self.height = 30
        self.x = WIDTH // 2 - self.width // 2
        self.y = HEIGHT - 100
        self.vx = 0
        self.vy = 0
        self.speed = 6
        self.gravity = 0.4
        self.jump_power = -14
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def update(self, platforms, camera_y):
        keys = pygame.key.get_pressed()
        self.vx = 0
        if keys[pygame.K_LEFT]:
            self.vx = -self.speed
        if keys[pygame.K_RIGHT]:
            self.vx = self.speed
        self.x += self.vx

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

        # 重力
        self.vy += self.gravity
        self.y += self.vy

        # 平台碰撞
        for p in platforms:
            if self.vy > 0:
                if self.rect.colliderect(p.rect):
                    if self.rect.bottom - self.vy <= p.rect.top + 5:
                        self.y = p.rect.top - self.height
                        self.vy = self.jump_power

        self.rect.x = self.x
        self.rect.y = self.y - camera_y

    def draw(self):
        pygame.draw.rect(screen, RED, self.rect)


class Platform:
    def __init__(self, x, y, width=80):
        self.x = x
        self.y = y
        self.width = width
        self.height = 15
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def draw(self, camera_y):
        draw_rect = pygame.Rect(self.x, self.y - camera_y, self.width, self.height)
        pygame.draw.rect(screen, GREEN, draw_rect)


def generate_platforms(count, start_y):
    plats = []
    y = start_y
    for _ in range(count):
        w = random.randint(60, 100)
        x = random.randint(0, WIDTH - w)
        plats.append(Platform(x, y, w))
        y -= random.randint(60, 90)
    return plats


# -------------------------- 主游戏循环 --------------------------
def game_loop():
    player = Player()
    camera_y = 0
    platforms = generate_platforms(30, HEIGHT - 50)
    score = 0
    
    # ✅关键修复！使用内置默认字体文件，完全避开SysFont崩溃bug
    font = pygame.font.Font(pygame.font.get_default_font(), 30)
    
    game_over = False

    while True:
        screen.fill(BLUE)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            if event.type == pygame.KEYDOWN:
                if game_over and event.key == pygame.K_SPACE:
                    game_loop()
                    return

        if not game_over:
            player.update(platforms, camera_y)

            target_y = player.y - HEIGHT * 0.4
            if target_y < camera_y:
                camera_y = target_y
                score = max(score, -int(camera_y // 10))

            top_platform_y = min(p.y for p in platforms)
            while top_platform_y > camera_y - 200:
                new_w = random.randint(60, 100)
                new_x = random.randint(0, WIDTH - new_w)
                new_y = top_platform_y - random.randint(60, 90)
                platforms.append(Platform(new_x, new_y, new_w))
                top_platform_y = new_y

            platforms = [p for p in platforms if p.y - camera_y < HEIGHT + 50]

            if player.y - camera_y > HEIGHT:
                game_over = True

        for p in platforms:
            p.draw(camera_y)
        player.draw()

        # 分数文字(英文，无中文乱码方块)
        score_text = font.render(f"Height: {score}", True, WHITE)
        screen.blit(score_text, (10, 10))

        if game_over:
            over_text = font.render("Game Over! SPACE to restart", True, WHITE)
            screen.blit(over_text, (WIDTH//2 - 180, HEIGHT//2))

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


if __name__ == "__main__":
    game_loop()
