### Gra: Spadające gwiazdy

import pygame
import random
import sys

# --- Inicjalizacja pygame ---
pygame.init()

# --- Ustawienie okna gry ---
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("🌟 Złap spadające gwiazdki!")

# --- Kolory (RGB) ---
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (30, 144, 255)
YELLOW = (255, 215, 0)
RED = (255, 0, 0)

# --- Ustawienia koszyka ---
basket_width = 100
basket_height = 30
basket_x = WIDTH // 2
basket_y = HEIGHT - 60
basket_speed = 10

# --- Ustawienia gwiazdki ---
star_size = 20
star_x = random.randint(0, WIDTH - star_size)
star_y = -20
star_speed = 5

# --- Punkty i życia ---
score = 0
lives = 3

# --- Czcionka ---
font = pygame.font.SysFont("Arial", 30)

# --- Zegar gry ---
clock = pygame.time.Clock()
FPS = 60

# --- Główna pętla gry ---
running = True

while running:
    # --- Obsługa zdarzeń (zamykanie okna itd.) ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False  # wyjdź z pętli

    # --- Ruch koszyka (klawisze lewo/prawo) ---
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and basket_x > 0:
        basket_x -= basket_speed
    if keys[pygame.K_RIGHT] and basket_x < WIDTH - basket_width:
        basket_x += basket_speed

    # --- Aktualizacja pozycji gwiazdy (spada w dół) ---
    star_y += star_speed

    # --- Sprawdzenie kolizji (czy gwiazdka dotknęła kosza) ---
    if (basket_y < star_y + star_size and
        basket_y + basket_height > star_y and
        basket_x < star_x + star_size and
        basket_x + basket_width > star_x):
        score += 1  # dodaj punkt
        star_y = -20  # zresetuj pozycję gwiazdy
        star_x = random.randint(0, WIDTH - star_size)
        star_speed += 0.3  # przyspiesz grę

    # --- Sprawdzenie, czy gwiazda spadła poza ekran ---
    if star_y > HEIGHT:
        lives -= 1
        star_y = -20
        star_x = random.randint(0, WIDTH - star_size)
        if lives == 0:
            running = False  # koniec gry

    # --- Rysowanie na ekranie ---
    screen.fill(BLACK)
    pygame.draw.rect(screen, BLUE, (basket_x, basket_y, basket_width, basket_height))  # koszyk
    pygame.draw.rect(screen, YELLOW, (star_x, star_y, star_size, star_size))  # gwiazdka

    # Wynik i życia
    score_text = font.render(f"Punkty: {score}", True, WHITE)
    lives_text = font.render(f"Życia: {lives}", True, RED)
    screen.blit(score_text, (10, 10))
    screen.blit(lives_text, (WIDTH - 150, 10))

    # Aktualizacja ekranu
    pygame.display.flip()
    clock.tick(FPS)

# --- Po zakończeniu gry ---
screen.fill(BLACK)
end_text = font.render(f"Koniec gry! Twój wynik: {score}", True, YELLOW)
screen.blit(end_text, (WIDTH // 2 - 200, HEIGHT // 2))
pygame.display.flip()
pygame.time.wait(3000)

pygame.quit()
sys.exit()
