import pyxel

pyxel.init(168,168)
pyxel.load("res.pyxres")
pyxel.playm(0, 0, True)

carré1_x=60
carré1_y=152
carré2_x=30
carré2_y=152

directionJ1 = "aucune"
directionJ2 = "aucune"

vy1 = 0
vy2 = 0

VieJ1Max = 100
VieJ2Max = 100
VieJ1 = 100
VieJ2 = 100

detection_collision = False

longueur_barre_de_vieJ1 = 70
longueur_barre_de_vieJ2 = 70
largeur_barre_de_vieJ1 = 10
largeur_barre_de_vieJ2 = 10

durée_cooldown = 30
cooldown_attaqueJ1 = 0
cooldown_attaqueJ2= 0
cooldown_paradeJ1 = 0
cooldown_paradeJ2 = 0

combo1 = 0
combo2 = 0

parade1droite = False
parade1gauche = False
parade2droite = False
parade2gauche = False
duree_parade1 = 0
duree_parade2 = 0
duree_parade1max = 20
duree_parade2max = 20

etat_jeu = "actif"

def joueur1déplacement(x,y, direction1 : str):
    global saut1, vy1
    if pyxel.btn(pyxel.KEY_RIGHT):
        direction1 = "droite"
        if (x<152):
            x=x+4
    if pyxel.btn(pyxel.KEY_LEFT):
        direction1 = "gauche"
        if (x > 0) :
            x = x - 4

    if pyxel.btnp(pyxel.KEY_UP) and vy1 == 0:
        vy1 = -12
    vy1 += 1
    y += vy1
    if y > 152:
        y = 152
        vy1=0
    
    return x, y, direction1
    
def joueur2déplacement(a,b, direction2 : str):
    global vy2
    if pyxel.btn(pyxel.KEY_D):
        direction2 = "droite"
        if (a<152):
            a=a+4
    if pyxel.btn(pyxel.KEY_Q):
        direction2 = "gauche"
        if (a > 0) :
            a = a - 4
    if pyxel.btnp(pyxel.KEY_Z) and vy2 == 0:
        vy2 = -12
    vy2 += 1
    b += vy2
    if b > 152:
        b = 152
        vy2 = 0
            
    return a, b, direction2
    
def collisions(x,y,a,b):
    if x <= a+13.5 and x+13.5 >= a and y <= b+15 and y+15 >= b:
        detection_collision = True
    else:
        detection_collision = False
    return detection_collision


def attaqueJ1(x1,y1,x2,y2):
    global VieJ2, combo1, directionJ1, carré2_x
    if directionJ1 == "droite":
        if x1 <= x2 and x1+30 >= x2 and y1-10 <= y2 and y1 >= y2 and parade2gauche == False:
            VieJ2 = VieJ2 - 5
            carré2_x= carré2_x + 4
            combo1 = combo1 + 1
            if combo1 > 2:
                VieJ2 = VieJ2 - (combo1*5)
            
    elif directionJ1 == "gauche":
        if x1 >= x2 and x1-30 <= x2 and y1-10 <= y2 and y1 >= y2 and parade2droite == False:
            VieJ2 = VieJ2 - 5
            combo1 = combo1 + 1
            if combo1 > 2:
                VieJ2 = VieJ2 - (combo1*5)


def annulation1(x1,y1,x2,y2):
    global combo1, directionJ1
    if directionJ1 == "droite":
        if not x1 <= x2 and x1+30 >= x2 and y1-10 <= y2 and y1 >= y2:
            combo1 = 0
                
    elif directionJ1 == "gauche":
        if not x1 >= x2 and x1-30 <= x2 and y1-10 <= y2 and y1 >= y2:
            combo1 = 0    

            

def attaqueJ2(x1,y1,x2,y2):
    global VieJ1, combo2, directionJ2
    if directionJ2 == "droite":
        if x1 <= x2 and x1+30 >= x2 and y1-10 <= y2 and y1 >= y2 and parade1gauche == False:
            VieJ1 = VieJ1 - 5
            combo2 = combo2 + 1
            if combo2 > 2:
                VieJ1 = VieJ1 - (combo2*5)
    elif directionJ2 == "gauche":
        if x1 >= x2 and x1-30 <= x2 and y1-10 <= y2 and y1 >= y2 and parade1droite == False:
            VieJ1 = VieJ1 - 5
            combo2 = combo2 + 1
            if combo2 > 2:
                VieJ1 = VieJ1 - (combo2*5)


def annulation2(x1,y1,x2,y2):
    global combo2, directionJ2
    if directionJ2 == "droite":
        if not x1 <= x2 and x1+30 >= x2 and y1-10 <= y2 and y1 >= y2:
            combo2 = 0
    elif directionJ2 == "gauche":
        if not x1 >= x2 and x1-30 <= x2 and y1-10 <= y2 and y1 >= y2:
            combo2 = 0

    
def barre_de_vie_J(VieX,VieX_MAX,longueur_barre_de_vieJX, x, y, largeur):
    longueur_actuelle = int((VieX / VieX_MAX) * longueur_barre_de_vieJX)
    pyxel.rect(x, y, longueur_actuelle, largeur, 8)
    
    
def parade(directionJ : str, cooldown_paradeJ, paradedroite, duree_parade, duree_parademax, paradegauche):

    if directionJ == "droite" and cooldown_paradeJ == 0:
            paradedroite = True
            cooldown_paradeJ = durée_cooldown
            duree_parade = duree_parademax
    if directionJ == "gauche" and cooldown_paradeJ == 0:
            paradegauche = True
            cooldown_paradeJ = durée_cooldown
            duree_parade = duree_parademax
    return paradegauche, paradedroite, cooldown_paradeJ, duree_parade


