import pyxel
import random
obstacles_liste = []
pyxel.init(200,200)
pyxel.load("res_final.pyxres")
pyxel.colors[11]=0x008000
pyxel.colors[8]=0xFF0000
x_perso1 = 1
y_perso1 = 184
x_perso2=100
y_perso2=100
jeu = 0
img = 0
compteur = 0
cooldown = 300
cooldown1 = 300
capa = 0
argent = 100
tung = 0
trala = 0
singe = 0
cafe = 0
bombardino = 0
ressources = [0,0,0,0,0]
slot=[False,False,False,False,False,False]
compteurcutscene = 0
brainrotliste = []
prestige = 1
width=16
width2=16
direction=48
accé=2
accé1=1
accéinverse=24
accéperso=1


def acceleration():
    global accé, compteur, accéinverse, accé1, accéperso
    if compteur>=accé*200:
        accé+=1
    if compteur>=accé1*150 and compteur<=1200:
        accé1+=1
        accéinverse-=2
        accéperso+=1


def obstacles_creation(x,y,obstacles_liste):
    global accéinverse
    xyz=accéinverse
    if pyxel.frame_count%xyz == 0:
        obstacles_liste.append([x,y])

def ob_deplacement(obstacles_liste):
    for element in obstacles_liste:
        element[1] +=accé
        if element[1] >= 216:
            obstacles_liste.remove(element)
def colision():
    global x_perso1, y_perso1, jeu
    for element in obstacles_liste:
        if element[0] >= x_perso1 - 28 and element[0] <= x_perso1 + 16 and element[1] >= y_perso1 - 28 and element[1] <= y_perso1 + 16:
            jeu = 2
def cheat():
    global compteur
    if pyxel.btn(pyxel.KEY_M):
        compteur += 100
def shop():
    if pyxel.btn(pyxel.KEY_N):
        pyxel.blt(31,66,0,0,96,144,64)
def cheat1():
    global argent
    if pyxel.btn(pyxel.KEY_M):
        argent += 100
def mouvement():
    if jeu==0:
        global x_perso1, y_perso1, img, cooldown, capa, cooldown1,width, accéperso
        if pyxel.btn(pyxel.KEY_D) and x_perso1<=196:
            if pyxel.frame_count%10>5:
                img=16
                width=16
            else:
                img=0
                width=16
            x_perso1 += accé
        if pyxel.btn(pyxel.KEY_Q) and x_perso1>=-12:
            if pyxel.frame_count%10>5:
                img=16
                width=-16
            else:
                img=0
                width=-16
            x_perso1 -= accé
        if pyxel.btn(pyxel.KEY_A) and cooldown >= 150 :
            cooldown = 0
            capa = 1
            x_perso1 -= 75
            if x_perso1 <= -14:
                x_perso1 = -14
        if pyxel.btn(pyxel.KEY_E) and cooldown1 >= 150 :
            cooldown1 = 0
            pyxel.play(0,0)
            capa = 1
            x_perso1 += 75
            if x_perso1 >= 214:
                x_perso1 = 214


def cutscene():
    global compteurcutscene
    pyxel.cls(14)
    if pyxel.frame_count%60<55:
        pyxel.text(0,25,"Vous avez debloque le monde secret des brainrots !!!!",7)
        pyxel.text(0,50,"Ici vous pouvez acheter des personnages !!",7)
        pyxel.text(0,75,"Un Tralalero Tralala coute 400 et",7)
        pyxel.text(0,85,"rapporte 4 par seconde.",7)
        pyxel.text(0,100,"Un Tung Tung Tung Sahur coute 200 et",7)
        pyxel.text(0,110,"rapporte 2 par seconde.",7)
        pyxel.text(0,125,"Un Cappucino Assassino coute 300 et",7)
        pyxel.text(0,135,"rapporte 3 par seconde.",7)
        pyxel.text(0,150,"Un Chimpanzini Bananini coute 100",7)
        pyxel.text(0,160,"et rapporte 1 par seconde.",7)
        pyxel.text(0,175,"Un Bombardino Crocodilo coute 500 et",7)
        pyxel.text(0,185,"rapporte 5 par seconde.",7)
    if pyxel.btn(pyxel.KEY_SPACE):
        compteurcutscene -= 1

