import pyxel
import math
from random import randint

pyxel.init(160, 160, title="cartman-o")
pyxel.load("res.pyxres")

    ##############

play = 0 #0: acceuil, 1: cinématique, 2: jeu, 3: sélection de skin
ecran_selection = [0, 0]

cartmano_x = 80
cartmano_y = 125
cartmano_vitesse = 1
cartmano_poid = 0   #0: premier, 16: deuxieme, 36: troisieme
cartmano_skin = 32    #32: normal, 48: autoritah, sot, token lives matter

endurance = 10
endurance_skin = 112
endurance_mode = True
epuisement = False
score = 0
vies = 10
vies_skin = 112 

kfc_liste = []
kfc_chance = 15

chapeau_liste = []
chapeau_chance = 15

tacos_liste = []

fleche_x = 32
fleche_y = 66
dialogue = 0

test = False
d = {
    0: "bonjour, je me prenomme eric\ncartman. appuie sur espace\nafin d'ecouter ce que j'ai\na te dire.\n \ntu vas le faire, hein ?\ntu as interet !",
    1: "bien.\n \ntu vois, je suis un petit\ngarcon banal. rien de special, a\npart mes quelques crimes par-ci\npar-la et mes nombreuses\napparitions a la television.",
    2: "enfin, si, je dois dire\nque quelque chose me rend\ntout particulierement special...\nje suis le plus gros fan de\nkfc des etats-unis.\n \nnon, de la galaxie !",
    3: "par contre, au contraire,\nil y a quelque chose que\nje deteste par-dessus tout.\n \nles... chapeaux verts.\n \nsurtout lorsqu'ils \ntombent du ciel.",
    4: "ce qui est dommage\ncar la meteo annonce aujourd'hui\nune pluie de chapeaux verts,\navec des averses de kfc.",
    5: "c'est pourquoi tu dois m'aider!\n \nappuies sur -> ou <- pour\nme deplacer, et espace pour\ncourir. attrape les buckets kfc\nsans meme froller ces horribles\nchapeaux, car juste les\ntoucher pourrait en faire\napparaitre d'autres.",
    6: "bref, fais attention a ta \nvie et ton score et blablabla...\n \nah, et garde les yeux ouvert pour\nun tacos ! ca sauve des vies.\n \nbonne chance, j'imagine.",
    7: "   "
}

s = {}
for num in range (0, 11):
    s[num] = 112 + 8 * num
sstr = str(s)
    

    ##############
    
def selection_d_ecran(ecran_selection):
    global play
    if play == 0:
        if pyxel.btnr(pyxel.KEY_DOWN):
            ecran_selection[0] = 1
            pyxel.play(0, 2)
        elif pyxel.btnr(pyxel.KEY_UP):
            ecran_selection[0] = 0
            pyxel.play(0, 2)
        return ecran_selection[0]

def selection_d_ecran_2(ecran_selection):
    global play
    if play == 0:
        if pyxel.btnr(pyxel.KEY_RETURN):
            ecran_selection[1] = 1
        else:
            ecran_selection[1] = 0
        return ecran_selection[1]


def cartmano_deplacement(x):
    if pyxel.btn(pyxel.KEY_RIGHT):
        if x < 144:
            x = x + cartmano_vitesse
    if pyxel.btn(pyxel.KEY_LEFT):
        if x > 0:
            x = x - cartmano_vitesse
    return x

def cartmano_changement_vitesse(cartmano_vitesse):
    global endurance_mode, endurance_mode
    if epuisement == True:
        cartmano_vitesse = 1
    else:
        if pyxel.btn(pyxel.KEY_SPACE) and (pyxel.btn(pyxel.KEY_RIGHT) or pyxel.btn(pyxel.KEY_LEFT)):
            cartmano_vitesse = 3
            endurance_mode = False
        else: 
            cartmano_vitesse = 1
            endurance_mode = True
    return cartmano_vitesse
    return endurance_mode

def endurance_baisse():
    global cartmano_vitesse, endurance, endurance_mode, endurance_skin
    if cartmano_vitesse == 3:
        if pyxel.btn(pyxel.KEY_RIGHT) or pyxel.btn(pyxel.KEY_LEFT):
            endurance = endurance - 0.1
        if endurance < 0:
            endurance = 0
        return endurance

def endurance_recuperation():
    global endurance, endurance_mode, epuisement
    if endurance == 0:
        epuisement = True
        endurance_mode = True
    elif endurance == 10:
        epuisement = False
    return epuisement
    return endurance_mode

def endurance_monte(): 
    global endurance, endurance_mode, epuisement, endurance_skin
    if endurance_mode == True and epuisement == True:
        endurance = endurance + 0.1
    elif endurance_mode == True and epuisement == False:
        endurance = endurance + 0.2
    elif endurance_mode == False:
        endurance = endurance
    return endurance
    
