
import pygame
import sys
import random
import time


# Initialize PyGame
pygame.init()
pygame.font.init()

# Set up the game window
screen_width = 1920 
screen_height = 1080 
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Game")


scale = (screen_width + screen_height) // 1000 #zet de game in verhouding tot de resolutie 

# Set the frame rate
clock = pygame.time.Clock()

# Player settings
player_width = int(30 * scale)
player_height = int(30 * scale)
player_x = (screen_width // 2 - player_width // 2) 
player_y = (screen_height //2 - player_height // 2)
player_speed = int(5 * scale)

barrier_width = 50 * scale
barrier_height = 250 * scale
barrier = []

coin_radius = 7 * scale
coins = []

level = 0
score = 0
score_amount = [1,1,2,2,1,2,3,2,5,1,2,0]
deaths = 0

end_width = 30 * scale
end_height = 30 *scale
end = []

lava_width = 50 * scale
lava_height = 250 *scale
lava = []



fontklein = pygame.font.Font(None, 60)
fontmini = pygame.font.Font(None, 45)
font = pygame.font.Font(None, 100)
instructions = []

enemy_width = int(50 * scale)
enemy_height = int(50 * scale)
enemy_speed = int(4 * scale)
enemy = []




#maak scene

def level_clear():
    global barrier
    global coins
    global end
    global text
    global instructions
    global lava
    global enemy
    global enemy_speed
    enemy_speed = 4*scale
    enemy = []
    lava = []
    instructions = []
    barrier = []
    coins = []
    end = []
    global score

    score = 0

def draw_level_finish():
    level_clear()
    global level
    level = -1
    endtime = time.time()
    lenght = int((endtime-start) // 1)

    end.append([(screen_width//2)-(end_width//2),(screen_height//1.2)-(end_height//2)])
    instructions.append([font.render('The end', True, (255, 255, 255)),(screen_width//2)-((font.render('The end', True, (255, 255, 255))).get_width()//2),(screen_height//6.5)])
    instructions.append([fontmini.render('you died '+str(deaths)+' times', True, (255, 255, 255)),(screen_width//2)-((fontmini.render('you died xx times', True, (255, 255, 255))).get_width()//2),(screen_height//4)])
    instructions.append([fontmini.render('and finished in '+str(lenght)+' seconds', True, (255, 255, 255)),(screen_width//2)-((fontmini.render('you finished in xxx seconds', True, (255, 255, 255))).get_width()//2),(screen_height//3.4)])
    instructions.append([fontklein.render('play again', True, (255, 255, 255)),(screen_width//2)-((fontklein.render('play again', True, (255, 255, 255))).get_width()//2),(screen_height//1.4)])


    global player_x
    global player_y

    player_x = (screen_width // 2 - player_width // 2) 
    player_y = (screen_height //2 - player_height // 2)
    
def draw_level0():
    level_clear()
    global level
    level =0

    coins.append([(screen_width//1.15-(coin_radius)),(screen_height//2)-(coin_radius)])
    end.append([(screen_width//6)-(end_width//2),(screen_height//2)-(end_height//2)])
    instructions.append([fontklein.render('collect the coins first', True, (255, 255, 255)),(screen_width//1.15)-((fontklein.render('collect the coins first', True, (255, 255, 255))).get_width()//2),(screen_height//5)])
    instructions.append([fontklein.render('then go to the next level', True, (255, 255, 255)),(screen_width//7)-((fontklein.render('then go to the next level', True, (255, 255, 255))).get_width()//2),(screen_height//5)])
    instructions.append([fontklein.render('LEVEL1', True, (255, 255, 255)),(0),(0)])


    global player_x
    global player_y

    player_x = (screen_width // 2 - player_width // 2) 
    player_y = (screen_height //2 - player_height // 2)

def draw_level1():
    level_clear()
    global level
    level = 1
    barrier.append([(screen_width//1.5-(barrier_width//2)),(screen_height//2)-(barrier_height//2)])
    barrier.append([(screen_width//3)-(barrier_width//2),(screen_height//2)-(barrier_height//2)])
    coins.append([(screen_width//1.15-(coin_radius)),(screen_height//2)-(coin_radius)])
    end.append([(screen_width//6)-(end_width//2),(screen_height//2)-(end_height//2)])
    instructions.append([fontklein.render('go around the obsticals', True, (255, 255, 255)),(screen_width//2)-((fontklein.render('go around the obsticals', True, (255, 255, 255))).get_width()//2),(screen_height//24)])
    instructions.append([fontklein.render('LEVEL2', True, (255, 255, 255)),(0),(0)])
    global player_x
    global player_y

    player_x = (screen_width // 2 - player_width // 2) 
    player_y = (screen_height //2 - player_height // 2)

def draw_level2():
    level_clear()
    global level
    level = 2
    barrier.append([(screen_width//1.5-(barrier_width//2)),(screen_height)-(barrier_height)]) 
    barrier.append([(screen_width//3)-(barrier_width//2),(screen_height)-(barrier_height)])

    coins.append([(screen_width//2-(coin_radius)),(screen_height//5)-(coin_radius)])
    coins.append([(screen_width//2-(coin_radius)),(screen_height//1.15)-(coin_radius)])
    end.append([(screen_width//1.15)-(end_width//2),(screen_height//2)-(end_height//2)])
    instructions.append([fontklein.render('collect al the coins', True, (255, 255, 255)),(screen_width//2)-((fontklein.render('collect al the coins', True, (255, 255, 255))).get_width()//2),(screen_height//24)])
    instructions.append([fontklein.render('LEVEL3', True, (255, 255, 255)),(0),(0)])
    
    global player_x
    global player_y

    player_x = (screen_width // 6 - player_width // 2) 
    player_y = (screen_height //2 - player_height // 2)

def draw_level3():
    level_clear()
    global level
    level = 3

    lava.append([(screen_width//1.5-(lava_width//2)),(screen_height)-(lava_height)]) 
    lava.append([(screen_width//3)-(lava_width//2),(screen_height//3)-(lava_height//2)])
    lava.append([(screen_width//1.65-(lava_width//2)),(screen_height)-(lava_height)]) 
    lava.append([(screen_width//2.6)-(lava_width//2),(screen_height//3)-(lava_height//2)])
    
    coins.append([(screen_width//2-(coin_radius)),(screen_height//5)-(coin_radius)])
    coins.append([(screen_width//2-(coin_radius)),(screen_height//1.15)-(coin_radius)])
    end.append([(screen_width//1.15)-(end_width//2),(screen_height//2)-(end_height//2)])
    instructions.append([fontklein.render('dont touch the lava', True, (255, 255, 255)),(screen_width//1.5)-((fontklein.render('dont touch the lava', True, (255, 255, 255))).get_width()//2),(screen_height//24)])
    instructions.append([fontklein.render('LEVEL4', True, (255, 255, 255)),(0),(0)])
    
    global player_x
    global player_y

    player_x = (screen_width // 6 - player_width // 2) 
    player_y = (screen_height //2 - player_height // 2)

def draw_level5():
    level_clear()
    global level
    level =5

    enemy.append(['up',(screen_width//3-(enemy_width//2)),(screen_height)])
    enemy.append(['up',(screen_width//1.5-(enemy_width//2)),(0)])


    lava.append([(screen_width//2)-(lava_width//2),(screen_height//2)-(lava_height//2)])
    coins.append([(screen_width//3-(coin_radius)),(screen_height//2)-(coin_radius)])
    coins.append([(screen_width//1.5-(coin_radius)),(screen_height//2)-(coin_radius)])
    
    end.append([(screen_width//1.15)-(end_width//2),(screen_height//2)-(end_height//2)])
    
    instructions.append([fontklein.render('LEVEL6', True, (255, 255, 255)),(0),(0)])
    

    global player_x
    global player_y

    player_x = (screen_width // 6 - player_width // 2) 
    player_y = (screen_height //2 - player_height // 2)

def draw_level4():
    level_clear()
    global level
    level =4

    enemy.append(['up',(screen_width//3-(enemy_width//2)),(screen_height)])
    enemy.append(['up',(screen_width//2-(enemy_width//2)),(0)])
    enemy.append(['up',(screen_width//1.5-(enemy_width//2)),(screen_height)])

    coins.append([(screen_width//2-(coin_radius)),(screen_height//2)-(coin_radius)])
    end.append([(screen_width//1.15)-(end_width//2),(screen_height//2)-(end_height//2)])
    instructions.append([fontklein.render('dont touch the enemies', True, (255, 255, 255)),(screen_width//2)-((fontklein.render('dont touch the enemies', True, (255, 255, 255))).get_width()//2),(screen_height//5)])
    instructions.append([fontklein.render('LEVEL5', True, (255, 255, 255)),(0),(0)])
    

    global player_x
    global player_y

    player_x = (screen_width // 6 - player_width // 2) 
    player_y = (screen_height //2 - player_height // 2)


def draw_level6():
    level_clear()
    global level
    level =6

    enemy.append(['left',((screen_width//2)),((screen_height//2)-(enemy_width//2))])



    barrier.append([(screen_width//1.5-(barrier_width//2)),(screen_height//2)-(barrier_height)-(enemy_height)]) 
    barrier.append([(screen_width//2)-(barrier_width//2),(screen_height//2)-(barrier_height)-(enemy_height)])
    barrier.append([(screen_width//3)-(barrier_width//2),(screen_height//2)-(barrier_height)-(enemy_height)])

    barrier.append([(screen_width//1.5-(barrier_width//2)),(barrier_height)-(enemy_height)]) 
    barrier.append([(screen_width//2)-(barrier_width//2),(barrier_height)-(enemy_height)])
    barrier.append([(screen_width//3)-(barrier_width//2),(barrier_height)-(enemy_height)])
    
    coins.append([(screen_width//2.35-(coin_radius)),(screen_height//5)-(coin_radius)])
    coins.append([(screen_width//1.7-(coin_radius)),(screen_height//5)-(coin_radius)])
    coins.append([(screen_width//1.7-(coin_radius)),(screen_height//1.5)-(coin_radius)])
    
    end.append([(screen_width//1.15)-(end_width//2),(screen_height//2)-(end_height//2)])
    
    instructions.append([fontklein.render('LEVEL7', True, (255, 255, 255)),(0),(0)])
    

    global player_x
    global player_y

    player_x = (screen_width // 6 - player_width // 2) 
    player_y = (screen_height //1.2 - player_height // 2)

def draw_level7():
    level_clear()
    global level
    level =7

    enemy.append(['up',(screen_width//2.4-(enemy_width//2)),(screen_height)])
    enemy.append(['up',(screen_width//1.71-(enemy_width//2)),(0)])



    lava.append([(screen_width//1.5-(lava_width//2)),(screen_height//2)-(lava_height)-(enemy_height)]) 
    lava.append([(screen_width//3)-(barrier_width//2),(screen_height//2)-(barrier_height)-(enemy_height)])

    lava.append([(screen_width//1.5-(barrier_width//2)),(barrier_height)-(enemy_height)]) 
    lava.append([(screen_width//3)-(barrier_width//2),(barrier_height)-(enemy_height)])
    
    lava.append([(screen_width//2)-(lava_width//2),(screen_height//2)-(lava_height//2)])
    coins.append([(screen_width//2-(coin_radius)),(screen_height//12)-(coin_radius)])
    coins.append([(screen_width//2-(coin_radius)),(screen_height//1.05)-(coin_radius)])
    
    end.append([(screen_width//1.15)-(end_width//2),(screen_height//2)-(end_height//2)])
    
    instructions.append([fontklein.render('LEVEL8', True, (255, 255, 255)),(0),(0)])
    

    global player_x
    global player_y

    player_x = (screen_width // 6 - player_width // 2) 
    player_y = (screen_height //1.2 - player_height // 2)

def draw_level8():
    level_clear()
    global level
    level =8

    enemy.append(['up',(screen_width//3.6-(enemy_width//2)),(screen_height)])
    enemy.append(['up',(screen_width//2.5-(enemy_width//2)),(0)])
    enemy.append(['up',(screen_width//2-(enemy_width//2)),(screen_height)])
    enemy.append(['up',(screen_width//1.7-(enemy_width//2)),(0)])
    enemy.append(['up',(screen_width//1.45-(enemy_width//2)),(screen_height)])


    coins.append([(screen_width//3.6-(coin_radius)),(screen_height//2)-(coin_radius)])
    coins.append([(screen_width//2.5-(coin_radius)),(screen_height//2)-(coin_radius)])
    coins.append([(screen_width//2-(coin_radius)),(screen_height//2)-(coin_radius)])
    coins.append([(screen_width//1.7-(coin_radius)),(screen_height//2)-(coin_radius)])
    coins.append([(screen_width//1.45-(coin_radius)),(screen_height//2)-(coin_radius)])

    lava.append([(screen_width//3.6-(barrier_width//2)),(barrier_height//0.85)]) 
    lava.append([(screen_width//2.5-(barrier_width//2)),(barrier_height//0.85)])
    lava.append([(screen_width//2-(barrier_width//2)),(barrier_height//0.85)]) 
    lava.append([(screen_width//1.7-(barrier_width//2)),(barrier_height//0.85)])
    lava.append([(screen_width//1.45-(barrier_width//2)),(barrier_height//0.85)])
    
    lava.append([(screen_width//3.6-(barrier_width//2)),(screen_height)-(barrier_height//0.47)]) 
    lava.append([(screen_width//2.5-(barrier_width//2)),(screen_height)-(barrier_height//0.47)])
    lava.append([(screen_width//2-(barrier_width//2)),(screen_height)-(barrier_height//0.47)]) 
    lava.append([(screen_width//1.7-(barrier_width//2)),(screen_height)-(barrier_height//0.47)])
    lava.append([(screen_width//1.45-(barrier_width//2)),(screen_height)-(barrier_height//0.47)])

    
    end.append([(screen_width//1.15)-(end_width//2),(screen_height//2)-(end_height//2)])
    instructions.append([fontklein.render('LEVEL9', True, (255, 255, 255)),(0),(0)])

    global player_x
    global player_y

    player_x = (screen_width // 8 - player_width // 2) 
    player_y = (screen_height //2 - player_height // 2)

def draw_level9():
    level_clear()
    global level
    level =9

    enemy.append(['right',(screen_width//2),((screen_height//7)-(enemy_width//2))])
    enemy.append(['left',((0)),((screen_height//3)-(enemy_width//2))])
    enemy.append(['left',((screen_width//2)),((screen_height//2)-(enemy_width//2))])
    enemy.append(['left',(screen_width),((screen_height//1.5)-(enemy_width//2))])


    coins.append([(screen_width//15-(coin_radius)),(screen_height//8)-(coin_radius)])

    lava.append([(0),(screen_height-lava_height)])
    lava.append([(screen_width//4-(barrier_width//2)),(0)]) 
    lava.append([(screen_width//3.4-(barrier_width//2)),(0)]) 
    lava.append([(screen_width//2-(barrier_width//2)),(screen_height-lava_height)])
    lava.append([(screen_width//1.5-(barrier_width//2)),(0)])
    lava.append([(screen_width//1.2-(barrier_width//2)),(screen_height-lava_height)])


    
    end.append([(screen_width//1.07)-(end_width//2),(screen_height//1.2)-(end_height//2)])
    instructions.append([fontklein.render('LEVEL10', True, (255, 255, 255)),(0),(0)])

    global player_x
    global player_y

    player_x = (screen_width // 8 - player_width // 2) 
    player_y = (screen_height //1.2 - player_height // 2)

def draw_level10():
    level_clear()
    global level
    global enemy_speed
    enemy_speed = 21
    level =10

    enemy.append(['left',((screen_width//2)),((screen_height//2)-(enemy_width//2))])


    barrier.append([(screen_width//1.5-(barrier_width//2)),-300]) 
    barrier.append([(screen_width//1.5)-(barrier_width),-300])
    barrier.append([(screen_width//1.5)-(2*barrier_width),-300])
    
    barrier.append([(screen_width//2)-(barrier_width//2),-300])
    barrier.append([(screen_width//2)-(barrier_width),-300])
    barrier.append([(screen_width//2)-(2*barrier_width),-300])
    
    barrier.append([(screen_width//3)-(barrier_width//2),-300])
    barrier.append([(screen_width//3)-(barrier_width),-300])
    barrier.append([(screen_width//3)-(2*barrier_width),-300])

    lava.append([(screen_width)-(barrier_width//2),-500])
    lava.append([(screen_width)-(barrier_width),-500])
    lava.append([(screen_width)-(2*barrier_width),-500])
    lava.append([(screen_width)-(3*barrier_width),-500])
    lava.append([(screen_width)-(3.77*barrier_width),-500])
    
    

    barrier.append([(screen_width//1.5-(barrier_width//2)),(barrier_height)-(enemy_height)]) 
    barrier.append([(screen_width//1.5)-(barrier_width),(barrier_height)-(enemy_height)])
    barrier.append([(screen_width//1.5)-(2*barrier_width),(barrier_height)-(enemy_height)])
    
    barrier.append([(screen_width//2)-(barrier_width//2),(barrier_height)-(enemy_height)])
    barrier.append([(screen_width//2)-(barrier_width),(barrier_height)-(enemy_height)])
    barrier.append([(screen_width//2)-(2*barrier_width),(barrier_height)-(enemy_height)])
    
    barrier.append([(screen_width//3)-(barrier_width//2),(barrier_height)-(enemy_height)])
    barrier.append([(screen_width//3)-(barrier_width),(barrier_height)-(enemy_height)])
    barrier.append([(screen_width//3)-(2*barrier_width),(barrier_height)-(enemy_height)])

    lava.append([(screen_width)-(barrier_width//2),screen_height-300])
    lava.append([(screen_width)-(barrier_width),screen_height-300])
    lava.append([(screen_width)-(2*barrier_width),screen_height-300])
    lava.append([(screen_width)-(3*barrier_width),screen_height-300])
    lava.append([(screen_width)-(3.77*barrier_width),screen_height-300])
    
    coins.append([(screen_width//8-(coin_radius)),(screen_height//5)-(coin_radius)])
    coins.append([(screen_width//2-(coin_radius)),(screen_height//2)-(coin_radius)])
    
    end.append([(screen_width//1.15)-(end_width//2),(screen_height//2)-(end_height//2)])
    
    instructions.append([fontklein.render('LEVEL11', True, (255, 255, 255)),(0),(0)])

    global player_x
    global player_y
    
    player_x = (screen_width // 8 - player_width // 2) 
    player_y = (screen_height //1.2 - player_height // 2)
    

def collision_barrier():
    lijst = []
    for barriere in barrier:
        lijst.append(pygame.Rect((player_x,player_y,player_height,player_width)).colliderect(pygame.Rect((barriere[0],barriere[1],barrier_width,barrier_height))))
    if True in lijst :
        return True
    else :
        return False

def collision_coin():
    lijst = []
    for coin in coins:
        lijst.append(pygame.Rect((player_x,player_y,player_height,player_width)).colliderect(pygame.Rect((coin[0], coin[1] , coin_radius,coin_radius))))
        if pygame.Rect((player_x,player_y,player_height,player_width)).colliderect(pygame.Rect((coin[0], coin[1] , coin_radius,coin_radius))):
            coins.remove([coin[0], coin[1]])
    if True in lijst :
        return True
    else :
        return False



def collision_end():
    lijst = []
    for ends in end:
        lijst.append(pygame.Rect((player_x,player_y,player_height,player_width)).colliderect(pygame.Rect((ends[0],ends[1],end_width,end_height))))
    if True in lijst :
        return True
    else :
        return False

def collision_lava():
    lijst = []
    for lavas in lava:
        lijst.append(pygame.Rect((player_x,player_y,player_height,player_width)).colliderect(pygame.Rect((lavas[0],lavas[1],lava_width,lava_height))))
    if True in lijst :
        return True
    else :
        return False

def collision_enemy():
    lijst = []
    for enemys in enemy:
        lijst.append(pygame.Rect((player_x,player_y,player_height,player_width)).colliderect(pygame.Rect((enemys[1],enemys[2],enemy_width,enemy_height))))
    if True in lijst :
        return True
    else :
        return False
    

draw_level = [draw_level0, draw_level1, draw_level2, draw_level3,draw_level4 ,draw_level5,draw_level6,draw_level7,draw_level8,draw_level9,draw_level10,draw_level_finish,draw_level0]

start = time.time()
draw_level0()   
# Main game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()



    
    # Handle player movement
    keys = pygame.key.get_pressed()
    if keys[pygame.K_q] and player_x > 0 and not collision_barrier():
        player_x -= player_speed
    if collision_barrier():
        player_x += 15


    if keys[pygame.K_d]  and player_x < screen_width - player_width and not collision_barrier():
        player_x += player_speed
    if collision_barrier():
        player_x -= 15
 

    if keys[pygame.K_z] and player_y > 0 and not collision_barrier():
        player_y -= player_speed
    if collision_barrier():
        player_y += 15

    if keys[pygame.K_s]  and player_y < screen_height - player_height and not collision_barrier():
        player_y += player_speed
    if collision_barrier():
        player_y -= 15

    if collision_coin():
        score+=1

    if collision_end():
        if score >= score_amount[level]:
            draw_level[int(level+1)]()

    if collision_lava():
        draw_level[int(level)]()
        deaths += 1
    if collision_enemy():
        draw_level[int(level)]()
        deaths += 1

    screen.fill((0, 0, 0))
    
    #render alle objecten in de scene

    pygame.draw.rect(screen, (0, 128, 255), (player_x, player_y, player_width, player_height))

    for barriere in barrier:
        pygame.draw.rect(screen, (200,200,200), (barriere[0], barriere[1] , barrier_width, barrier_height))

    for coin in coins :
        pygame.draw.circle(screen, (255,255,64), (coin[0], coin[1]) , coin_radius)
    for ends in end:
        pygame.draw.rect(screen, (128,255,128), (ends[0], ends[1], end_width, end_height))

    for tekst in instructions :
        screen.blit(tekst[0],(tekst[1],tekst[2]))

    for lavas in lava :
        pygame.draw.rect(screen, (255,64,64), (lavas[0], lavas[1] , lava_width, lava_height))

    for enemys in enemy :
        if enemys[2] >= screen_height-enemy_height:
            enemys[0] = 'down'
        if enemys[2] <= 0:
            enemys[0] = 'up'

        if enemys[1] >= screen_width-enemy_width:
            enemys[0] = 'left'
        if enemys[1] <= 0:
            enemys[0] = 'right'

        

        if enemys[0] == 'down':
            enemys[2] -= enemy_speed
        if enemys[0] == 'up':
            enemys[2] += enemy_speed

        if enemys[0] == 'left':
            enemys[1] -= enemy_speed
        if enemys[0] == 'right':
            enemys[1] += enemy_speed
            
        pygame.draw.rect(screen, (255,000,000), (enemys[1], enemys[2] , enemy_width, enemy_height))


    
    

    #update display
    pygame.display.flip()

    # Cap the frame rate at 60 FPS
    clock.tick(60)
