# Pyxel Studio

import pyxel
from random import randint
from math import *


pyxel.init(256, 256, title="Nuit du Code")
start = True
But=False
gardien_x = 115
sens = 1
fleche_x = 115
tir_au_but = False
foot = False
x_ballon = 115
y_ballon = 190
x_texte = 100000
y_texte = 100000
game_over = False
score = 0
x_poids = 122
y_poids = 210
charge = 0
temps_max = 300
stop_temps = False
poids_lache = False
puissance = 0
x_score = 10000
x_distance = 1000
Gagner = False
GAME_OVER = False
handball = False
lancer_poid = False
clignotant = 0
xmovH = 20
start2 = False
ymovH = 80
xjav = 153
yjav = 85
xjav2 = 170
yjav2 = 80
xmovM = 20
xpoids = 165
trait_x = 50
trait_x2 = 100
trait_x3 = 150
trait_x4 = 200
trait_x5 = 250
ypoids = 180
descente_poids = False
haie = False
lancer = False
joueur_centdixmetres_y = 150
haie_x = 280
vitesse_haie = 8
compteur_haie = 0
lancer_jav = False
retry = False
courir = False
multi = False
x_jav_1 = 2
y_jav_1 = 210
temps_score = 0
coureur_cent_x = 0
finish = False
x_jav_2 = 22
y_jav_2 = 206
x_lanceur = 5
x_texte = 100000
y_texte = 100000
game_over = False
score1 = 0
multiplicateur = 1
vie_centdixmetre = 50
score = 0
test = False
course = False

def poids_charge():
    global x_poids, charge, temps_max, puissance, poids_lache
    if temps_max > 0 and poids_lache == False:
        if pyxel.btnr(pyxel.KEY_RIGHT) == True and x_poids != 136:
            x_poids += 14
            charge += 1
            puissance += 0.1
        if pyxel.btnr(pyxel.KEY_LEFT) == True and x_poids != 122:
            x_poids -= 14
            charge += 1
            puissance += 0.1
            
def lancer_poids():
    global x_poids, y_poids, temps_max, poids_lache, stop_temps, puissance, x_score, x_distance, x_texte, y_texte
    if y_poids < 220 and temps_max > 0:
        if pyxel.btnr(pyxel.KEY_SPACE) == True and x_poids == 136:
            poids_lache = True
        if poids_lache == True:
            stop_temps = True
            x_poids += 2
            puissance -= 0.2
            y_poids -= puissance
            x_score = 120
            x_distance = 120
            x_texte = 110
            y_texte = 80
            
def chrono():
    global temps_max, x_score, x_distance, x_texte, y_texte
    if stop_temps == False and temps_max > 0:
        temps_max -= 1
    else:
        x_score = 120
        x_distance = 120
        x_texte = 110
        y_texte = 80
    
def score_poids():
    global score, charge, temps_max
    if temps_max > 0:
        score = charge / (1/temps_max)
    else:
        x_texte = 120
        y_texte = 80
        score = 0
        charge = 0

def rejouer():
    global  x_texte, y_texte, game_over, score, x_poids, y_poids, charge, temps_max, stop_temps, poids_lache, puissance, x_score, x_distance
    if pyxel.btnr(pyxel.KEY_R) == True:
        poids_lache = False
        stop_temps = False
        game_over = False
        x_texte = 100000
        y_texte = 100000
        score = 0
        x_poids = 122
        y_poids = 210
        charge = 0
        temps_max = 300
        puissance = 0
        x_score = 10000
        x_distance = 1000

def calcule_distance_tab(xa,xb,ya,yb):
   dist = sqrt((xb-xa)**2 + (yb-ya)**2)
   return dist


def test_but():
   global GAME_OVER, Gagner
   if calcule_distance_tab(x_ballon, gardien_x, y_ballon, 100) < 35:
       GAME_OVER = True
   if y_ballon < 40:
       Gagner = True
   pass
def tir():
   global fleche_x, tir_au_but
   if pyxel.btn(pyxel.KEY_RIGHT):
       fleche_x += 3
   if pyxel.btn(pyxel.KEY_LEFT):
       fleche_x -= 3
   if pyxel.btn(pyxel.KEY_SPACE) and tir_au_but == False:
       tir_au_but =True
       print('true')
   pass
def calcule_distance(xa,xb,ya,yb):
   dist = sqrt((xb-xa)**2 + (yb-ya)**2)
   return dist