def endurance_skin_changement(): ############################## range
    global endurance_skin
    """for key, value in s.items():
        if endurance_mode == True and epuisement == True:
            if math.ceil(endurance) == num:
                endurance_skin = endurance_skin - 8 * value
        if endurance_mode == True and epuisement == False:
            if math.ceil(endurance) == num:
                endurance_skin = endurance_skin - 16 * value
        if cartmano_vitesse == 3 and (pyxel.btn(pyxel.KEY_RIGHT) or pyxel.btn(pyxel.KEY_LEFT)):
            if math.ceil(endurance) == num:
                endurance_skin = endurance_skin + 8 * value
        return endurance_skin"""
    for i in range (0, 11):
        if math.ceil(endurance) == i:
            endurance_skin = s[i]
            play(0, 1)
        return endurance_skin
        


def kfc_creation(kfc_liste):
    global kfc_chance
    if pyxel.frame_count % kfc_chance == 0:
        kfc_liste.append([randint(0, 144), 0])

def kfc_mouvement(kfc_liste):
    for kfc in kfc_liste:
        kfc[1] = kfc[1] + 2
        if kfc[1] >= 125:
            kfc_liste.remove(kfc)
    return kfc_liste

def kfc_attrapage(kfc_liste, cartmano_x, cartmano_y):
    global score, kfc_chance
    for kfc in kfc_liste:
        if kfc[0] <= cartmano_x + 16\
            and kfc[1] <= cartmano_y + 16 \
            and kfc[0] + 16 >= cartmano_x \
            and kfc[1] + 16 >= cartmano_y:
                kfc_liste.remove(kfc)
                score = score + 1
                kfc_chance = kfc_chance + 1
                pyxel.play(0, 8)
        return kfc_liste

def chapeau_creation(chapeau_liste):
    if pyxel.frame_count % chapeau_chance == 0:
        chapeau_liste.append([randint(0, 144), 0])
        
def chapeau_mouvement(chapeau_liste):
    for chapeau in chapeau_liste:
        chapeau[1] = chapeau[1] + 4
        if chapeau[1] >= 125:
            chapeau_liste.remove(chapeau)
    return chapeau_liste
    
def chapeau_attrapage(chapeau_liste, cartmano_x, cartmano_y):
    global chapeau_chance, chapeau_vitesse, vies, vies_skin
    for chapeau in chapeau_liste:
        if chapeau[0] <= cartmano_x + 16\
            and chapeau[1] <= cartmano_y + 16 \
            and chapeau[0] + 16 >= cartmano_x \
            and chapeau[1] + 16 >= cartmano_y:
                chapeau_liste.remove(chapeau)
                chapeau_chance = chapeau_chance - 1
                vies = vies - 1
                vies_skin = vies_skin + 8
                pyxel.play(0, 1)
        return chapeau_liste
    return kfc_liste

def tacos_creation(tacos_liste):
    global vies
    if vies <= 4:
        if pyxel.frame_count % 200 == 0:
            tacos_liste.append([randint(0, 144), 0])
            pyxel.playm(0)

def tacos_mouvement(tacos_liste):
    for tacos in tacos_liste:
        tacos[1] = tacos[1] + 1.5
        if tacos[1] >= 125:
            tacos_liste.remove(tacos)
        return tacos_liste

def tacos_attrapage(tacos_liste, cartmano_x, cartmano_y):
    global vies, vies_skin, score, chapeau_chance
    for tacos in tacos_liste:
            if tacos[0] <= cartmano_x + 16\
                and tacos[1] <= cartmano_y + 16 \
                and tacos[0] + 16 >= cartmano_x \
                and tacos[1] + 16 >= cartmano_y:
                    tacos_liste.remove(tacos)
                    vies = vies + 4
                    score = score + 3
                    vies_skin = vies_skin - 32
                    chapeau_chance = chapeau_chance + 3
                    pyxel.play(0, 10)
                    pyxel.stop
    return tacos_liste


def fleche_deplacement(x):
        if pyxel.btnr(pyxel.KEY_RIGHT):
            if x <= 80:
                x = x + 24
                pyxel.play(0, 4)
            else:
                pyxel.play(0, 3)
        elif pyxel.btnr(pyxel.KEY_LEFT):
            if x >= 56:
                x = x - 24
                pyxel.play(0, 4)
            else:
                pyxel.play(0, 3)
        return x
    
def skin_changement():
    global cartmano_skin      
        #fleche_x, cartmano_skin
    if fleche_x == 32:
        cartmano_skin = 32
    elif fleche_x == 56:
        cartmano_skin = 48
    elif fleche_x == 80:
        cartmano_skin = 64
    elif fleche_x == 104:
        cartmano_skin = 80
    return cartmano_skin
    
