# import pygame

# def load_image(img_path: str, postion):
#     image = pygame.image.load(img_path)
#     surface = image.convert()
#     transparent_color = (2, 5, 5)
#     surface.set_colorkey(transparent_color)

#     rect = surface.get_rect(center=postion)
#     return [image, surface, rect]

# def print_image(img_list) -> None:
#     image, surface, rect = img_list
#     screen_surface.blit(surface, rect)
#     pass

# pygame.init()

# SCREEN_WIDTH = 800
# SCREEN_HEIGHT = 600

# screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# pygame.display.set_caption("Pierwsza gra")

# clock = pygame.time.Clock()

# player_pos = [SCREEN_WIDTH //2, SCREEN_HEIGHT //2 ]
# player = load_image('player.png', player_pos)

# game_status = True

# while game_status:
#     events = pygame.event.get()
    
#     for event in events:
#         print(event)

#         if event.type == pygame.QUIT:
#             game_status = False
#         pass
#     print_image(player)
#     pygame.display.update()
#     clock.tick(60)
    
# print("Zamykanie aplikacji")
# pygame.quit()
# quit()

# Import modulu pygame, dzieki ktoremu tworzymy aplikacje okienkowa
import pygame

# Inicjalizacja modułu
pygame.init()
# Utworzenie okna o określonych wymiarach
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Nadanie nazwy oknu
pygame.display.set_caption('Pierwsza Gra')

# Utworzenie zegara, który nadzoruje stałe wartości fps
clock = pygame.time.Clock()

def set_position_image(img_list, position):
    image, surface, rect = img_list
    rect = surface.get_rect(center=position)
    return [image, surface, rect]

def limit_position(position):
    x, y = position

    x = max(20, min(x, SCREEN_WIDTH - 20))
    y = max(20, min(y, SCREEN_HEIGHT - 20))

    return[x, y]

def calculate_player_movement(keys):
    speed = 10
    delta_X = 0
    delta_Y = 0
    if keys[pygame.K_LSHIFT]:
        speed *=2
    if keys[pygame.K_w]:
        delta_Y -= speed
    if keys[pygame.K_s]:
        delta_Y += speed
    if keys[pygame.K_d]:
        delta_X += speed
    if keys[pygame.K_a]:
        delta_X -= speed
    return [delta_X, delta_Y]


def load_image(img_path: str, position):
    image = pygame.image.load(img_path)
    surface = image.convert()

    transparent_color = (0, 0, 0)
    surface.set_colorkey(transparent_color)

    # Pozycja wyświetlania obiektu zapisana jest w rect
    rect = surface.get_rect(center=position)

    return [image, surface, rect]


def print_image(img_list) -> None:
    # [image, surface, rect]
    image, surface, rect = img_list
    screen_surface.blit(surface, rect)
    pass

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
player_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2]
player = load_image('player.png', player_pos)
background_color = [0, 0, 0]


# Zmienna określająca, czy należy zamknąć okno
game_status = True
# Kod wykonywany póki aplikacja jest uruchomiona
while game_status:

    # Odczytanie zdarzeń zarejestrowanych przez komputer
    events = pygame.event.get()

    for event in events:
        # Naciśnięto X - zamykanie aplikacji
        if event.type == pygame.QUIT:
            game_status = False
        pass  # for event

        pressed_keys = pygame.key.get_pressed()

        delta_x, delta_y = calculate_player_movement(pressed_keys)

        player_pos[0] += delta_x
        player_pos[1] += delta_y

        player_pos = limit_position(player_pos)

        player = set_position_image(player, player_pos)

    screen_surface.fill(background_color)
    # Wyświetl gracza
    print_image(player)

    # Odświeżenie wyświetlanego okna
    pygame.display.update()

    clock.tick(60)
    pass

print("Zamykanie aplikacji")
# Zamknięcie aplikacji
pygame.quit()
# Zamknięcie skryptu
quit()


































