def brainrot_creation():
    global brainrotliste, tung,trala, noob
    a = random.randint(0,125)
    if a <25:
        brainrotliste.append([-32,32,0,32,13,2])#x,y,x de la ou on prend l'image, le y comme précédent, et l'argent généré
    elif a <50 and a >= 25:
        brainrotliste.append([-32,32,32,32,15,5])
    elif a<75 and a >= 50:
        brainrotliste.append([-32,32,64,32,13,1])
    elif a<100 and a >=75:
        brainrotliste.append([-32,32,96,32,8,3])
    elif a < 125 and a >= 100:
        brainrotliste.append([-32,32,128,32,9,4])

def clicker():
    global x_perso2, y_perso2, argent
    pyxel.blt(168,134,0,0,64,32,32,0)
    if 168<x_perso2<200 and 134<y_perso2<166 and pyxel.btnp(pyxel.KEY_SPACE): argent +=1

def prestige1():
    global argent,prestige,brainrotliste,jeu,slot
    if prestige == 1 and argent >=400:
        pyxel.text(15,125,"press 0 for prestige",8)
        if pyxel.btn(pyxel.KEY_0):
            brainrotliste = []
            argent = 0
            prestige +=1
            for element in brainrotliste:
                brainrotliste.remove(element)
            slot=[False,False,False,False,False,False]
    if prestige == 2 and argent >= 3200:
        pyxel.text(15,125,"press 0 for prestige",8)
        if pyxel.btn(pyxel.KEY_0):
            brainrotliste = []
            argent = 0
            prestige +=1
            for element in brainrotliste:
                brainrotliste.remove(element)
            slot=[8,False,False,False,False,False]
            brainrotliste.append([0,164,160,32,13,8])
    if prestige == 3 and argent >= 12800:
        pyxel.text(15,125,"press 0 for prestige",8)
        if pyxel.btn(pyxel.KEY_0):
            jeu = 3




def achat():
    global brainrotliste,slot,trala,tung,singe,cafe,bombardino,argent
    for element in brainrotliste:
        if x_perso2 + 16 >= element[0] >= x_perso2 - 31 and y_perso2 + 16 >=element[1] >= y_perso2 - 30 and element[1]!=164 and element[5]*100<=argent:
            pyxel.text(15,100,"choose slot 1-6",7)
            if pyxel.btn(pyxel.KEY_R):
                if slot[0]==False:
                    element[0]=0
                    element[1]=164
                    slot[0]=element[5]
                else:
                    for truc in brainrotliste:
                        if truc[1]==164 and truc[0]==0:
                            brainrotliste.remove(truc)
                    element[0]=0
                    element[1]=164
                    slot[0]=element[5]
                argent-=element[5]*100
            elif pyxel.btn(pyxel.KEY_T):
                if slot[1]==False:
                    element[0]=33
                    element[1]=164
                    slot[1]=element[5]
                else:
                    for truc in brainrotliste:
                        if truc[1]==164 and truc[0]==33:
                            brainrotliste.remove(truc)
                    element[0]=33
                    element[1]=164
                    slot[1]=element[5]
                argent-=element[5]*100
            elif pyxel.btn(pyxel.KEY_Y):
                if slot[2]==False:
                    element[0]=66
                    element[1]=164
                    slot[2]=element[5]
                else:
                    for truc in brainrotliste:
                        if truc[1]==164 and truc[0]==66:
                            brainrotliste.remove(truc)
                    element[0]=66
                    element[1]=164
                    slot[2]=element[5]
                argent-=element[5]*100
            elif pyxel.btn(pyxel.KEY_U):
                if slot[3]==False:
                    element[0]=99
                    element[1]=164
                    slot[3]=element[5]
                else:
                    for truc in brainrotliste:
                        if truc[1]==164 and truc[0]==99:
                            brainrotliste.remove(truc)
                    element[0]=99
                    element[1]=164
                    slot[3]=element[5]
                argent-=element[5]*100
            elif pyxel.btn(pyxel.KEY_I):
                if slot[4]==False:
                    element[0]=132
                    element[1]=164
                    slot[4]=element[5]
                else:
                    for truc in brainrotliste:
                        if truc[1]==164 and truc[0]==132:
                            brainrotliste.remove(truc)
                    element[0]=132
                    element[1]=164
                    slot[4]=element[5]
                argent-=element[5]*100
            elif pyxel.btn(pyxel.KEY_O):
                if slot[5]==False:
                    element[0]=165
                    element[1]=164
                    slot[5]=element[5]
                else:
                    for truc in brainrotliste:
                        if truc[1]==164 and truc[0]==165:
                            brainrotliste.remove(truc)
                    element[0]=165
                    element[1]=164
                    slot[5]=element[5]
                argent-=element[5]*100

