import pygame
import random

# 初始化
pygame.init()

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

clock = pygame.time.Clock()
FPS = 60

# 颜色常量
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (220, 30, 30)
GREEN = (30, 180, 30)

# 玩家参数
player_w = 30
player_h = 40
player_x = WIDTH // 2 - player_w // 2
player_y = HEIGHT - 120
player_vx = 0
player_vy = 0
gravity = 0.4
jump_power = -13
speed = 6


class Platform:
    def __init__(self):
        self.w = random.randint(60, 120)
        self.h = 16
        self.x = random.randint(0, WIDTH - self.w)
        self.y = 0

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


def reset_game():
    global player_x, player_y, player_vx, player_vy, platforms, score, game_over
    player_x = WIDTH // 2 - player_w // 2
    player_y = HEIGHT - 120
    player_vx = 0
    player_vy = 0
    platforms.clear()
    p0 = Platform()
    p0.w = 140
    p0.x = WIDTH // 2 - 70
    p0.y = HEIGHT - 80
    platforms.append(p0)
    for i in range(10):
        p = Platform()
        p.y = HEIGHT - 80 - i * 70
        platforms.append(p)
    score = 0
    game_over = False


platforms = []
score = 0
game_over = False
reset_game()

# ----------------关键修复----------------
# 使用 pygame 的内置默认字体文件，不要传None
try:
    font = pygame.font.Font(pygame.font.get_default_font(), 26)
except Exception:
    font = pygame.font.SysFont("arial", 26)


running = True
while running:
    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_SPACE:
                reset_game()

    if not game_over:
        keys = pygame.key.get_pressed()
        player_vx = 0
        if keys[pygame.K_LEFT]:
            player_vx = -speed
        if keys[pygame.K_RIGHT]:
            player_vx = speed

        # 重力
        player_vy += gravity
        player_x += player_vx
        player_y += player_vy

        # 左右环绕穿墙
        if player_x < -player_w:
            player_x = WIDTH
        if player_x > WIDTH:
            player_x = -player_w

        # 平台碰撞
        for plat in platforms:
            if player_vy > 0:
                if (player_y + player_h >= plat.y
                        and player_y + player_h <= plat.y + plat.h + 8
                        and player_x + player_w > plat.x
                        and player_x < plat.x + plat.w):
                    player_y = plat.y - player_h
                    player_vy = jump_power

        # 镜头向上滚动
        if player_y < HEIGHT * 0.35:
            offset = HEIGHT * 0.35 - player_y
            player_y = HEIGHT * 0.35
            for plat in platforms:
                plat.y += offset
            score += int(offset)

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

        # 生成新平台
        top_y = min(p.y for p in platforms)
        while top_y > -20:
            new_p = Platform()
            new_p.y = top_y - random.randint(55, 85)
            platforms.append(new_p)
            top_y = new_p.y

        # 掉落死亡
        if player_y > HEIGHT:
            game_over = True

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

    # 文字渲染
    score_surface = font.render(f"分数:{score}", True, WHITE)
    screen.blit(score_surface, (10, 10))

    if game_over:
        over_surface = font.render("游戏结束！按空格重新开始", True, WHITE)
        over_rect = over_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2))
        screen.blit(over_surface, over_rect)

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

pygame.quit()
