import pyxel
import time

class App:
    def __init__(self):
        pyxel.init(250, 78, title="BABO Fighting", quit_key=pyxel.KEY_M, fps=60)
        pyxel.load("res.pyxres")
        pyxel.playm(0, loop=True)
        self.player = Player1()
        self.player2 = Player2()
        self.background = Background()
        self.bannierROSE = BannierROSE()
        self.bannierBLEU = BannierBLEU()
        self.paused = False
        self.pause_button = pyxel.KEY_SPACE
        self.last_attack_time = 0
        self.game_over = False
        pyxel.playm(0, loop=True)
        pyxel.run(self.update, self.draw)
        
        
    def update(self):
        if not self.game_over and not self.paused:
            if pyxel.btn(pyxel.KEY_LEFT):
                self.player.move_left()
            elif pyxel.btn(pyxel.KEY_RIGHT):
                self.player.move_right()
            elif pyxel.btn(pyxel.KEY_UP):
                self.player.jump()
            elif pyxel.btn(pyxel.KEY_S):  # Touche S pour attaquer avec le joueur 2
                self.player2.attack()
                
            if pyxel.btn(pyxel.KEY_Q):
                self.player2.move_left()
            elif pyxel.btn(pyxel.KEY_D):
                self.player2.move_right()   
            elif pyxel.btn(pyxel.KEY_Z):
                self.player2.jump()
            elif pyxel.btn(pyxel.KEY_DOWN):  # Touche Descendre pour attaquer avec le joueur 1
                self.player.attack()
    
            if pyxel.btnp(pyxel.KEY_SPACE):
                self.paused = True
        else:
            if pyxel.btnp(pyxel.KEY_SPACE):
                if self.game_over:
                    self.restart_game()
                else:
                    self.paused = False

        self.player.update()
        self.player2.update()

        # Vérifier si l'un des joueurs dépasse l'autre
        if self.player.x < self.player2.x:
            self.player.flipped = False
            self.player2.flipped = True
        elif self.player.x > self.player2.x:
            self.player.flipped = True
            self.player2.flipped = False
        else:
            self.player.flipped = False
            self.player2.flipped = False
        
        # Vérifier si les joueurs sont à portée d'attaque
        if abs(self.player.x - self.player2.x) <= 20:
            # Vérifier si l'attaquant est en train d'attaquer
            if self.player.attacking and not self.player2.is_jumping:  # Vérifier si le joueur 2 n'est pas en train de sauter
                # Vérifier si l'attaque est valide
                current_time = time.time()
                if current_time - self.last_attack_time >= 0.9:
                    self.player2.HP -= 10
                    if self.player2.HP <= 0:
                        self.player2.HP = 0
                        self.game_over = True
                    self.last_attack_time = current_time
            if self.player2.attacking and not self.player.is_jumping:  # Vérifier si le joueur 1 n'est pas en train de sauter
                # Vérifier si l'attaque est valide
                current_time = time.time()
                if current_time - self.last_attack_time >= 0.9:
                    self.player.HP -= 10
                    if self.player.HP <= 0:
                        self.player.HP = 0
                        self.game_over = True
                    self.last_attack_time = current_time
        pyxel.playm(0, loop=True)
    def draw(self):
        pyxel.cls(0)
        pyxel.blt(
            self.background.x1,
            self.background.y1,
            self.background.IMG1,
            self.background.U1,
            self.background.V1,
            self.background.WIDTH1,
            self.background.HEIGHT1,
            )
        pyxel.blt(
            self.bannierROSE.x5,
            self.bannierROSE.y5,
            self.bannierROSE.IMG5,
            self.bannierROSE.U5,
            self.bannierROSE.V5,
            self.bannierROSE.WIDTH5,
            self.bannierROSE.HEIGHT5,
            self.bannierROSE.COOLKEY5,
            )
        pyxel.blt(
            self.bannierBLEU.x6,
            self.bannierBLEU.y6,
            self.bannierBLEU.IMG6,
            self.bannierBLEU.U6,
            self.bannierBLEU.V6,
            self.bannierBLEU.WIDTH6,
            self.bannierBLEU.HEIGHT6,
            self.bannierBLEU.COOLKEY6,
            )
        self.player.draw()
        self.player2.draw()
        if self.paused:
            pyxel.text(110, 38, "PAUSE", pyxel.COLOR_YELLOW)  # Affichage du texte "PAUSE" en jaune, plus gros
        elif self.game_over:
            pyxel.text(108, 18, "GAME OVER", pyxel.frame_count%10)
            pyxel.text(90, 28, "Press SPACE to replay", pyxel.COLOR_YELLOW)
        pyxel.playm(3, True)
        pyxel.playm(0, True)
        pyxel.music(0)
        pyxel.music(3)
    
        
    def restart_game(self):
        self.player.HP = 100
        self.player2.HP = 100
        self.game_over = False
        self.paused = False
        self.player.x = 200
        self.player2.x = 30

