import pyxel

x = 50
y = 50
vitesse = 1

pyxel.init(160, 120)
pyxel.load("2.pyxres")

class Collisionneur:
    def __init__(self, x, y, largeur, hauteur):
        self.x = x
        self.y = y
        self.largeur = largeur
        self.hauteur = hauteur

class Application:
    def est_en_collision(self, a, b):
        return (
            a.x < b.x + b.largeur and
            a.x + a.largeur > b.x and
            a.y < b.y + b.hauteur and
            a.y + a.hauteur > b.y
        )

jeu = Application()

# 💡 Création du PNJ
class PNJ:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.largeur = 16
        self.hauteur = 18
        self.chemin = ["droite", "bas", "gauche", "haut"]
        self.direction_index = 0
        self.compteur = 0

    def bouger(self):
        direction = self.chemin[self.direction_index]
        if direction == "droite":
            self.x += 1
        elif direction == "gauche":
            self.x -= 1
        elif direction == "haut":
            self.y -= 1
        elif direction == "bas":
            self.y += 1

        self.compteur += 1
        if self.compteur > 30:
            self.compteur = 0
            self.direction_index = (self.direction_index + 1) % len(self.chemin)

# Instanciation du PNJ
pnj = PNJ(100, 50)

def update():
    global x, y

    mur = Collisionneur(10, 10, 20, 20)

    if pyxel.btn(pyxel.KEY_RIGHT):
        prochain = Collisionneur(x + vitesse, y, 16, 18)
        if not jeu.est_en_collision(prochain, mur):
            x += vitesse

    if pyxel.btn(pyxel.KEY_LEFT):
        prochain = Collisionneur(x - vitesse, y, 16, 18)
        if not jeu.est_en_collision(prochain, mur):
            x -= vitesse

    if pyxel.btn(pyxel.KEY_UP):
        prochain = Collisionneur(x, y - vitesse, 16, 18)
        if not jeu.est_en_collision(prochain, mur):
            y -= vitesse

    if pyxel.btn(pyxel.KEY_DOWN):
        prochain = Collisionneur(x, y + vitesse, 16, 18)
        if not jeu.est_en_collision(prochain, mur):
            y += vitesse

    # 💡 Déplacement du PNJ
    pnj.bouger()

def draw():
    pyxel.cls(0)
    pyxel.rect(10, 10, 20, 20, 11)  # mur
    pyxel.blt(x, y, 0, 0, 15, 16, 18)  # joueur
    pyxel.blt(pnj.x, pnj.y, 0, 0, 0, 16, 18)  # 💡 affichage du PNJ

pyxel.run(update, draw)