import pyxel
import random

pyxel.init(256, 256, title="RPG TROLL")
pyxel.mouse(True)
pyxel.load("res.pyxres")  # Charge les ressources, incluant l'image de quart de cœur

boss_hp = 4
player_hp = 4
in_spe = False

def update():
    global boss_hp, player_hp, in_spe
    if boss_hp <= 0 or player_hp <= 0:
        return
    if not pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
        return
    x, y = pyxel.mouse_x, pyxel.mouse_y
    if not in_spe:
        if 60 <= x <= 130 and 200 <= y <= 230:
            if random.random() < 0.2:
                boss_hp -= 1
                pyxel.play(0, [60, 0, 60, 0])  # Hit
            else:
                pyxel.play(0, [40, 0])  # Miss
        elif 140 <= x <= 210 and 200 <= y <= 230:
            in_spe = True
        else:
            for i in range(5):
                bx = 50 + (i % 2) * 90
                by = 80 + (i // 2) * 40
                if bx <= x <= bx + 80 and by <= y <= by + 30:
                    pyxel.play(0, [20, 25, 30, 0])  # Bruit
                    in_spe = False
            return
    if 220 <= x <= 240 and 180 <= y <= 195:
        boss_hp = 0
        pyxel.play(0, [80, 78, 76, 74, 72, 70])  # ONESHOT
        in_spe = False
    if pyxel.frame_count % 60 == 0 and boss_hp > 0 and not in_spe:
        player_hp -= 1

def draw():
    pyxel.cls(0)
    pyxel.circ(128, 90, 45, 8)
    pyxel.circ(110, 80, 8, 0)
    pyxel.circ(146, 80, 8, 0)
    pyxel.rect(113, 105, 30, 5, 0)
    for i in range(4):
        pyxel.rect(78 + i*25, 45, 20, 8, 8 if i < boss_hp else 1)
    
    # Utilise l'image de quart de cœur pour les HP du joueur (assumant sprite 8x8 à la position 0,0 dans bank 0)
    for i in range(4):
        qx = 10 + (i % 2)*9
        qy = 10 + (i // 2)*9
        if i < player_hp:
            pyxel.blt(qx, qy, 0, 0, 0, 8, 8, 0)  # Blit du sprite (ajuste u,v si position différente dans res.pyxres)
        else:
            pyxel.rect(qx, qy, 8, 8, 0)  # Fond noir pour les HP vides
    
    if not in_spe:
        pyxel.rect(60, 200, 70, 30, 7)
        pyxel.text(78, 210, "ATK", 0)
        pyxel.rect(140, 200, 70, 30, 7)
        pyxel.text(158, 210, "SPE", 0)
    else:
        pyxel.rect(35, 65, 190, 140, 1)
        pyxel.text(100, 70, "SPE", 7)
        for i in range(5):
            bx = 50 + (i % 2) * 90
            by = 80 + (i // 2) * 40
            pyxel.rect(bx, by, 80, 30, 7)
            pyxel.text(bx + 5, by + 10, ["Zblorp","Pfiou","Krrrch","Wub","Glitch"][i], 0)
    pyxel.rect(220, 180, 20, 15, 5)
    if boss_hp == 0:
        pyxel.text(80, 120, "VICTOIRE !", 10)
    elif player_hp == 0:
        pyxel.text(80, 120, "DEFAITE", 8)

pyxel.run(update, draw)