def mouvement2():
    if jeu ==1:
        global x_perso2, y_perso2,direction,width2
        if pyxel.btn(pyxel.KEY_D) and x_perso2<=196:
            x_perso2 += 2
            if pyxel.frame_count%15>=7:
                direction=0
                width2=-16
            else:
                direction =64
                width2=-16
        if pyxel.btn(pyxel.KEY_Q) and x_perso2>=-12:
            x_perso2 -= 2
            if pyxel.frame_count%15>=7:
                direction=0
                width2=16
            else:
                direction = 64
                width2=16
        if pyxel.btn(pyxel.KEY_S) and y_perso2<=196:
            y_perso2 += 2
            if 15>pyxel.frame_count%30>=7:
                direction=16
                width2=16
            elif 22>pyxel.frame_count%30>=15 or 7>pyxel.frame_count%30>=0:
                direction=48
            elif 30>pyxel.frame_count%30>=22:
                direction=16
                width2=-16
        if pyxel.btn(pyxel.KEY_Z) and y_perso2>=-12:
            y_perso2 -= 2
            direction=32
            width2=16

def update():
    global x_perso1
    global obstacles_liste
    global compteur,accéperso, cooldown, cooldown1, capa, jeu, argent, tung, noob,trala, compteurcutscene,slot, accé, accé1, accéinverse
    if jeu == 0:
        a = random.randint(-16,200)
        acceleration()
        obstacles_creation(a,0,obstacles_liste)
        ob_deplacement(obstacles_liste)
        mouvement()
        colision()
        compteur += 1
        if compteur >= 2200:
            jeu = 1
            compteurcutscene +=1
        cooldown += 1
        cooldown1 += 1
        cheat()

    elif jeu == 1:
        if pyxel.frame_count%30==1:
            for element in slot:
                if element!=False:
                    argent+=element*prestige
        prestige1()
        cheat1()
    elif jeu == 2:
        cooldown=150
        cooldown1=150
        accé=1
        accéinverse=24
        accé1=1
        compteur = 0

def draw():

    global x_perso1, jeu, obstacles_liste, capa, compteur, argent, compteurcutscene,slot,width,brainrotliste
    if jeu == 0:
        pyxel.cls(3)
        if capa == 1 :
            pyxel.cls(14)
            pyxel.play(0,0)
            capa = 0
        score = pyxel.frame_count
        pyxel.text(15,20,"SCORE :" + str(compteur),7)
        pyxel.blt(x_perso1,y_perso1,0,img,0,width,16,0)


        for element in obstacles_liste:
            if element[0] >= x_perso1 - 28 and element[0] <= x_perso1 + 16 :
                pyxel.blt(element[0],element[1],0,33,1,28,28,14)
            else:
                pyxel.blt(element[0],element[1],0,65,1,28,28,14)

    elif jeu == 1:
        pyxel.cls(11)
        pyxel.text(15,20,"ARGENT : "+str(argent),18)
        clicker()
        if prestige==1:
            pyxel.blt(x_perso2,y_perso2,2,direction,0,width2,16,7)
        elif prestige >= 2:
            pyxel.blt(x_perso2,y_perso2,2,direction,16,width2,16,7)
        if pyxel.frame_count%300 == 0:
            brainrot_creation()
        for element in brainrotliste:
            pyxel.blt(element[0],element[1],0,element[2],element[3],32,32,element[4])

        if pyxel.frame_count%6==0:
            for element in brainrotliste:
                if element[1]!=164:
                    element[0] +=1
        achat()
        mouvement2()
        shop()
        prestige1()

        if compteurcutscene > 0:
            cutscene()
    elif jeu == 2 :
        pyxel.cls(8)
        pyxel.blt(0,100,1,0,32,200,31,7)
        pyxel.mouse = True
        compteur  = 0

        if pyxel.btn(pyxel.KEY_SPACE):
            jeu = 0
            compteur = 0
            obstacles_liste = []
    elif jeu == 3:
        for a in range(200):
            pyxel.rect(0,a,200,1,a%15+1)

        brainrotliste=[]
        brainrotliste.append([4,32,0,32,13,2])
        brainrotliste.append([36,32,32,32,15,5])
        brainrotliste.append([68,32,64,32,13,1])
        brainrotliste.append([100,32,96,32,8,3])
        brainrotliste.append([132,32,128,32,9,4])
        brainrotliste.append([164,164,160,32,13,8])
        for element in brainrotliste:
            pyxel.blt(element[0],32,0,element[2],32,32,32,element[4])
            pyxel.text(20,125,"BRAVO VOUS ETES LE ROI DES PUANTS !!!!!",0)

pyxel.run(update, draw)