import pyxel
from random import randint

pyxel.init(256, 256, title="Super Jeu")
pyxel.load("res_fruit.pyxres")

#Coordonnées des images en début de jeu
x_fruit = 50
y_fruit = 0
x_cadie = 128
y_cadie = 240
x_laser = 1000
y_laser = 1000
x_explo = 1000
y_explo = 1000

#Score au début
score = 0
burgers_explosés = 0
burgers_attrapés = 0

background = 0

#SECRET CONFIDENCIEL
secret = 0

#Joue une musique horible crée par moi-même 😀
pyxel.playm(0, True)

##################
# GESTION DU JEU #
##################

def update():
    tomber_fruit()
    mouv_cadie()
    tir()
    difficulté()
    code_secret()
    droite()
    haut()
    gauche()
    bas()
    A()
    B()
    
def draw():
    pyxel.cls(background)
    afficher_fruit()
    afficher_cadie()
    afficher_tir()
    nbr_point()
    game_over()
    explosion()
    afficher_explosion()
    
########################
# PROGRAMME PRINCIPAL  #
########################

#Affiche le tir de la librairie
def afficher_tir():
    global x_laser, y_laser
    pyxel.blt(x_laser, y_laser, 0, 16, 16, 16, 16, 0)

#Affiche l'explosion de la librairie
def afficher_explosion():
    global x_explo, y_explo
    pyxel.blt(x_explo, y_explo, 0, 32, 16, 16, 16, 0)
    
#Affiche le cadie de la librairie
def afficher_cadie():
    global x_cadie, y_cadie
    pyxel.blt(x_cadie, y_cadie, 0, 0, 16, 16, 16, 0)

#Affiche un fruit aléatoire de la librairie
def afficher_fruit():
    global x_fruit, y_fruit, fruit_aléatoire
    if y_fruit <= 10:
        fruit_aléatoire = randint(0, 3)
    x_image = 16 * fruit_aléatoire
    pyxel.blt(x_fruit, y_fruit, 0, x_image, 0, 16, 16, 0)

#Change la difficulté en fonction du score
def difficulté():
    global score, y_fruit, x_fruit, background
    if score < 10 :
        y_fruit = y_fruit + 3
        background = 1
    if score >= 10 and score < 20 :
        y_fruit = y_fruit + 4
        background = 2
    if score >= 20 and score < 30 :
        y_fruit = y_fruit + 5
        background = 3
    if score >= 30 and score < 40 :
        y_fruit = y_fruit + 7 
        x_fruit = x_fruit + randint(-1,1)
        background = 4
    if score >= 40 and score < 50 :
        y_fruit = y_fruit + 8
        x_fruit = x_fruit + randint(-1,1)
        background = 5
    if score >= 50 :
        y_fruit = y_fruit + 10
        x_fruit = x_fruit + randint(-2,2)
        background = 0

#Permet d'utiliser les toucher pour bouger le cadie
def mouv_cadie():
    global x_cadie, y_cadie
    if pyxel.btn(pyxel.KEY_RIGHT):
        if (x_cadie < 242) :
            x_cadie = x_cadie + 10
    if pyxel.btn(pyxel.KEY_LEFT):
        if (x_cadie > 0) :
            x_cadie = x_cadie - 10

#Fais tomber un fruit
#Fais réaparaitre un fruit lorsque le cadie le touche
#Ajoute 1 au score si le cadie touche un fruit
#Enlève 1 au score et ajoute 1 à burgers_attrapés si le cadie touche un burger
def tomber_fruit():
    global x_fruit, y_fruit, x_cadie, y_cadie, score, burgers_attrapés
    if calculer_distance(x_fruit, y_fruit, x_cadie, y_cadie ) <= 70:
        if fruit_aléatoire == 3:
            y_fruit = 0
            x_fruit = randint(20, 240)
            score = score - 1
            burgers_attrapés = burgers_attrapés + 1
        else:
            y_fruit = 0
            x_fruit = randint(20, 240)
            score = score + 1

#Tir lorsque espace est appuyé
def tir():
    global x_laser, y_laser
    y_laser = y_laser - 10
    if pyxel.btnr(pyxel.KEY_SPACE):
        x_laser = x_cadie
        y_laser = 230

#Va faire diparaitre le burger lorque celui ci est touché par un laser
#Rajoute 1 au score et 1 à burgers_explosés si le burger est toucher
#Fait apparaitre une explosion
def explosion():
    global x_laser, y_laser, x_fruit, y_fruit, fruit_aléatoire, score, burgers_explosés, x_explo, y_explo
    y_explo = y_explo - 15
    if fruit_aléatoire == 3:
        if calculer_distance(x_fruit, y_fruit, x_laser, y_laser) <= 70:
            x_explo = x_fruit
            y_explo = y_fruit
            score = score + 1
            burgers_explosés = burgers_explosés + 1
            y_fruit = 0
            x_fruit = randint(20, 240)
            fruit_aléatoire = randint(0, 3)
            x_image = 16 * fruit_aléatoire
            pyxel.blt(x_fruit, y_fruit, 0, x_image, 0, 16, 16, 0)

#Affiche le score pendant et à la fin de la partie
def nbr_point():
    global score, y_fruit, burgers_explosés, burgers_attrapés
    if y_fruit < 260 :
        pyxel.text(10, 10, "Point:" + str(score), 7)
    elif y_fruit > 260 :
         pyxel.text(80, 110, "Tu as fait " + str(score) + ", bien joue!", 7)
         pyxel.text(60, 120, "Tu as tire sur " + str(burgers_explosés) + " Burgers, Bravo a toi!", 7)
         pyxel.text(70, 130, "Tu as attrapes " + str(burgers_attrapés) + " Burgers, Beurk!", 7)

#Permet de finir la partie
def game_over():
    global y_fruit
    if y_fruit > 260 :
        pyxel.text(108, 100, "Game Over", 7)

#############################
# Fonctions Suplémentaires  #
#############################

#Calcul Pythagore
def calculer_distance(x1, y1, x2, y2):
    return (x2 - x1)**2 + ( y2 - y1)**2
    
#######################
# SECRET CONFIDENCIEL #
#######################

def droite():
    global secret
    if pyxel.btnr(pyxel.KEY_RIGHT):
        secret = secret + 1
    
def gauche():
    global secret
    if pyxel.btnr(pyxel.KEY_LEFT):
        secret = secret + 10
    
def haut():
    global secret
    if pyxel.btnr(pyxel.KEY_UP):
        secret = secret + 100
    
def bas():
    global secret
    if pyxel.btnr(pyxel.KEY_DOWN):
        secret = secret + 1000
    
def A():
    global secret
    if pyxel.btnr(pyxel.KEY_A):
        secret = secret + 10000
    
def B():
    global secret
    if pyxel.btnr(pyxel.KEY_B):
        secret = secret + 100000

def code_secret():
    global secret, score
    if secret == 112222 :
        score = score + 1000

#Lance le jeu
pyxel.run(update, draw,)