import pygame
import sys

pygame.init()

# =========================
# USTAWIENIA
# =========================
WIDTH, HEIGHT = 1000, 600
FPS = 60

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Mini Platformówka")
clock = pygame.time.Clock()

# Kolory
SKY = (135, 206, 235)
GREEN = (60, 180, 75)
DARK_GREEN = (35, 120, 50)
BROWN = (120, 75, 40)
RED = (220, 50, 50)
YELLOW = (255, 215, 0)
BLUE = (50, 100, 220)
WHITE = (255, 255, 255)
BLACK = (20, 20, 20)

font = pygame.font.Font(None, 42)
big_font = pygame.font.Font(None, 80)

# =========================
# ŚWIAT GRY
# =========================

platforms = [
    pygame.Rect(0, 550, 1000, 50),
    pygame.Rect(100, 450, 180, 25),
    pygame.Rect(350, 380, 180, 25),
    pygame.Rect(620, 450, 180, 25),
    pygame.Rect(780, 320, 150, 25),
    pygame.Rect(500, 250, 150, 25),
    pygame.Rect(200, 280, 160, 25),
]

coins = [
    pygame.Rect(160, 410, 20, 20),
    pygame.Rect(420, 340, 20, 20),
    pygame.Rect(690, 410, 20, 20),
    pygame.Rect(840, 280, 20, 20),
    pygame.Rect(560, 210, 20, 20),
    pygame.Rect(260, 240, 20, 20),
]

# Punkt końcowy
goal = pygame.Rect(900, 480, 35, 70)

# =========================
# GRACZ
# =========================

player = pygame.Rect(50, 490, 40, 60)

player_x = float(player.x)
player_y = float(player.y)

velocity_x = 0
velocity_y = 0

SPEED = 5
JUMP = -13
GRAVITY = 0.6

on_ground = False

# =========================
# PRZECIWNICY
# =========================

enemies = [
    {
        "rect": pygame.Rect(400, 320, 40, 40),
        "speed": 2,
        "direction": 1,
        "left": 350,
        "right": 530,
    },
    {
        "rect": pygame.Rect(680, 410, 40, 40),
        "speed": 2,
        "direction": -1,
        "left": 620,
        "right": 800,
    }
]

# =========================
# STAN GRY
# =========================

score = 0
lives = 3
game_over = False
win = False


def reset_player():
    global player_x, player_y, velocity_x, velocity_y

    player_x = 50
    player_y = 490
    velocity_x = 0
    velocity_y = 0


def draw_text(text, x, y, color=WHITE, font_obj=font):
    surface = font_obj.render(text, True, color)
    screen.blit(surface, (x, y))


def restart_game():
    global score, lives, game_over, win, coins

    score = 0
    lives = 3
    game_over = False
    win = False

    coins = [
        pygame.Rect(160, 410, 20, 20),
        pygame.Rect(420, 340, 20, 20),
        pygame.Rect(690, 410, 20, 20),
        pygame.Rect(840, 280, 20, 20),
        pygame.Rect(560, 210, 20, 20),
        pygame.Rect(260, 240, 20, 20),
    ]

    reset_player()


# =========================
# PĘTLA GRY
# =========================

