import pygame
import random
import sys

pygame.init()

# ========== 基础配置 ==========
WIDTH, HEIGHT = 400, 600
FPS = 60

GRAVITY = 0.5
JUMP = -12
SPEED = 5

SKY = (135, 206, 250)
GREEN = (0, 180, 0)
RED = (220, 20, 60)
BLUE = (30, 144, 255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

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

# ===== 字体修复：不用 SysFont 列表，直接取默认 =====
font = pygame.font.Font(None, 28)


# ========== 玩家 ==========
class Player:
    def __init__(self):
        self.w = 30
        self.h = 40
        self.x = WIDTH // 2 - self.w // 2
        self.y = HEIGHT - 100
        self.vy = 0
        self.alive = True

    def update(self, platforms):
        self.vy += GRAVITY
        self.y += self.vy

        if self.y > HEIGHT:
            self.alive = False

        for p in platforms:
            if (self.x + self.w > p.x and self.x < p.x + p.w and
                self.y + self.h > p.y and self.y + self.h < p.y + p.h + 12 and
                self.vy > 0):
                self.y = p.y - self.h
                self.vy = JUMP

    def move(self, dx):
        self.x += dx
        if self.x < 0:
            self.x = 0
        if self.x > WIDTH - self.w:
            self.x = WIDTH - self.w

    def draw(self, surf):
        pygame.draw.rect(surf, BLUE, (self.x, self.y, self.w, self.h))
        pygame.draw.circle(surf, (255, 220, 180), (self.x + self.w // 2, self.y + 8), 10)
        pygame.draw.circle(surf, BLACK, (self.x + self.w // 2 - 3, self.y + 7), 2)
        pygame.draw.circle(surf, BLACK, (self.x + self.w // 2 + 3, self.y + 7), 2)


# ========== 平台 ==========
class Platform:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.w = random.randint(60, 110)
        self.h = 20

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


# ========== 游戏 ==========
class Game:
    def __init__(self):
        self.reset()

    def reset(self):
        self.player = Player()
        self.platforms = []
        self.scroll = 0
        self.score = 0
        self.state = "start"

        p = Platform(WIDTH // 2 - 50, HEIGHT - 80)
        self.platforms.append(p)

        for i in range(15):
            self.spawn()

    def spawn(self):
        y = self.platforms[-1].y - random.randint(60, 90)
        x = random.randint(0, WIDTH - 110)
        self.platforms.append(Platform(x, y))

    def update(self):
        if self.state != "playing":
            return

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.player.move(-SPEED)
        if keys[pygame.K_RIGHT]:
            self.player.move(SPEED)

        self.player.update(self.platforms)

        if not self.player.alive:
            self.state = "over"
            return

        # 屏幕跟随
        if self.player.y < HEIGHT // 3:
            dy = HEIGHT // 3 - self.player.y
            self.player.y = HEIGHT // 3
            self.scroll += dy
            for p in self.platforms:
                p.y += dy
            self.score = int(self.scroll / 10)

        self.platforms = [p for p in self.platforms if p.y < HEIGHT + 50]
        while len(self.platforms) < 15:
            self.spawn()

    def draw(self, surf):
        surf.fill(SKY)

        for p in self.platforms:
            p.draw(surf)
        self.player.draw(surf)

        score_text = font.render(f"层数: {self.score}", True, BLACK)
        surf.blit(score_text, (10, 10))

        if self.state == "start":
            self.draw_start(surf)
        elif self.state == "over":
            self.draw_over(surf)

    def draw_start(self, surf):
        s = pygame.Surface((WIDTH, HEIGHT))
        s.fill((0, 0, 0))
        s.set_alpha(180)
        surf.blit(s, (0, 0))
        t = font.render("是男人就上一百层", True, WHITE)
        surf.blit(t, (WIDTH // 2 - t.get_width() // 2, 200))
        t2 = font.render("<- -> 移动   空格开始", True, WHITE)
        surf.blit(t2, (WIDTH // 2 - t2.get_width() // 2, 260))

    def draw_over(self, surf):
        s = pygame.Surface((WIDTH, HEIGHT))
        s.fill((0, 0, 0))
        s.set_alpha(200)
        surf.blit(s, (0, 0))
        t = font.render("GAME OVER", True, RED)
        surf.blit(t, (WIDTH // 2 - t.get_width() // 2, 200))
        t2 = font.render(f"层数: {self.score}", True, WHITE)
        surf.blit(t2, (WIDTH // 2 - t2.get_width() // 2, 250))
        t3 = font.render("空格重来   ESC退出", True, WHITE)
        surf.blit(t3, (WIDTH // 2 - t3.get_width() // 2, 300))

    def handle_key(self, key):
        if key == pygame.K_SPACE:
            if self.state == "start" or self.state == "over":
                self.reset()
                self.state = "playing"
        elif key == pygame.K_ESCAPE:
            return False
        return True


# ========== 主循环 ==========
game = Game()
running = True

while running:
    clock.tick(FPS)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if not game.handle_key(event.key):
                running = False

    game.update()
    game.draw(screen)
    pygame.display.flip()

pygame.quit()
sys.exit()