import pyxel
import random

# Initialisation des paramètres du jeu
pyxel.init(160, 120, title="Bird Escape")
pyxel.load ("res.pyxres")
SCREEN_WIDTH = 160
SCREEN_HEIGHT = 120
GRAVITY = 0.5
JUMP_STRENGTH = -4
GAP_HEIGHT = 40
TRUNK_WIDTH = 25
WIDTH = 160
HEIGHT = 120  # Correction de l'orthographe ici
PIPE_SPEED = 1.5
bird_vy = 1  # Vitesse verticale de l'oiseau
bird_y = SCREEN_HEIGHT // 2  # Position de départ au milieu de l'écran
score = 0
game_over = False
frame_count = 0
troncs = []  # Liste des tuyaux
passed_pipes = []

def reset():
    global bird_y, bird_vy, troncs, frame_count, score, passed_pipes, game_over
    bird_y = SCREEN_HEIGHT // 2
    bird_vy = 0
    troncs = []
    frame_count = 0
    score = 0
    passed_pipes = []
    game_over = False


def check_collision_with_green(x, y):
# Vérifie la couleur sous l'oiseau à la position donnée
    color_at_pos = pyxel.pget(x, y)
    return color_at_pos == 3

        
def update():
    global bird_vy, bird_y, troncs, frame_count, score, game_over
    
    game_over = check_collision_with_green(TRUNK_WIDTH,bird_y)
    
    if game_over:
        if pyxel.btn(pyxel.KEY_O):  # Si l'utilisateur appuie sur 'O', réinitialiser le jeu
            reset()
        return

    # Saut
    if pyxel.btn(pyxel.KEY_SPACE):
        bird_vy = JUMP_STRENGTH
        
    # Gravité
    bird_vy += GRAVITY
    bird_y += bird_vy
        
    """ Vérification si l'oiseau touche le sol, le plafond ou le tronc""" 
    if bird_y > SCREEN_HEIGHT - 8: 
        bird_y = SCREEN_HEIGHT - 8 
        game_over = True 
    if bird_y < 0:
        bird_y = 0 
        game_over = True 
    
    # Création de nouveaux tuyaux à chaque frame_count
    if frame_count % 90 == 0:
        gap_y = random.randint(20, SCREEN_HEIGHT - GAP_HEIGHT - 20)
        troncs.append({"x": SCREEN_WIDTH, "gap_y": gap_y})
    
    # Mise à jour des tuyaux
    for tronc in troncs:
        tronc["x"] -= PIPE_SPEED
        
        if tronc["x"] + TRUNK_WIDTH < 20 and tronc not in passed_pipes :
         score += 1
    
    # Supprimer les tuyaux sortis de l'écran
    troncs[:] = [tronc for tronc in troncs if tronc["x"] + TRUNK_WIDTH > 0]
    bird_x = 30 # Position fixe en X de l’oiseau
    for tronc in troncs:
        if (bird_x + 8 > tronc["x"]) and (bird_x < tronc["x"] + TRUNK_WIDTH) :
            # Si l’oiseau n’est PAS dans le trou, il y a collision
            if not (tronc["gap_y"] < bird_y < tronc["gap_y"] + GAP_HEIGHT):
                game_over = True
    frame_count += 1

def draw():
    global game_over
    
    pyxel.cls(6)  # Efface l'écran avec la couleur de fond (6)

    # Dessin de l'oiseau
    pyxel.blt(TRUNK_WIDTH, bird_y, 0,0,0,16,16,0)

    # Dessin des tuyaux
    for tronc in troncs:
        x = tronc["x"]
        gap_y = tronc["gap_y"]
        pyxel.rect(x, 0, TRUNK_WIDTH, gap_y, 3)  # Tuyau du haut
        pyxel.rect(x, gap_y + GAP_HEIGHT, TRUNK_WIDTH, SCREEN_HEIGHT, 3)  # Tuyau du bas
        
    # Affichage du score
    pyxel.text(5, 5, f"Score: {score}", 7)

    # Affichage du message "Game Over"
    if game_over:
        pyxel.text(WIDTH // 2 - 20, HEIGHT // 2, "GAME OVER", 8)
        pyxel.text(WIDTH // 2 - 70, HEIGHT // 2 + 10, "Appuie sur 'KEY_O' pour recommencer", 7)

pyxel.run(update, draw)