def dialogue_changement():
    global dialogue
    if pyxel.btnr(pyxel.KEY_SPACE):
        dialogue = dialogue + 1
        pyxel.play(0, 4)
    return dialogue
    
    #########################

def update():
    global play, ecran_selection, cartmano_x, cartmano_vitesse, cartmano_poid, fleche_x, cartmano_skin, dialogue, vies, score, endurance, endurance_mode, epuisement
    
    if play == 0:
        selection_d_ecran(ecran_selection)
        selection_d_ecran_2(ecran_selection)
        if ecran_selection == [0, 1] :
            play = 1 ###################
            pyxel.play(0, 7)
        elif ecran_selection == [1, 1] :
            play = 3
            pyxel.play(0, 5)

    elif play == 1:
        dialogue_changement()
        if dialogue > 6:
            play = 2
            pyxel.play(0, 7)


    elif play == 2:
        cartmano_x = cartmano_deplacement(cartmano_x)
        cartmano_vitesse = cartmano_changement_vitesse(cartmano_vitesse)
        
        endurance_baisse()
        endurance_recuperation()
        endurance_monte()
        endurance_skin_changement()
        if endurance > 10:
            endurance = 10
            
        kfc_creation(kfc_liste)
        kfc_mouvement(kfc_liste)
        kfc_attrapage(kfc_liste, cartmano_x, cartmano_y)
        
        chapeau_creation(chapeau_liste)
        chapeau_mouvement(chapeau_liste)
        chapeau_attrapage(chapeau_liste, cartmano_x, cartmano_y)
    
        tacos_creation(tacos_liste)
        tacos_mouvement(tacos_liste)
        tacos_attrapage(tacos_liste, cartmano_x, cartmano_y)
        if vies > 10:
            vies = 10
    
    elif play == 3:
        fleche_x = fleche_deplacement(fleche_x)
        skin_changement()
        if pyxel.btnr(pyxel.KEY_SPACE):
            play = 0
            pyxel.play(0, 6)
        
    ##############
    
def draw():
    global cartmano_poid, cartmano_skin, vies_skin, endurance_skin
    
    if play == 0:
        if ecran_selection[0] == 0:
            pyxel.blt(0, 0, 1, 0, 0, 160, 160)
        elif ecran_selection[0] == 1:
            pyxel.blt(0, 0, 2, 0, 0, 160, 160)
        pyxel.text(35, 145, "- entree pour valider -", 7)
        
        
    elif play == 1:
        pyxel.cls(0)
        pyxel.blt(72, 50, 0, 48, cartmano_skin, 16, 16, 13)
        pyxel.text(15, 90, d[dialogue], 7)
        
    elif play == 2:
        pyxel.cls(6)
        pyxel.bltm(0, 0, 0, 72*8, 0, 160, 160, 13)
        pyxel.bltm(0, 0, 0, 24*8, 0, 160, 160, 13)
        pyxel.bltm(0, 0, 0, 48*8, 0, 160, 160, 13)
        pyxel.blt(cartmano_x,cartmano_y, 0, cartmano_poid, cartmano_skin, 16, 16, 13)
        for kfc in kfc_liste:
            pyxel.blt(kfc[0],kfc[1], 0, 32, 96, 16, 16, 3)
        for chapeau in chapeau_liste:
            pyxel.blt(chapeau[0],chapeau[1], 0, 16, 96, 16, 16, 13)
        for tacos in tacos_liste:
            pyxel.blt(tacos[0],tacos[1], 0, 48, 96, 16, 16, 13)
            
        pyxel.blt(1, 1, 0, 0, 240, 160, 16, 3)
        pyxel.blt(51, 5, 0, 16, vies_skin, 42, 8, 13)
        #pyxel.blt(114, 20, 0, 64, endurance_skin, 42, 8, 13)
        
        #pyxel.text(50, 4, f"vies : {vies}", 7)
        pyxel.text(6, 6, f"score : {score}", 7)
        pyxel.text(98, 6, f"endurence : {math.ceil(endurance)}", 7)
        #pyxel.text(4, 20, f"test : {sstr}", 0)
        #pyxel.text(4, 29, f"epuisement :  aaaa {endurance_mode}", 0)
    
    
    
    elif play == 3:
        pyxel.cls(6)
        pyxel.bltm(0, 0, 0, 0, 0, 160, 160, 13)
        
        pyxel.blt(fleche_x, fleche_y, 0, 0, 96, 16, 16, 13)
        
        pyxel.text(20, 118, "appuyez sur espace pour quitter\napres selection !", 1)
    
    
    
    ##############    

pyxel.run(update, draw)