class Player1:
    IMG = 0
    U = 38
    V = 0
    WIDTH = -26
    HEIGHT = 32
    DX = 1.5
    DY = 0
    GRAVITY = 0.3
    JUMP_POWER = -4.5
    COLKEY = 0
    ATTACK_DURATION = 0.25  # Durée de l'attaque en secondes
    HP = 100

    def __init__(self):
        self.x = 200
        self.y = 40
        self.is_jumping = False
        self.flipped = False
        self.attacking = False
        self.attack_start_time = None  # Variable pour garder une trace du moment où l'attaque a commencé

    def move_left(self):
        if self.x - self.DX >= -1:
            self.x -= self.DX

    def move_right(self):
        if self.x + self.WIDTH + self.DX <= pyxel.width + 1:
            self.x += self.DX

    def jump(self):
        if not self.is_jumping:
            self.is_jumping = True
            self.DY = self.JUMP_POWER
            if self.y == 40:
                self.DY = self.JUMP_POWER

    def attack(self):
        if not self.attacking:
            self.attacking = True
            self.attack_start_time = time.time()
            # Modifier les coordonnées dans l'image pendant l'attaque
            self.U = 0  # Nouvelles coordonnées U pendant l'attaque
            self.V = 0  # Nouvelles coordonnées V pendant l'attaque
            self.WIDTH = -32  # Nouvelle largeur pendant l'attaque
            self.HEIGHT = 32  # Nouvelle hauteur pendant l'attaque

    def update(self):
        if self.is_jumping:
            self.y += self.DY
            self.DY += self.GRAVITY
            if self.y >= 40:
                self.y = 40
                self.is_jumping = False

        if self.attacking:
            if time.time() - self.attack_start_time >= self.ATTACK_DURATION:
                self.U = 44
                self.V = 0
                self.WIDTH = -22
                self.HEIGHT = 32
                self.attacking = False


    def draw(self):
        # Dessiner le joueur
        if self.flipped:
                pyxel.blt(self.x, self.y, self.IMG, self.U, self.V, -self.WIDTH, self.HEIGHT, self.COLKEY)
        else:
                pyxel.blt(self.x, self.y, self.IMG, self.U, self.V, self.WIDTH, self.HEIGHT, self.COLKEY)
    
        # Dessiner la barre de vie du joueur 2 en haut à droite
        bar_width = 30
        bar_height = 5
        remaining_width = int((self.HP / 100) * bar_width)
        bar_x = pyxel.width - bar_width - 10  # Coordonnée X pour la barre de vie du joueur 2
        pyxel.rect(bar_x-7, 11, 30, 5, pyxel.COLOR_WHITE)
        pyxel.rect(bar_x-7, 11, remaining_width, bar_height, pyxel.COLOR_RED)
        pyxel.rectb(bar_x-7, 11, bar_width, bar_height, pyxel.COLOR_BLACK)
            
        


class Player2:
    IMG = 0
    U = 38
    V = 33
    WIDTH = -26
    HEIGHT = 32
    DX = 1.5
    DY = 0
    GRAVITY = 0.3
    JUMP_POWER = -4.5
    COLKEY = 0
    ATTACK_DURATION = 0.25  # Durée de l'attaque en secondes
    HP = 100

    def __init__(self):
        self.x = 30
        self.y = 41
        self.is_jumping = False
        self.flipped = False
        self.attacking = False
        self.attack_start_time = None  # Variable pour garder une trace du moment où l'attaque a commencé

    def move_left(self):
        if self.x - self.DX >= 0:
            self.x -= self.DX

    def move_right(self):
        if self.x + self.DX <= pyxel.width + self.WIDTH:
            self.x += self.DX

    def jump(self):
        if not self.is_jumping:
            self.is_jumping = True
            self.DY = self.JUMP_POWER
            if self.y == 41:
                self.DY = self.JUMP_POWER

    def attack(self):
        if not self.attacking:
            self.attacking = True
            self.attack_start_time = time.time()
            # Modifier les coordonnées dans l'image pendant l'attaque
            self.U = 3  # Nouvelles coordonnées U pendant l'attaque
            self.V = 33  # Nouvelles coordonnées V pendant l'attaque
            self.WIDTH = -29  # Nouvelle largeur pendant l'attaque (négative car sprite inversé)
            self.HEIGHT = 31  # Nouvelle hauteur pendant l'attaque

    def update(self):
        if self.is_jumping:
            self.y += self.DY
            self.DY += self.GRAVITY
            if self.y >= 41:
                self.y = 41
                self.is_jumping = False

        if self.attacking:
            if time.time() - self.attack_start_time >= self.ATTACK_DURATION:
                self.U = 38
                self.V = 33
                self.WIDTH = -26
                self.HEIGHT = 32
                self.attacking = False

    
    def draw(self):
        # Dessiner le joueur
        if self.flipped:
            pyxel.blt(self.x, self.y, self.IMG, self.U, self.V, -self.WIDTH, self.HEIGHT, self.COLKEY)
        else:
            pyxel.blt(self.x, self.y, self.IMG, self.U, self.V, self.WIDTH, self.HEIGHT, self.COLKEY)

        # Dessiner la barre de vie du joueur 1 en haut à gauche
        bar_width = 30
        bar_height = 5
        remaining_width = int((self.HP / 100) * bar_width)
        pyxel.rect(17, 11, 30, 5, pyxel.COLOR_WHITE)
        pyxel.rect(17, 11, remaining_width, bar_height, pyxel.COLOR_RED)
        pyxel.rectb(17, 11, bar_width, bar_height, pyxel.COLOR_BLACK)
     

class BannierROSE:
    IMG5 = 0
    U5 = 50
    V5 = 0
    WIDTH5 = 13
    HEIGHT5 = 17
    COOLKEY5 = 0
    def __init__(self):
        self.x5 = 235
        self.y5 = 2
        
class BannierBLEU:
    IMG6 = 0
    U6 = 50
    V6 = 33 
    WIDTH6 = -13
    HEIGHT6 = 16
    COOLKEY6 = 0
    def __init__(self):
        self.x6 = 2
        self.y6 = 2
        
class Background:
    IMG1 = 1
    U1 = 0
    V1 = 0
    WIDTH1 = 255
    HEIGHT1 = 135

    def __init__(self):
        self.x1 = 0
        self.y1 = 0
        
App()