while True:
    clock.tick(FPS)

    # -------------------------
    # EVENTY
    # -------------------------
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                sys.exit()

            if event.key == pygame.K_r:
                restart_game()

            if event.key in (pygame.K_SPACE, pygame.K_UP, pygame.K_w):
                if on_ground and not game_over and not win:
                    velocity_y = JUMP

    # -------------------------
    # GRA
    # -------------------------
    if not game_over and not win:

        keys = pygame.key.get_pressed()

        velocity_x = 0

        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            velocity_x = -SPEED

        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            velocity_x = SPEED

        # Ruch poziomy
        player_x += velocity_x
        player.x = int(player_x)

        # Kolizja ze ścianami
        if player.left < 0:
            player.left = 0
            player_x = player.x

        if player.right > WIDTH:
            player.right = WIDTH
            player_x = player.x

        # -------------------------
        # GRAWITACJA
        # -------------------------
        velocity_y += GRAVITY
        player_y += velocity_y
        player.y = int(player_y)

        on_ground = False

        # -------------------------
        # KOLIZJE Z PLATFORMAMI
        # -------------------------
        for platform in platforms:

            if player.colliderect(platform):

                # Spadanie na platformę
                if velocity_y > 0 and player.bottom <= platform.bottom + velocity_y:
                    player.bottom = platform.top
                    player_y = player.y
                    velocity_y = 0
                    on_ground = True

                # Uderzenie głową
                elif velocity_y < 0 and player.top >= platform.top:
                    player.top = platform.bottom
                    player_y = player.y
                    velocity_y = 0

        # -------------------------
        # MONETY
        # -------------------------
        for coin in coins[:]:
            if player.colliderect(coin):
                coins.remove(coin)
                score += 10

        # -------------------------
        # PRZECIWNICY
        # -------------------------
        for enemy in enemies:

            rect = enemy["rect"]

            rect.x += enemy["speed"] * enemy["direction"]

            if rect.left <= enemy["left"]:
                rect.left = enemy["left"]
                enemy["direction"] = 1

            if rect.right >= enemy["right"]:
                rect.right = enemy["right"]
                enemy["direction"] = -1

            # Kolizja z graczem
            if player.colliderect(rect):

                # Jeśli gracz spada na przeciwnika
                if velocity_y > 0 and player.bottom - velocity_y <= rect.top + 10:
                    velocity_y = JUMP
                    enemy["rect"].x = enemy["left"]

                else:
                    lives -= 1
                    reset_player()

                    if lives <= 0:
                        game_over = True

        # -------------------------
        # SPADNIĘCIE
        # -------------------------
        if player.top > HEIGHT:
            lives -= 1
            reset_player()

            if lives <= 0:
                game_over = True

        # -------------------------
        # META
        # -------------------------
        if player.colliderect(goal):
            win = True

    # =========================
    # RYSOWANIE
    # =========================

    screen.fill(SKY)

    # Chmury
    pygame.draw.circle(screen, WHITE, (150, 100), 35)
    pygame.draw.circle(screen, WHITE, (190, 100), 45)
    pygame.draw.circle(screen, WHITE, (235, 100), 30)

    pygame.draw.circle(screen, WHITE, (700, 120), 30)
    pygame.draw.circle(screen, WHITE, (740, 120), 40)
    pygame.draw.circle(screen, WHITE, (780, 120), 25)

    # Platformy
    for platform in platforms:
        pygame.draw.rect(screen, BROWN, platform)
        pygame.draw.rect(
            screen,
            GREEN,
            (platform.x, platform.y, platform.width, 7)
        )

    # Monety
    for coin in coins:
        pygame.draw.circle(
            screen,
            YELLOW,
            coin.center,
            10
        )
        pygame.draw.circle(
            screen,
            (255, 240, 100),
            coin.center,
            5
        )

    # Meta
    pygame.draw.rect(screen, BLACK, (goal.x, goal.y, 5, goal.height))
    pygame.draw.polygon(
        screen,
        RED,
        [
            (goal.x + 5, goal.y),
            (goal.x + 35, goal.y + 12),
            (goal.x + 5, goal.y + 24),
        ]
    )

    # Przeciwnicy
    for enemy in enemies:
        rect = enemy["rect"]

        pygame.draw.rect(screen, RED, rect)

        # Oczy
        pygame.draw.circle(screen, WHITE, (rect.x + 11, rect.y + 12), 6)
        pygame.draw.circle(screen, WHITE, (rect.x + 29, rect.y + 12), 6)

        pygame.draw.circle(screen, BLACK, (rect.x + 11, rect.y + 12), 3)
        pygame.draw.circle(screen, BLACK, (rect.x + 29, rect.y + 12), 3)

    # Gracz
    pygame.draw.rect(screen, BLUE, player)

    # Oczy gracza
    pygame.draw.circle(screen, WHITE, (player.x + 12, player.y + 15), 7)
    pygame.draw.circle(screen, WHITE, (player.x + 28, player.y + 15), 7)

    pygame.draw.circle(screen, BLACK, (player.x + 12, player.y + 15), 3)
    pygame.draw.circle(screen, BLACK, (player.x + 28, player.y + 15), 3)

    # UI
    draw_text(f"Monety: {score}", 20, 20)
    draw_text(f"Życia: {lives}", 20, 60)

    draw_text(
        "← → / A D = ruch | SPACJA = skok | R = restart",
        300,
        20,
        BLACK,
    )

    # -------------------------
    # GAME OVER
    # -------------------------
    if game_over:

        overlay = pygame.Surface((WIDTH, HEIGHT))
        overlay.set_alpha(180)
        overlay.fill(BLACK)
        screen.blit(overlay, (0, 0))

        text = big_font.render("KONIEC GRY", True, RED)
        screen.blit(
            text,
            (
                WIDTH // 2 - text.get_width() // 2,
                200,
            ),
        )

        draw_text(
            "Naciśnij R, aby zagrać ponownie",
            WIDTH // 2 - 200,
            300,
        )

    # -------------------------
    # WYGRANA
    # -------------------------
    if win:

        overlay = pygame.Surface((WIDTH, HEIGHT))
        overlay.set_alpha(180)
        overlay.fill((0, 100, 0))
        screen.blit(overlay, (0, 0))

        text = big_font.render("WYGRANA!", True, YELLOW)
        screen.blit(
            text,
            (
                WIDTH // 2 - text.get_width() // 2,
                200,
            ),
        )

        draw_text(
            f"Zdobyte monety: {score}",
            WIDTH // 2 - 130,
            300,
        )

        draw_text(
            "Naciśnij R, aby zagrać ponownie",
            WIDTH // 2 - 200,
            350,
        )

    pygame.display.flip()


























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