def update():
    
    global carré1_x, carré1_y, carré2_x, carré2_y, directionJ1, directionJ2, cooldown_attaqueJ1, cooldown_attaqueJ2, vy1, vy2, parade1droite, parade1gauche, parade2droite, parade2gauche, duree_parade1, duree_parade2, cooldown_paradeJ1, cooldown_paradeJ2, etat_jeu
    
    if cooldown_attaqueJ1 > 0:
        cooldown_attaqueJ1 -= 1
    if cooldown_attaqueJ2 > 0:
        cooldown_attaqueJ2 -= 1
        
    if cooldown_paradeJ1 > 0:
        cooldown_paradeJ1 -= 1
    if cooldown_paradeJ2 > 0:
        cooldown_paradeJ2 -= 1    
    
        
    if duree_parade1 > 0:
        duree_parade1 -= 1
    if duree_parade2 > 0:
        duree_parade2 -= 1
    if duree_parade1 == 0:
        parade1droite= False
        parade1gauche= False
    if duree_parade2 == 0:
        parade2droite= False
        parade2gauche= False
    
    
    position_précédente_joueur1_x, position_précédente_joueur1_y = carré1_x, carré1_y
    position_précédente_joueur2_x, position_précédente_joueur2_y = carré2_x, carré2_y
    
    
    carré1_x, carré1_y, directionJ1 = joueur1déplacement(carré1_x, carré1_y, directionJ1)
    carré2_x, carré2_y, directionJ2 = joueur2déplacement(carré2_x, carré2_y, directionJ2)
    
    boom = collisions(carré1_x, carré1_y, carré2_x, carré2_y)
    if boom:
            carré1_x, carré1_y = position_précédente_joueur1_x, position_précédente_joueur1_y
            vy1= 0
            carré2_x, carré2_y = position_précédente_joueur2_x, position_précédente_joueur2_y
            vy2= 0

    if pyxel.btn(pyxel.KEY_M) and cooldown_attaqueJ1 == 0:
        attaqueJ1(carré1_x, carré1_y, carré2_x, carré2_y)
        annulation1(carré1_x, carré1_y, carré2_x, carré2_y)
        cooldown_attaqueJ1 = durée_cooldown

    if pyxel.btn(pyxel.KEY_B) and cooldown_attaqueJ2 == 0:
        attaqueJ2(carré2_x, carré2_y, carré1_x, carré1_y)
        annulation2(carré2_x, carré2_y, carré1_x, carré1_y)
        cooldown_attaqueJ2 = durée_cooldown
        
    if pyxel.btn(pyxel.KEY_L):
        parade1gauche, parade1droite, cooldown_paradeJ1, duree_parade1 = parade(directionJ1, cooldown_paradeJ1, parade1droite, duree_parade1, duree_parade1max, parade1gauche)
    if pyxel.btn(pyxel.KEY_V):
        parade2gauche, parade2droite, cooldown_paradeJ2, duree_parade2 = parade(directionJ2, cooldown_paradeJ2, parade2droite, duree_parade2, duree_parade2max, parade2gauche)
    
    if VieJ1 <1 or VieJ2 < 1:
        etat_jeu = "mort"
        
    
    
    


def draw():
    pyxel.cls(1)
    pyxel.blt(0, 0, 1, 0, 0, 168, 168,0)

    
    if directionJ1 == "aucune":
        if (pyxel.frame_count//5)%2 != 0:
            pyxel.blt(carré1_x, carré1_y, 0, 16, 32, 16, 16,7)
        else:
            pyxel.blt(carré1_x, carré1_y, 0, 16, 48, 16, 16,7)
    if directionJ1 == "gauche": 
        if (pyxel.frame_count//5)%2 != 0:
            pyxel.blt(carré1_x, carré1_y, 0, 0, 32, 16, 16,7)
        else:
            pyxel.blt(carré1_x, carré1_y, 0, 0, 48, 16, 16,7)

    if directionJ1 == "droite":
        if (pyxel.frame_count//5)%2 != 0:
            pyxel.blt(carré1_x, carré1_y, 0, 16, 32, 16, 16,7)
        else:
            pyxel.blt(carré1_x, carré1_y, 0, 16, 48, 16, 16,7)   
    
    
    
    if directionJ2 == "aucune":
        if (pyxel.frame_count//5)%2 != 0:
            pyxel.blt(carré2_x, carré2_y, 0, 0, 0, 16, 16,7)
        else:
            pyxel.blt(carré2_x, carré2_y, 0, 0, 16, 16, 16,7)
    if directionJ2 == "gauche":    
        if (pyxel.frame_count//5)%2 != 0:
            pyxel.blt(carré2_x, carré2_y, 0, 16, 0, 16, 16,7)
        else:
            pyxel.blt(carré2_x, carré2_y, 0, 16, 16, 16, 16,7)
    if directionJ2 == "droite":
        if (pyxel.frame_count//5)%2 != 0:
            pyxel.blt(carré2_x, carré2_y, 0, 0, 0, 16, 16,7)
        else:
            pyxel.blt(carré2_x, carré2_y, 0, 0, 16, 16, 16,7)    
            
    

        
        
        
    barre_de_vie_J(VieJ1, VieJ1Max, longueur_barre_de_vieJ1, 90, 10, largeur_barre_de_vieJ1)
    barre_de_vie_J(VieJ2, VieJ2Max, longueur_barre_de_vieJ2, 10, 10, largeur_barre_de_vieJ2)
    if etat_jeu == "mort":
        pyxel.cls(0)
    
        if VieJ1 <= 0:
            pyxel.text(47, 80, "LE JOUEUR 2 GAGNE", 7)
        elif VieJ2 <= 0:
            pyxel.text(47, 80, "LE JOUEUR 1 GAGNE", 7)
pyxel.run(update, draw)