import pygame
import random

# 初始化
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("男人上一百层 - 完美可玩")

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 70, 70)
BLUE = (80, 180, 255)
YELLOW = (255, 255, 0)

# 玩家类


class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((35, 55))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH // 2
        self.rect.bottom = HEIGHT - 50

        self.speed_x = 0
        self.speed_y = 0
        self.gravity = 0.8
        self.jump_power = -18  # 跳跃力度

    def update(self):
        # 重力
        self.speed_y += self.gravity
        # 水平移动
        self.rect.x += self.speed_x

        # 边界限制
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH

        self.rect.y += self.speed_y

# 平台类


class Platform(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        width = random.randint(90, 140)
        self.image = pygame.Surface((width, 15))
        self.image.fill(BLUE)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

# 创建平台


def create_platforms():
    platforms = pygame.sprite.Group()
    platforms.add(Platform(WIDTH//2 - 70, HEIGHT - 20))

    for i in range(12):
        x = random.randint(0, WIDTH - 120)
        y = random.randint(-HEIGHT, HEIGHT - 50)
        platforms.add(Platform(x, y))
    return platforms

# 主游戏


def game():
    player = Player()
    platforms = create_platforms()
    all_sprites = pygame.sprite.Group(player, *platforms)

    score = 0
    clock = pygame.time.Clock()
    game_over = False
    font = pygame.font.SysFont(None, 45)

    while True:
        clock.tick(60)
        screen.fill(BLACK)

        # 退出事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return

        if not game_over:
            # 移动控制
            keys = pygame.key.get_pressed()
            player.speed_x = 0
            if keys[pygame.K_LEFT]:
                player.speed_x = -7
            if keys[pygame.K_RIGHT]:
                player.speed_x = 7

            # 更新角色
            player.update()

            # 碰撞检测（正常跳跃核心代码）
            hits = pygame.sprite.spritecollide(player, platforms, False)
            for plat in hits:
                if player.speed_y > 0 and player.rect.bottom < plat.rect.bottom:
                    player.rect.bottom = plat.rect.top
                    player.speed_y = player.jump_power

            # 屏幕滚动
            scroll = 0
            if player.rect.top < HEIGHT // 3:
                scroll = HEIGHT // 3 - player.rect.top
                player.rect.top += scroll
                score += scroll

                for p in platforms:
                    p.rect.y += scroll

            # 生成新平台
            while len(platforms) < 15:
                x = random.randint(0, WIDTH - 120)
                new_p = Platform(x, random.randint(-80, -20))
                platforms.add(new_p)
                all_sprites.add(new_p)

            # 清理旧平台
            for p in platforms:
                if p.rect.top > HEIGHT:
                    p.kill()

            # 死亡
            if player.rect.top > HEIGHT:
                game_over = True

        # 绘制
        all_sprites.draw(screen)

        # 分数
        score_txt = font.render(f"层数: {score//10}", True, YELLOW)
        screen.blit(score_txt, (15, 15))

        # 游戏结束
        if game_over:
            go_txt = font.render("游戏结束!", True, WHITE)
            screen.blit(go_txt, (WIDTH//2 - 70, HEIGHT//2))

        pygame.display.flip()


if __name__ == "__main__":
    game()
