import pygame
import random

# ===== Init =====
pygame.init()
WIDTH, HEIGHT = 480, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Only Real Men Climb 100 Floors")

# ===== Colors =====
BG = (15, 15, 25)
PLAYER = (220, 200, 80)
PLATFORM = (90, 180, 90)
TEXT = (230, 230, 230)
RED = (220, 80, 80)

FONT = pygame.font.Font(None, 36)
FONT_BIG = pygame.font.Font(None, 54)

# ===== Player =====
player = pygame.Rect(WIDTH // 2 - 30, HEIGHT - 150, 60, 60)
velocity_y = 0
gravity = 0.6
jump_power = -14

# ===== Platforms =====
platforms = []
for i in range(10):
    platforms.append(pygame.Rect(random.randint(50, WIDTH - 110), i * 120 + 200, 80, 20))

floor = 0
game_over = False

clock = pygame.time.Clock()
running = True

# ===== Main Loop =====
while running:
    screen.fill(BG)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if game_over and event.key == pygame.K_r:
                # Restart
                player.y = HEIGHT - 150
                velocity_y = 0
                platforms.clear()
                for i in range(10):
                    platforms.append(
                        pygame.Rect(random.randint(50, WIDTH - 110), i * 120 + 200, 80, 20)
                    )
                floor = 0
                game_over = False

            if not game_over and event.key == pygame.K_SPACE and velocity_y > 4:
                velocity_y = jump_power

    if not game_over:
        # Movement
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            player.x -= 6
        if keys[pygame.K_RIGHT]:
            player.x += 6

        velocity_y += gravity
        player.y += velocity_y

        # Screen wrap
        if player.left > WIDTH:
            player.right = 0
        if player.right < 0:
            player.left = WIDTH

        # Jump on platform
        for plat in platforms:
            if player.colliderect(plat) and velocity_y > 0:
                if player.bottom <= plat.top + 15:
                    velocity_y = jump_power
                    floor += 1

        # Scroll
        if player.top < HEIGHT // 3:
            player.y += 5
            for plat in platforms:
                plat.y += 5
                if plat.top > HEIGHT:
                    platforms.remove(plat)
                    platforms.append(
                        pygame.Rect(random.randint(50, WIDTH - 110), -20, 80, 20)
                    )

        # Game over
        if player.top > HEIGHT:
            game_over = True

    # ===== Draw =====
    for plat in platforms:
        pygame.draw.rect(screen, PLATFORM, plat, border_radius=6)

    pygame.draw.rect(screen, PLAYER, player, border_radius=10)

    floor_text = FONT.render(f"Floor: {floor}", True, TEXT)
    screen.blit(floor_text, (20, 20))

    if game_over:
        over_text = FONT_BIG.render("GAME OVER", True, RED)
        restart_text = FONT.render("Press R to Restart", True, TEXT)
        screen.blit(over_text, over_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 30)))
        screen.blit(restart_text, restart_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 20)))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()