import pygame
import random
import sys

# 初始化Pygame
pygame.init()

# 游戏设置
WIDTH = 600
HEIGHT = 800
FPS = 60

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 100, 255)
YELLOW = (255, 255, 0)
BROWN = (139, 69, 19)
GRAY = (128, 128, 128)

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("是男人就上100层")
clock = pygame.time.Clock()

# 玩家类


class Player:
    def __init__(self):
        self.width = 40
        self.height = 50
        self.x = WIDTH // 2 - self.width // 2
        self.y = HEIGHT - 100
        self.vel_y = 0
        self.vel_x = 0
        self.speed = 8
        self.jump_power = -15
        self.gravity = 0.8
        self.on_ground = False

    def move(self):
        keys = pygame.key.get_pressed()
        self.vel_x = 0

        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            self.vel_x = -self.speed
        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            self.vel_x = self.speed

        self.x += self.vel_x
        self.y += self.vel_y
        self.vel_y += self.gravity

        # 边界检测
        if self.x < 0:
            self.x = 0
        if self.x > WIDTH - self.width:
            self.x = WIDTH - self.width

    def jump(self):
        if self.on_ground:
            self.vel_y = self.jump_power
            self.on_ground = False

    def draw(self):
        # 绘制玩家角色
        pygame.draw.rect(
            screen, BLUE, (self.x, self.y, self.width, self.height))
        # 绘制眼睛
        pygame.draw.circle(screen, WHITE, (self.x + 10, self.y + 15), 5)
        pygame.draw.circle(screen, WHITE, (self.x + 30, self.y + 15), 5)
        pygame.draw.circle(screen, BLACK, (self.x + 10, self.y + 15), 2)
        pygame.draw.circle(screen, BLACK, (self.x + 30, self.y + 15), 2)

# 平台类


class Platform:
    def __init__(self, x, y, width=100, height=15):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.type = random.choice(['normal', 'moving', 'fragile'])
        self.move_speed = random.choice(
            [-2, 2]) if self.type == 'moving' else 0
        self.fragile_timer = 0

    def update(self):
        if self.type == 'moving':
            self.x += self.move_speed
            if self.x <= 0 or self.x + self.width >= WIDTH:
                self.move_speed = -self.move_speed

    def draw(self):
        if self.type == 'normal':
            color = GREEN
        elif self.type == 'moving':
            color = YELLOW
        else:
            color = RED

        pygame.draw.rect(
            screen, color, (self.x, self.y, self.width, self.height))
        pygame.draw.rect(screen, BLACK, (self.x, self.y,
                         self.width, self.height), 2)

# 游戏主类


class Game:
    def __init__(self):
        self.player = Player()
        self.platforms = []
        self.score = 0
        self.game_over = False
        self.camera_y = 0
        self.level = 1
        self.create_initial_platforms()

    def create_initial_platforms(self):
        # 创建初始平台
        for i in range(10):
            x = random.randint(0, WIDTH - 100)
            y = HEIGHT - (i * 80)
            self.platforms.append(Platform(x, y))

    def update(self):
        if not self.game_over:
            self.player.move()

            # 更新平台位置（模拟相机跟随）
            if self.player.y < HEIGHT // 2:
                offset = HEIGHT // 2 - self.player.y
                self.player.y += offset
                self.camera_y += offset

                for platform in self.platforms:
                    platform.y += offset

            # 检查碰撞
            self.player.on_ground = False
            for platform in self.platforms:
                platform.update()

                if (self.player.y + self.player.height >= platform.y and
                    self.player.y + self.player.height <= platform.y + platform.height + 10 and
                    self.player.vel_y > 0 and
                    self.player.x + self.player.width > platform.x and
                        self.player.x < platform.x + platform.width):

                    if platform.type == 'fragile':
                        platform.fragile_timer += 1
                        if platform.fragile_timer > 30:
                            self.platforms.remove(platform)
                            continue

                    self.player.y = platform.y - self.player.height
                    self.player.vel_y = 0
                    self.player.on_ground = True

            # 自动跳跃
            if self.player.on_ground:
                self.player.jump()

            # 生成新平台
            while len(self.platforms) < 15:
                x = random.randint(0, WIDTH - 100)
                y = self.platforms[-1].y - random.randint(60, 100)
                self.platforms.append(Platform(x, y))

            # 移除屏幕外的平台
            self.platforms = [p for p in self.platforms if p.y < HEIGHT + 100]

            # 更新分数
            self.score = max(self.score, self.camera_y // 80)
            self.level = 1 + self.score // 10

            # 检查游戏结束
            if self.player.y > HEIGHT + 100:
                self.game_over = True

    def draw(self):
        screen.fill(BLACK)

        # 绘制平台
        for platform in self.platforms:
            if -100 < platform.y < HEIGHT + 100:
                platform.draw()

        # 绘制玩家
        self.player.draw()

        # 绘制UI
        font = pygame.font.Font(None, 36)
        score_text = font.render(f"层数: {int(self.score)}", True, WHITE)
        level_text = font.render(f"难度: {self.level}", True, WHITE)
        screen.blit(score_text, (10, 10))
        screen.blit(level_text, (10, 50))

        if self.game_over:
            game_over_text = font.render("游戏结束！按R重新开始", True, RED)
            text_rect = game_over_text.get_rect(center=(WIDTH//2, HEIGHT//2))
            screen.blit(game_over_text, text_rect)

        pygame.display.flip()

# 主循环


def main():
    game = Game()
    running = True

    while running:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    game.player.jump()
                if event.key == pygame.K_r and game.game_over:
                    game = Game()

        game.update()
        game.draw()

    pygame.quit()
    sys.exit()


if __name__ == "__main__":
    main()