def move_joueur_centdixmetre():
   global joueur_centdixmetres_y
   if pyxel.btn(pyxel.KEY_SPACE) == True and joueur_centdixmetres_y >= 150:   
       joueur_centdixmetres_y = joueur_centdixmetres_y - 100


   if  joueur_centdixmetres_y < 150:
       joueur_centdixmetres_y = joueur_centdixmetres_y + 5
def obstacle_haie():
   global haie_x, compteur_haie, vitesse_haie
  
   if haie_x > -10:
       haie_x = haie_x - vitesse_haie
   else:
       haie_x = 280
       compteur_haie = compteur_haie+  1
   if compteur_haie == 6:
       vitesse_haie +=2
       compteur_haie += 1
   if compteur_haie == 10:
       vitesse_haie +=2
       compteur_haie += 1
   if compteur_haie == 17:
       vitesse_haie +=2
       compteur_haie += 1
      
def test_collision():
   global vitesse_haie, vie_centdixmetre
   coll = calcule_distance(60,haie_x+10,joueur_centdixmetres_y+50,200)
   if coll < 20:
       print("toucher")
       vie_centdixmetre -= 4
def lanceur_jav():
    global x_jav_1, x_jav_2, x_lanceur, courir, lancer_jav, x_texte, y_texte, game_over, lancer_jav
    if pyxel.btnr(pyxel.KEY_RIGHT) == True:
        courir = True
    if x_lanceur >= 40:
        game_over = True
        courir = False
        x_texte = 100
        y_texte = 128
    if lancer_jav == True:
        courir = False
    if courir == True:
        x_jav_1 += 2
        x_jav_2 += 2
        x_lanceur += 2
        
def lancer_javelot():
    global x_jav_1, y_jav_1, x_jav_2, y_jav_2, lancer_jav, game_over
    if pyxel.btnr(pyxel.KEY_SPACE) == True and game_over == False and score2 > 0:
        lancer_jav = True
    if lancer_jav == True :
        x_jav_1 += 7
        y_jav_1 -= 3
        x_jav_2 += 7
        y_jav_2 -= 3
        
def score_javelot():
    global score2, x_jav_1, x_lanceur, multiplicateur, multi
    if pyxel.btnr(pyxel.KEY_SPACE) == True:
        multi == True
    if multi == True:
        while x_jav_1 <=250 :
            if pyxel.btnr(pyxel.KEY_SPACE) == True:
                multiplicateur += 1
    if x_jav_1 >= 250:
        multi = False
    score2 = x_lanceur * multiplicateur - 5
    if game_over == True:
        score2 = 0
        
        
        
def retry():
    global x_jav_1, y_jav_1, x_jav_2, y_jav_2, lancer_jav, courir, x_lanceur, x_texte, y_texte,game_over, multi, multiplicateur, score
    if pyxel.btnr(pyxel.KEY_R) == True:
        retry = True
    else : 
        retry = False
    if retry == True:
        lancer_jav = False
        courir = False
        x_jav_1 = 2
        y_jav_1 = 210
        x_jav_2 = 22
        y_jav_2 = 206
        x_lanceur = 5
        x_texte = 100000
        y_texte = 100000
        game_over = False
        multi = False
        multiplicateur = 1
        score = 0
def coureur_cent():
   global coureur_cent_x
  
   if pyxel.btnr(pyxel.KEY_SPACE) == True:
       coureur_cent_x += 2
def ligne_arrivee():
   global finish
   if coureur_cent_x >= 210:
       finish = True


