mario.py

Created by guillaumecreusot71

Created on December 01, 2024

2.84 KB


# Type your text hereimport random
import time

# Paramètres du jeu
LARGEUR_ECRAN = 20  # Largeur de l'écran
NIVEAUX_MAX = 5     # Nombre de niveaux

# Classe Mario pour gérer son état
class Mario:
    def __init__(self):
        self.position = 1  # Position initiale de Mario
        self.vivant = True

    def deplacer(self, deplacement):
        """Déplace Mario de 1 ou 2 cases"""
        self.position += deplacement

    def est_sur_obstacle(self, obstacle):
        """Vérifie si Mario a touché un obstacle"""
        return self.position == obstacle

    def reset_position(self):
        """Réinitialise la position de Mario pour le niveau suivant"""
        self.position = 1

# Fonction pour afficher l'écran du jeu
def afficher_ecran(mario, obstacle, niveau):
    """Affiche l'écran du jeu avec la position de Mario et de l'obstacle"""
    print("\n" + "-" * LARGEUR_ECRAN)
    print(f"Niveau {niveau}")
    print("Position de Mario: " + "-" * (mario.position - 1) + "M" + "-" * (LARGEUR_ECRAN - mario.position))
    print(f"Obstacle à la position {obstacle}")
    print("-" * LARGEUR_ECRAN)

# Fonction principale du jeu
def jouer():
    """Fonction qui gère l'ensemble du jeu"""
    mario = Mario()
    niveau = 1

    print("Bienvenue dans le jeu Mario !")

    # Boucle principale du jeu
    while mario.vivant and niveau <= NIVEAUX_MAX:
        obstacle = random.randint(1, LARGEUR_ECRAN - 1)  # Position aléatoire pour l'obstacle
        afficher_ecran(mario, obstacle, niveau)

        # Demander à l'utilisateur de se déplacer
        try:
            deplacement = int(input("Déplace Mario de 1 ou 2 : "))
            if deplacement not in [1, 2]:
                print("Choix invalide. Mario ne bouge pas.")
                continue
        except:
            print("Choix invalide. Mario ne bouge pas.")
            continue

        # Déplacer Mario
        mario.deplacer(deplacement)

        # Vérifier si Mario a touché l'obstacle
        if mario.est_sur_obstacle(obstacle):
            print("Oh non ! Mario a touché un obstacle !")
            mario.vivant = False
            break
        else:
            print(f"Bien joué ! Mario avance à la position {mario.position}.")
        
        # Si Mario atteint la fin de l'écran, il passe au niveau suivant
        if mario.position >= LARGEUR_ECRAN:
            niveau += 1
            mario.reset_position()  # Réinitialise la position pour le niveau suivant
            print(f"Bravo ! Mario passe au niveau {niveau}.")
        
        # Petite pause pour rendre le jeu un peu plus fluide (remplaçant du time.sleep())
        for i in range(1000):  # Remplaçant une pause rapide
            pass

    if mario.vivant:
        print("Félicitations ! Mario a gagné le jeu.")
    else:
        print("Dommage ! Mario a perdu.")
        print("Game Over.")

# Lancer le jeu
jouer()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.