import pygame

pygame.init()

def load_img(img_path: str, position):
    image = pygame.image.load(img_path)
    surface = image.convert()

    transparent_color = (0, 0, 0)
    surface.set_colorkey(transparent_color)

    rect = surface.get_rect(center=position)
    return [image, surface, rect]

def print_img(img_list):
    img, surface, rect  = img_list
    screen_surface.blit(surface, rect)
    pass

def move_player(keys):
    speed = 10
    delta_x = 0
    delta_y = 0

    if keys[pygame.K_w]:
        delta_y -= speed
    elif keys[pygame.K_s]:
        delta_y += speed
    elif keys[pygame.K_d]:
        delta_x += speed
    elif keys[pygame.K_a]:
        delta_x -= speed
    
    return [delta_x, delta_y]

def set_position_image(img_list, postion):
    image, surface, rect = img_list
    rect = surface.get_rect(center=postion)
    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]

bonuses_img = [
    'bonus_1.png',
    'bonus_2.png',
    'bonus_3.png'
]

bonuses_obj = []

import random

def generate_bonus_obj():
    image_name = random.choice(bonuses_img)

    x = random.randint(20, SCREEN_WIDTH)
    y = random.randint(20, SCREEN_HEIGHT)

    position = [x, y]

    new_obj = load_img(image_name, position)

    bonuses_obj.append(new_obj)
    pass

def print_bonus_obj():
    for obj in bonuses_obj:
        print_img(obj)
        pass
    pass

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
fps_cnt = 0

screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

pygame.display.set_caption('Moja pierwsza gra!')

clock = pygame.time.Clock()

player_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2]
player = load_img('player.png', player_pos)

is_game_running = True

while is_game_running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_game_running = False
            pass
        pass
    pass


    pressed_key = pygame.key.get_pressed()
    
    delta_x, delta_y = move_player(pressed_key)
    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("yellow")

    print_img(player)

    if (fps_cnt % (FPS * 1) == 0):
        generate_bonus_obj()
    print_bonus_obj()

    pygame.display.update()
    fps_cnt += 1

    clock.tick(FPS)

print('KONIEC GRY!')
pygame.quit() # zamykamy aplikację
quit() # zamykamy skrypt