import pygame
import random

# 初始化Pygame
pygame.init()

WIDTH = 480
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("是男人就上100层")
clock = pygame.time.Clock()
FPS = 60

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 60, 60)
GREEN = (40, 200, 80)
BLUE = (30, 144, 255)
GRAY = (120, 120, 120)

# 兼容系统默认字体，避免simhei缺失闪退
try:
    font_num = pygame.font.SysFont("simhei", 32)
    font_tip = pygame.font.SysFont("simhei", 24)
except:
    font_num = pygame.font.SysFont(None, 32)
    font_tip = pygame.font.SysFont(None, 24)

# 玩家属性
player_w = 36
player_h = 44
player_x = WIDTH // 2 - player_w // 2
player_y = HEIGHT - 100
speed_x = 6
gravity = 0.4
jump_power = -11
vel_y = 0

# 平台类
class Platform:
    def __init__(self, y_pos):
        self.w = random.randint(65, 110)
        self.h = 16
        self.x = random.randint(0, WIDTH - self.w)
        self.y = y_pos

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

platforms = []
# 出生平台
start_plat = Platform(HEIGHT - 60)
start_plat.x = WIDTH // 2 - start_plat.w // 2
platforms.append(start_plat)

# 初始平台固定间距生成
init_gap = 75
for i in range(12):
    new_p = Platform(HEIGHT - 60 - i * init_gap)
    platforms.append(new_p)

layer = 0
game_over = False

def reset_game():
    """重置游戏，彻底清空重建，防止列表异常"""
    global player_x, player_y, vel_y, platforms, layer, game_over
    player_x = WIDTH // 2 - player_w // 2
    player_y = HEIGHT - 100
    vel_y = 0
    layer = 0
    game_over = False
    platforms.clear()
    start_plat = Platform(HEIGHT - 60)
    start_plat.x = WIDTH // 2 - start_plat.w // 2
    platforms.append(start_plat)
    gap = 75
    for i in range(12):
        new_p = Platform(HEIGHT - 60 - i * gap)
        platforms.append(new_p)

running = True
while running:
    screen.fill(WHITE)
    clock.tick(FPS)

    # 事件捕获
    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:
                reset_game()

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

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

        # 重力下落
        vel_y += gravity
        player_y += vel_y

        # 踩踏平台跳跃
        for p in platforms:
            if vel_y > 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 + 8):
                    player_y = p.y - player_h
                    vel_y = jump_power

        # 镜头向上滚动
        if player_y < HEIGHT / 3:
            offset = HEIGHT / 3 - player_y
            player_y = HEIGHT / 3
            layer += int(offset)

            # 全体平台下移
            for p in platforms:
                p.y += offset

            # 移除超出屏幕下方的平台
            platforms = [p for p in platforms if p.y < HEIGHT]

            # 安全判断：必须有平台才生成新的，防止min()报错退出
            if platforms:
                top_y = min(p.y for p in platforms)
                new_y = top_y - random.randint(70, 100)
                platforms.append(Platform(new_y))

        # 掉落出底部判定游戏结束
        if player_y > HEIGHT:
            game_over = True

    # 绘制所有平台
    for p in platforms:
        p.draw()

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

    # 显示层数
    text_layer = font_num.render(f"层数：{layer}", True, BLACK)
    screen.blit(text_layer, (10, 10))

    # 通关提示
    if layer >= 100:
        win_text = font_tip.render("恭喜！成功上100层！", True, GREEN)
        screen.blit(win_text, (WIDTH//2 - win_text.get_width()//2, HEIGHT//2))

    # 游戏结束界面
    if game_over:
        over_text = font_num.render("游戏结束", True, RED)
        tip_text = font_tip.render("按空格键重新开始", True, GRAY)
        screen.blit(over_text, (WIDTH//2 - over_text.get_width()//2, HEIGHT//2 - 40))
        screen.blit(tip_text, (WIDTH//2 - tip_text.get_width()//2, HEIGHT//2 + 10))

    pygame.display.update()

pygame.quit()
