import pyxel

# Variables non-physiques
moto_x = 6
moto_y = 94
camera_x = 0
stop = False
boom = False

# Variables de vitesse
v_droite = 0
v_gauche = 0
v_max_droite = 3
v_max_gauche = -2
en_mouvement_x = False

# Paramètres physiques
GRAVITE = 0.8
FORCE_SAUT = -8.5
en_air = True
vitesse_y = 0 

# Liste des plateformes
plateformes = [
    [5*8, 13*8, 6*8, 8],
    [6*8, 12*8, 4*8, 8],
    [0, 14*8, 256*8, 16],
    [24*8, 13*8, 22*8, 8],
    [27*8, 12*8, 16*8, 8],
    [30*8, 11*8, 10*8, 8],
    [33*8, 10*8, 4*8, 8],
    [92*8, 13*8, 8*8, 8],
    [93*8, 12*8, 6*8, 8],
    [94*8, 11*8, 4*8, 8],
    [95*8, 10*8, 2*8, 8],
    [134*8, 13*8, 9*8, 8],
    [137*8, 12*8, 6*8, 8],
    [140*8, 11*8, 3*8, 8],
    [163*8, 13*8, 6*8, 8],
    [166*8, 12*8, 3*8, 8],
    [216*8, 13*8, 4*8, 8],
    [218*8, 12*8, 2*8, 8],
]
# Liste des obstacles (cactus)
cactus = [
    [13*8+3, 12*8, 2*8-9, 2*8],
    [47*8+3, 12*8, 2*8-9, 2*8],
    [64*8+3, 12*8, 2*8-9, 2*8],
    [87*8+3, 12*8, 2*8-9, 2*8],
    [107*8+3, 12*8, 2*8-9, 2*8],
    [127*8+3, 12*8, 2*8-9, 2*8],
    [144*8+3, 11*8, 2*8-9, 3*8],
    [170*8+3, 12*8, 2*8-9, 2*8],
    [183*8+3, 12*8, 2*8-9, 2*8],
    [195*8+3, 12*8, 2*8-9, 2*8],
    [221*8+3, 12*8, 2*8-9, 2*8],
    [233*8+3, 12*8, 2*8-9, 2*8],
]

def update():
    global moto_x, moto_y, v_droite, v_gauche, en_mouvement_x, camera_x, en_air, vitesse_y, stop, boom
    if boom:
        if pyxel.btnp(pyxel.KEY_R):
            moto_x = 6
            moto_y = 94
            boom = False
        return
    
    boom = False
    
    # Déplacement horizontal avec inertie
    deplacement_x = v_droite + v_gauche
    
    # Collisions horizontales avant déplacement
    nouvelle_x = moto_x + deplacement_x
    collision = False
    
    # Gestion des collisions 
    for plat in plateformes + cactus:
        px, py, pw, ph = plat
        if (nouvelle_x + 29 > px and
            nouvelle_x < px + pw and
            moto_y + 18 > py and
            moto_y < py + ph):
            collision = True
            if plat in cactus:
                boom = True
            break
    
    if not collision:
        moto_x = nouvelle_x
    else:
        v_droite = 0
        v_gauche = 0
    
    # controle gauche
    if pyxel.btn(pyxel.KEY_LEFT) and moto_x > 6 and not stop:  # Condition stop ajoutée
        v_gauche = max(v_gauche - 0.1, v_max_gauche)
    else:
        v_gauche = min(v_gauche + 0.1, 0)
        if moto_x + v_gauche < 6:
            moto_x = 6
            v_gauche = 0
    
    # Gestion du stop quand on est au bord droit
    if pyxel.btn(pyxel.KEY_RIGHT) and moto_x < 239*8-105:
        v_droite = min(v_droite + 0.1, v_max_droite)
        stop = False  # Reset du stop quand on se déplace à droite
    else:
        v_droite = max(v_droite - 0.05, 0)
        if moto_x >= 239*8-40 and pyxel.btn(pyxel.KEY_LEFT):
            stop = True
    
    #respawn
    if moto_x < 239*8-30:
        if pyxel.btnp(pyxel.KEY_R):
            moto_x = 6
            moto_y = 94
    
    #maximum camera
    camera_x = max(moto_x - 25, 0)
    en_mouvement_x = v_gauche < 0 or v_droite > 0

    # Saut
    if pyxel.btnp(pyxel.KEY_SPACE) and not en_air:
        vitesse_y = FORCE_SAUT
        en_air = True

    # Gravité
    vitesse_y += GRAVITE
    moto_y += vitesse_y
    
    # Collisions
    en_air = True
    for plat in plateformes + cactus:
        px, py, pw, ph = plat
        if (moto_x + 29 > px and
            moto_x < px + pw and
            moto_y + 18 > py and
            moto_y < py + ph):
                
            if plat in cactus:
                boom = True
            
            if vitesse_y > 0:  # Collision par le bas
                moto_y = py - 18
                vitesse_y = 0
                en_air = False
            elif vitesse_y < 0:  # Collision par le haut
                moto_y = py + ph
                vitesse_y = 0

def draw():
    #fond bleu + emplacement camera
    pyxel.cls(12)
    pyxel.camera(camera_x, 0)
    
    # Fond tilemap
    pyxel.bltm(-12, 7*8, 0, 0*8, 250*8, 255*8, 6*8, 8)
    pyxel.bltm(0, 9*8, 0, 0*8, 250*8, 255*8, 6*8, 8)
    pyxel.bltm(-12*3, 10.5*8, 0, 0*8, 250*8, 255*8, 6*8, 8)
    pyxel.bltm(0, 0, 0, 0*8, 0*8, 255*8, 16*8, 8)
    
    # Animation moto
    if not boom:
        if en_air == True:
            if pyxel.frame_count % 60 < 30:
                pyxel.blt(moto_x, moto_y, 0, 65, 67, 29, 26, 8)
            else:
                pyxel.blt(moto_x, moto_y, 0, 2, 75, 28, 26, 8)
        else:
            if en_mouvement_x:
                if pyxel.frame_count % 60 < 30:
                    pyxel.blt(moto_x, moto_y, 0, 48, 12, 29, 18, 8)
                else:
                    pyxel.blt(moto_x, moto_y, 0, 48, 36, 29, 18, 8)
            else:
                if pyxel.frame_count % 60 < 30:
                    pyxel.blt(moto_x, moto_y, 0, 48, 36, 29, 18, 8)
                else:
                    pyxel.blt(moto_x, moto_y, 0, 16, 36, 29, 18, 8)
    else:
        pyxel.blt(moto_x, moto_y - 6, 0, 0, 232, 32, 24, 8)


pyxel.init(140, 128, fps=60, title="Ride or Die")
pyxel.load("res.pyxres")
pyxel.run(update, draw)