# Type your text here
importtime# Paramètres du jeu
LARGEUR_ECRAN=20# Largeur de l'écran
NIVEAUX_MAX=5# Nombre de niveaux
# Classe Mario pour gérer son état
classMario:def__init__(self):self.position=1# Position initiale de Mario
self.vivant=Truedefdeplacer(self,deplacement):"""Déplace Mario de 1 ou 2 cases"""self.position+=deplacementdefest_sur_obstacle(self,obstacle):"""Vérifie si Mario a touché un obstacle"""returnself.position==obstacledefreset_position(self):"""Réinitialise la position de Mario pour le niveau suivant"""self.position=1# Fonction pour afficher l'écran du jeu
defafficher_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
defjouer():"""Fonction qui gère l'ensemble du jeu"""mario=Mario()niveau=1print("Bienvenue dans le jeu Mario !")# Boucle principale du jeu
whilemario.vivantandniveau<=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 : "))ifdeplacementnotin[1,2]:print("Choix invalide. Mario ne bouge pas.")continueexcept:print("Choix invalide. Mario ne bouge pas.")continue# Déplacer Mario
mario.deplacer(deplacement)# Vérifier si Mario a touché l'obstacle
ifmario.est_sur_obstacle(obstacle):print("Oh non ! Mario a touché un obstacle !")mario.vivant=Falsebreakelse:print(f"Bien joué ! Mario avance à la position {mario.position}.")# Si Mario atteint la fin de l'écran, il passe au niveau suivant
ifmario.position>=LARGEUR_ECRAN:niveau+=1mario.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())
foriinrange(1000):# Remplaçant une pause rapide
passifmario.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:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
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.