import pygame
from random import randint, choice

pygame.init() #inicjalizacja modułu

#Tworzenie okna
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

screen_surface = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption("Kupa") #nazwa okienka

#utworzenie zegara do nadzoru FPS
FPS = 60
clock = pygame.time.Clock()

bonus_images = ["bonus_1.png","bonus_2.png","bonus_3.png"]

################################################################################################

def load_image(img_path :str, position):
    image = pygame.image.load(img_path)
    surface = image.convert()
    transparent_color = (0,0,0) #kolor czarny rgb
    surface.set_colorkey(transparent_color)
    rect = surface.get_rect(center = position)

    return[image,surface,rect]

def print_image(img_list):
    image,surface,rect = img_list
    screen_surface.blit(surface,rect)

def set_position_image(img_list,position):
    image,surface,rect = img_list
    rect = surface.get_rect(center=position)

    return[image,surface,rect]

def calculate_player_movement(keys):
    #poruszanie postacią
    speed = 4
    delta_x = 0
    delta_y = 0

    if keys[pygame.K_LSHIFT]:
        speed*=2

    if keys[pygame.K_w]: #K_w = klawisz "w"
        delta_y -= speed

    if keys[pygame.K_s]: #K_w = klawisz "s"
        delta_y += speed

    if keys[pygame.K_a]: #K_w = klawisz "a"
        delta_x -= speed

    if keys[pygame.K_d]: #K_w = klawisz "d"
        delta_x += speed

    return [delta_x,delta_y]

def limit_position(position):
    x,y = position
    x = max(0,min(x,SCREEN_WIDTH))
    y = max(0,min(y,SCREEN_HEIGHT))

    return[x,y]

def generate_bonus_object():
    image_name = choice(bonus_images)
    x = randint(0,SCREEN_WIDTH)
    y = randint(0,SCREEN_HEIGHT)
    position = [x,y]
    new_obj = load_image(image_name,position)

    return new_obj

def print_bonus_objects(bonus_objects):
    for object in bonus_objects:
        print_image(object)

def check_collision():
    rect_player = player[2]

    for index in range((len(bonus_objects)-1),-1,-1):
        obj = bonus_objects[index]
        rect = obj[2]
        if rect.colliderect(rect_player):
            bonus_objects.pop(index)


################################################################################################

player_pos = [SCREEN_WIDTH/2, SCREEN_HEIGHT/2]
player = load_image('player.png',player_pos)
bonus_objects = []

################################################################################################

game_status = True

frames_cnt = 0

while game_status:
    frames_cnt += 1
    #odczytywanie zdarzeń
    events = pygame.event.get()

    for event in events:
        if event.type == pygame.QUIT:
            game_status = False

    #bonusy
    if frames_cnt % (FPS * 0.025) == 0:
        bonus_objects.append(generate_bonus_object())

    #interpretacja klaeiszy
    pressed_keys = pygame.key.get_pressed() #K_x - klawisz "x" ///  K_w = klawisz "w"
    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)

    #UZUPELNIJ TLO
    screen_surface.fill([0,0,0])
    #WYSWietl GRACZA
    print_image(player)

    check_collision()

    print_bonus_objects(bonus_objects)
    pygame.display.update()
    clock.tick(FPS)
    pass

pygame.quit()
quit()