import pygame
import random

# 初始化
pygame.init()

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

# 颜色
WHITE = 255, 255, 255
BLACK = 0, 0, 0
RED = 255, 0, 0
GREEN = 0, 255, 0
BLUE = 0, 120, 255
GRAY = 100, 100, 100

# 帧率
clock = pygame.time.Clock()
FPS = 60

# 玩家属性
player_w = 30
player_h = 40
player_x = WIDTH // 2 - player_w // 2
player_y = HEIGHT - 100
speed_x = 5
gravity = 0.6
jump_power = -15
vy = 0
is_jump = False

# 楼层类
class Platform:
    def __init__(self, x, y):
        self.w = 80
        self.h = 15
        self.x = x
        self.y = y

# 生成初始楼层
platforms = []
# 底部固定楼层
platforms.append(Platform(WIDTH//2 - 40, HEIGHT - 60))
# 随机生成上面楼层
for i in range(10):
    x = random.randint(20, WIDTH - 100)
    y = HEIGHT - 60 - i * 70
    platforms.append(Platform(x, y))

# 分数
score = 0
font = pygame.font.SysFont("SimHei", 28)
game_over = False

# 游戏主循环
running = True
while running:
    clock.tick(FPS)
    screen.fill(WHITE)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        # 游戏结束按空格重启
        if game_over and event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                # 重置所有数据
                player_x = WIDTH // 2 - player_w // 2
                player_y = HEIGHT - 100
                vy = 0
                score = 0
                platforms.clear()
                platforms.append(Platform(WIDTH//2 - 40, HEIGHT - 60))
                for i in range(10):
                    x = random.randint(20, WIDTH - 100)
                    y = HEIGHT - 60 - i * 70
                    platforms.append(Platform(x, y))
                game_over = False

    if not game_over:
        # 左右移动
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and player_x > 0:
            player_x -= speed_x
        if keys[pygame.K_RIGHT] and player_x < WIDTH - player_w:
            player_x += speed_x

        # 重力
        vy += gravity
        player_y += vy

        # 碰撞楼层 & 跳跃
        for p in platforms:
            if vy > 0:
                if (player_x + player_w > p.x and
                    player_x < p.x + p.w and
                    player_y + player_h > p.y and
                    player_y + player_h < p.y + p.h + 10):
                    player_y = p.y - player_h
                    vy = jump_power

        # 视角上移（往上爬）
        if player_y < HEIGHT // 2:
            diff = HEIGHT // 2 - player_y
            player_y = HEIGHT // 2
            score += int(diff)
            # 所有楼层下移
            for p in platforms:
                p.y += diff
            # 删除出屏幕的楼层
            platforms = [p for p in platforms if p.y < HEIGHT]
            # 顶部生成新楼层
            new_x = random.randint(20, WIDTH - 100)
            new_y = random.randint(-20, 50)
            platforms.append(Platform(new_x, new_y))

        # 掉落游戏结束
        if player_y > HEIGHT:
            game_over = True

    # 绘制楼层
    for p in platforms:
        pygame.draw.rect(screen, GRAY, (p.x, p.y, p.w, p.h))

    # 绘制玩家
    pygame.draw.rect(screen, BLUE, (player_x, player_y, player_w, player_h))

    # 绘制分数
    score_text = font.render(f"分数: {score}", True, BLACK)
    screen.blit(score_text, (10, 10))

    # 游戏结束提示
    if game_over:
        over_text = font.render("游戏结束！按空格重新开始", True, RED)
        screen.blit(over_text, (WIDTH//2 - 160, HEIGHT//2))

    pygame.display.update()

pygame.quit()