if start == True:
    def draw():
        global clignotant, xmovH, ymovH, xjav, yjav, xjav2, yjav2, xmovM, xpoids, ypoids, descente_poids, haie, start, haie, trait_x, trait_x2, trait_x3, trait_x4, trait_x5, start2, x_jav_1, y_jav_1, x_jav_2, y_jav_2, x_texte1, y_texte1, score2
        if start == True:
            pyxel.cls(0)
            pyxel.rect(0, 0, 256, 256, 1)
            pyxel.text(75, 5, "Choississez votre mini-jeu :", 11)
            
            pyxel.text(40, 50, "110m Haies", 7)
            pyxel.rect(15, 60, 90, 50, 3)
            pyxel.rect(15, 100, 90, 10, 4)
            
            pyxel.text(165, 50, "Lancer de Javelot", 7)
            pyxel.rect(150, 60, 90, 50, 3)
            pyxel.rect(150, 100, 90, 10, 4)
            
            pyxel.text(50, 150, "100m", 7)
            pyxel.rect(15, 160, 90, 50, 3)
            pyxel.rect(15, 200, 90, 10, 4)
            pyxel.rect(xmovM, 180, 8, 20, 11)
            
            pyxel.text(165, 150, "Lancer de Poids", 7)
            pyxel.rect(150, 160, 90, 50, 3)
            pyxel.rect(150, 200, 50, 10, 4)
            pyxel.rect(200, 200, 40, 10, 10)
            pyxel.rect(155, 180, 8, 20, 11)
            pyxel.circ(xpoids, ypoids, 2, 0)
            
            # Les anneaux des Jeux Olympiques
            pyxel.circb(128, 30, 10, 0)
            pyxel.circb(115, 40, 10, 10)
            pyxel.circb(102, 30, 10, 6)
            pyxel.circb(141, 40, 10, 11)
            pyxel.circb(154, 30, 10, 8)
            
            pyxel.rectb(15, 230, 215, 20, 7)
            pyxel.text(20, 235, "Pour changer de page appuyer sur la fleche de droite", 7)
    
            pyxel.rect(80, 90, 4, 10, 7)
            pyxel.rect(155, 80, 8, 20, 11)
            # Création du javelot
            pyxel.line(xjav, yjav, xjav2, yjav2, 7)
            if xmovH < 90:
                pyxel.rect(xmovH, ymovH, 8, 20, 11)
            if xmovH == 74:
                ymovH -= 12
            if xmovH == 80:
                ymovH += 12
            if xmovH > 105:
                xmovH = 20
                ymovH = 80
                
            if ypoids > 190:
                ypoids = 180
                xpoids = 165
                
            if xmovM > 90:
                xmovM = 20
            # Lancer de javelot :
            if yjav < 60:
                xjav = 153
                yjav = 85
                xjav2 = 170
                yjav2 = 80
    
            
            if clignotant <= 30:
                pyxel.text(25, 63, "Press H to Launch", 4)
                pyxel.text(165, 63, "Press L to Launch", 4)
                pyxel.text(25, 163, "Press J to Launch", 4)
                pyxel.text(165, 163, "Press R to Launch", 4)
            if clignotant >= 30:
                pyxel.text(25, 63, "Press H to Launch", 11)
                pyxel.text(165, 63, "Press L to Launch", 11)
                pyxel.text(25, 163, "Press J to Launch", 11)
                pyxel.text(165, 163, "Press R to Launch", 11)
                clignotant = 0
                
        if start2 == True:
            pyxel.rect(0, 0, 256, 256, 1)
            pyxel.text(75, 5, "Choississez votre mini-jeu :", 11)
            
            pyxel.circb(128, 30, 10, 0)
            pyxel.circb(115, 40, 10, 10)
            pyxel.circb(102, 30, 10, 6)
            pyxel.circb(141, 40, 10, 11)
            pyxel.circb(154, 30, 10, 8)
            pyxel.rectb(15, 230, 215, 20, 7)
            pyxel.text(20, 235, "Pour changer de page appuyer sur la fleche de gauche", 7)
            pyxel.text(105, 80, "Tir au But", 7)
            pyxel.rect(83, 90, 90, 50, 3)
            pyxel.rect(83, 140, 90, 10, 11)
            if clignotant <= 30:
                pyxel.text(95, 95, "Press Y to Launch", 4)
            if clignotant >= 30:
                pyxel.text(95, 95, "Press Y to Launch", 11)
                clignotant = 0
            pyxel.line(103, 140, 103, 110, 7)
            pyxel.line(103, 110, 153, 110, 7)
            pyxel.line(153, 110, 153, 140, 7)
            pyxel.rect(126, 124, 8, 16, 0)
        if foot == True:
            if GAME_OVER == False and Gagner == False:
               pyxel.cls(3)
               pyxel.rect(0, 175, 300, 200, 11)
               pyxel.rect(20, 10, 216, 150, 7)
               pyxel.rect(30, 20, 196, 140, 3)#BUT
               pyxel.rect(gardien_x,100,40,40,0)
               pyxel.rect(fleche_x, 200, 8, 35 , 7)
               pyxel.tri(fleche_x+4 , 190, fleche_x-10, 220,fleche_x+15, 220, 7)
            if tir_au_but == True:
                pyxel.circ(x_ballon, y_ballon, 10, 5)
            if GAME_OVER == True:
                pyxel.cls(3)
                pyxel.text(100,100, "PERDU", 8)
                pyxel.text(100,110, "Pour rejouer appuyez sur O", 8)
                pyxel.text(100,120, "Pour aller au menu appuyez sur M", 11)
            if Gagner == True :
                pyxel.cls(3)
                pyxel.text(100,100, "GAGNER", 11)
                pyxel.text(100,110, "Pour rejouer appuyez sur O", 11)
                pyxel.text(100,120, "Pour aller au menu appuyez sur M", 11)
        if lancer_poid == True : 
            pyxel.cls(0)
            pyxel.rect(0, 0, 256, 256, 12)
            pyxel.rect(0, 220, 256, 36, 11)
            pyxel.rect(126, 205, 7, 20, 10)
            pyxel.circ(x_poids, y_poids, 3, 0)
            pyxel.text(x_texte, y_texte, "GAME OVER, PRESS R", 0)
            pyxel.text(x_texte, 90, "Retour au menu M", 0)
            pyxel.text(x_score, 120, "score: " + str(score) + "m", 0)
            pyxel.text(120, 100, "Temps: " + str(temps_max), 0)
            pyxel.text(x_distance, 110, "Distance: " + str(charge/10), 0)

        if haie == True:
            pyxel.cls(3)
            pyxel.rect(0, 200, 300, 70, 4)
            pyxel.rect(40, joueur_centdixmetres_y, 20, 50, 11)
            pyxel.rect(haie_x, 175, 10, 25, 7)
            pyxel.rect(trait_x2, 200, 5, 25, 0)
            pyxel.rect(trait_x3, 200, 5, 25, 0)
            pyxel.rect(trait_x4, 200, 5, 25, 0)
            pyxel.rect(trait_x5, 200, 5, 25, 0)
            pyxel.rect(trait_x, 200, 5, 25, 0)
            pyxel.circ(128, 40, 20, 10)
            pyxel.rect(200, 10, vie_centdixmetre, 10, 8)
            pyxel.rectb(200, 10, 50, 10, 8)
            pyxel.text(10, 10, "score : " + str(score1), 8)
            if vie_centdixmetre <= 0:
                pyxel.cls(3)
                pyxel.text(80, 100, "Votre score est de : " + str(score1), 8)
                pyxel.text(90, 110, "Pour rejouer pressez H", 5)
                pyxel.text(50, 120, "Pour aller dans le menu pressez M", 5)
        if lancer == True:
            pyxel.cls(0)
            pyxel.rect(0, 0, 256, 256, 12)
            pyxel.rect(0, 220, 256, 36, 11)
            pyxel.circ(128, 40, 20, 10)   
            pyxel.rect(x_lanceur, 205, 5, 15, 4)
            pyxel.line(x_jav_1, y_jav_1, x_jav_2, y_jav_2, 7)
            pyxel.text(100, 128, "PRESS R to regame", 0)
            pyxel.text(100, 138, "PRESS M to menu", 0)
            pyxel.text(120, 100, "score: " + str(score2), 0)
        
        if course == True:
            pyxel.cls(3)
            pyxel.cls(3)
            pyxel.rect(0, 200, 300, 70, 4)
            pyxel.rect(190, 200 , 10, 70, 0)
            pyxel.rect(200, 200 , 10, 70, 7)
            pyxel.rect(210, 200 , 10, 70, 0)
            pyxel.rect(0, 200, 5, 25, 0)
            pyxel.rect(32, 200, 5, 25, 0)
            pyxel.rect(64, 200, 5, 25, 0)
            pyxel.rect(96, 200, 5, 25, 0)
            pyxel.circ(128, 40, 20, 10)
            pyxel.rect(128, 200, 5, 25, 0)
            pyxel.rect(160, 200, 5, 25, 0)
            pyxel.rect(242, 200, 5, 25, 0)
            pyxel.text(10,10, str(temps_score), 5)
            pyxel.rect(coureur_cent_x, 170 , 20, 50, 11)
            if finish == True:
                pyxel.cls(3)
                pyxel.text(60,100, "Bravo vous avez fait " + str(temps_score) + " secondes", 5)
                pyxel.text(60,110, "Pour rejouer appuyez sur J ", 5)
                pyxel.text(60,120, "Pour aller au menu appuyez sur M ", 5)

        
        
    def update():
        global clignotant, xmovH, ymovH, xjav, yjav, xjav2, yjav2, xmovM, xpoids, ypoids, descente_poids, start, haie, joueur_centdixmetres_y, vitesse_haie,score,vie_centdixmetre,haie_x,compteur_haie, lancer, course, score1, vitesse_haie, trait_x, trait_x2, trait_x3, trait_x4, trait_x5, lancer_jav, start2, foot, gardien_x,sens,x_ballon, y_ballon,GAME_OVER,Gagner, fleche_x, tir_au_but, lancer_poid, x_texte, y_texte, score, x_poids, y_poids, temps_max, puissance, x_score, x_charge
        if start == True :
            clignotant = clignotant + 1
            xmovH += 2
            xjav += 3
            yjav -= 1
            xjav2 += 3
            yjav2 -= 1
            xmovM += 2
            xpoids += 2
            a = 3/35
            ypoids = a*(xpoids - 165)**2-40*a*(xpoids-165)+180
            if pyxel.btn(pyxel.KEY_H) == True:
                start = False
                haie = True
            if pyxel.btn(pyxel.KEY_L) == True:
                start = False
                lancer = True
            if pyxel.btn(pyxel.KEY_J) == True:
                start = False
                course = True
            if pyxel.btn(pyxel.KEY_R) == True:
                start = False
                lancer_poid = True
            if pyxel.btn(pyxel.KEY_RIGHT) == True:
                start = False
                start2 = True
        if start2 == True:
            clignotant = clignotant + 1
            if pyxel.btn(pyxel.KEY_LEFT) == True:
                start2 = False
                start = True
            if pyxel.btn(pyxel.KEY_Y) == True:
                start2 = False
                foot = True
        if foot == True:
            if GAME_OVER == False and Gagner == False:
               tir()
               test_but()
               print(calcule_distance_tab(x_ballon, (gardien_x+40)/2, y_ballon, 165/2))
            if sens ==1:
                gardien_x += 7
                if gardien_x > 195:
                    sens = 2
            if sens == 2:
                gardien_x -=7
                if gardien_x < 20:
                    sens = 1
            if tir_au_but == True:
                x_ballon = fleche_x
                y_ballon = y_ballon - 5
            if GAME_OVER == True or Gagner == True:
                if pyxel.btn(pyxel.KEY_O):
                    But=False
                    gardien_x = 115
                    sens = 1
                    fleche_x = 115
                    tir_au_but = False
                    x_ballon = 115
                    y_ballon = 190
                    Gagner = False
                    GAME_OVER = False
            if pyxel.btn(pyxel.KEY_M) == True:
                foot = False
                start = True
        if lancer_poid == True:
            score_poids()
            poids_charge()
            chrono()
            lancer_poids()
            rejouer()
            if pyxel.btn(pyxel.KEY_M) == True:
                lancer_poid = False
                start = True
        if haie == True :
            if vie_centdixmetre > 0:
               move_joueur_centdixmetre()
               obstacle_haie()
               test_collision()
               score1 += 0.1
               score1 = round(score1, 2)
            if pyxel.btn(pyxel.KEY_H):
               joueur_centdixmetres_y = 150
               haie_x = 280
               vitesse_haie = 8
               compteur_haie = 0
               vie_centdixmetre = 50
               score1 = 0
            if pyxel.btn(pyxel.KEY_M):
                haie = False
                start = True
            trait_x2 -= vitesse_haie
            trait_x3 -= vitesse_haie
            trait_x4 -= vitesse_haie
            trait_x5 -= vitesse_haie
            trait_x -= vitesse_haie
            if trait_x2 < 0:
                trait_x2 = 250
            if trait_x3 < 0:
                trait_x3 = 250
            if trait_x4 < 0:
                trait_x4 = 250
            if trait_x < 0:
                trait_x = 250
            if trait_x5 < 0:
                trait_x5 = 250
        if lancer == True :
            lancer_javelot()
            retry()
            lanceur_jav()
            score_javelot()
            if lancer_jav == True : 
                x_texte = 90
                y_texte = 80
                pyxel.text(x_texte, y_texte, "GAME OVER, PRESS R", 0)
                pyxel.text(120, 100, "score: " + str(score), 0)
            if pyxel.btn(pyxel.KEY_M):
                lancer = False
                start = True
                
        if course == True :
            global coureur_cent_x, temps_score,finish
            if finish == False:
                ligne_arrivee()
                coureur_cent()
                temps_score += 0.036
                temps_score = round(temps_score,2)
                coureur_cent_x += 0.2
            if pyxel.btnr(pyxel.KEY_J) == True and finish == True:
                temps_score = 0
                coureur_cent_x = 0
                finish = False
            if pyxel.btnr(pyxel.KEY_M) == True :
                course = False
                start = True


pyxel.run(update, draw)