Python jeu labyrinthe (sans pygame/tkinter)

Résolu/Fermé
Fritz - Modifié le 23 avril 2022 à 00:14
 Fritz - 24 avril 2022 à 03:50
Bonsoir,

Je dois finir un code pour un devoir de NSI je suis bloqué.

Voici le code
from random import*

#fonction qui gère les déplacements sur une même ligne
#entrée : liste, nombre entier, nombre entier
#sortie : liste, nombre entier, nombre entier
def deplacement_x(tab,i,j):
    if direction == "D":
        if j+1 != "#" and "|":
            tab[i][j] = "0"
            tab[i][j+1] = "A"
            j=j+1
            return(tab,i,j)

    elif direction == "Q" :
        if j-1 != "#"and "|":
            tab[i][j] = "0"
            tab[i][j-1] = "A"
            j=j-1
            return(tab,i,j)
        
#fonction qui gèree les déplacements sur une même ligne
#entrée : liste, nombre entier, nombre entier
#sortie : liste, nombre entier, nombre entier
def deplacement_y(tab,i,j):
    if direction == "Z":
        if i-1 != "#" and "|":
            tab[i][j] = "0"
            tab[i-1][j] = "A"
            i=i-1
            return(tab,i,j)

    elif direction == "S" :
        if i+1 != "#" and "|":
            tab[i][j] = "0"
            tab[i+1][j] = "A"
            i=i+1
            return(tab,i,j)

print("Bienvenue dans 'Le Tombeau''")
print("Vous êtes un archéologue coincé dans un tombeau,")
print("trouvez le trésor pour enfin sortir du tombeau !!!")
print("Mais attention au piège mortel dispersé dans le tombeau.")
print("Egalemeent plusieurs trésors bonus sont eparpillés donnant un grand avantage")
print("Z = haut, Q = gauche, D = droite, S = bas")
#########################################################################################
# Définition de la taille du tombeau facile

ligne = 12
colonne =12
tableau1 = [["0"]*colonne for i in range (ligne)]

# Définition de i et j
i = ligne
j = colonne

# Codage du trésor du tombeau
if tableau1 == [i+1] or [j+1] == "T":
    game = True
    print("Bravo vous avez trouvé le trésor !!!")

# Codage du piège du tombeau
if tableau1 == [i+1] or [j+1] == "P":
    game = False
    print("Vous avez péri")

tableau1 [3][1]="A"
tableau1 [2][2]="|"
tableau1 [3][2]="|"
tableau1 [4][2]="|"
tableau1 [1][2]="B"
tableau1 [5][2]="|"
tableau1 [6][2]="|"
tableau1 [7][2]="|"
tableau1 [8][2]="|"
tableau1 [10][1]="B"
tableau1 [10][2]="|"
tableau1 [10][3]="P"
tableau1 [3][4]="|"
tableau1 [4][4]="|"
tableau1 [5][4]="|"
tableau1 [6][4]="|"
tableau1 [7][4]="|"
tableau1 [8][4]="|"
tableau1 [9][4]="|"
tableau1 [10][4]="|"
tableau1 [1][5]="P"
tableau1 [1][4]="|"
tableau1 [1][3]="|"
tableau1 [1][6]="|"
tableau1 [2][6]="|"
tableau1 [3][6]="|"
tableau1 [4][6]="|"
tableau1 [5][6]="|"
tableau1 [6][6]="|"
tableau1 [7][6]="|"
tableau1 [8][6]="|"
tableau1 [9][6]="|"
tableau1 [2][8]="|"
tableau1 [3][8]="|"
tableau1 [4][8]="|"
tableau1 [5][8]="|"
tableau1 [6][8]="|"
tableau1 [7][8]="|"
tableau1 [8][8]="|"
tableau1 [9][8]="|"
tableau1 [10][8]="|"
tableau1 [2][10]="|"
tableau1 [3][10]="|"
tableau1 [4][10]="|"
tableau1 [5][10]="|"
tableau1 [6][10]="|"
tableau1 [7][10]="|"
tableau1 [8][10]="|"
tableau1 [9][10]="|"
tableau1 [1][10]="|"
tableau1 [10][10]="T"

for i in range (0,11):
    tableau1 [i][0]="#"
for i in range (0,12):
    tableau1 [0][i]="#"
for i in range (0,11):
    tableau1 [11][i]="#"
for i in range (0,12):
    tableau1 [i][11]="#"

#########################################################################################
# Définition de la taille tu tombeau moyen


ligne = 12
colonne = 12
tableau2 = [["0"]*colonne for i in range (ligne)]

# Définition de i et j
i = ligne
j = colonne

# Codage du trésor du tombeau
if tableau2 == [i+1] or [j+1] == "T":
    game = True
    print("Bravo vous avez trouvé le trésor !!!")

# Codaage du piège du tombeau
if tableau2 == [i+1] or [j+1] == "P" or "|" or "B":
    game = False
    print("Vous avez péri")

tableau2 [5][1]="A"

tableau2 [2][10]="T"

tableau2 [1][2]="|"
tableau2 [2][2]="|"
tableau2 [3][2]="|"
tableau2 [4][2]="|"
tableau2 [5][2]="|"
tableau2 [6][2]="|"
tableau2 [8][2]="|"
tableau2 [8][1]="|"

tableau2 [9][1]="!"

tableau2 [10][1]="|"
tableau2 [6][3]="|"
tableau2 [2][4]="|"
tableau2 [4][4]="|"
tableau2 [6][4]="|"
tableau2 [7][4]="|"

tableau2 [8][4]="P"

tableau2 [2][5]="|"
tableau2 [4][5]="|"
tableau2 [6][5]="|"
tableau2 [10][5]="|"
tableau2 [2][6]="|"
tableau2 [4][6]="|"
tableau2 [6][6]="|"

tableau2 [8][6]="P"

tableau2 [9][6]="|"
tableau2 [10][6]="|"
tableau2 [2][7]="|"
tableau2 [4][7]="|"
tableau2 [6][7]="|"
tableau2 [10][7]="|"
tableau2 [2][8]="|"
tableau2 [4][8]="|"
tableau2 [6][8]="|"
tableau2 [7][8]="|"
tableau2 [8][8]="|"
tableau2 [10][8]="|"
tableau2 [3][9]="|"
tableau2 [4][9]="|"
tableau2 [6][9]="|"

tableau2 [7][9]="!"

tableau2 [8][9]="|"
tableau2 [10][9]="|"
tableau2 [1][10]="|"
tableau2 [3][10]="|"
tableau2 [4][10]="|"
tableau2 [10][10]="|"

            #

for i in range (0,11):
    tableau2 [i][0]="#"
for i in range (0,12):
    tableau2 [0][i]="#"
for i in range (0,11):
    tableau2 [11][i]="#"
for i in range (0,12):
    tableau2 [i][11]="#"
    
###################################################################################
# Définition du tombeau difficile


ligne = 15
colonne =15
tableau3 = [["0"]*colonne for i in range (ligne)]

# Définition de i et j
i = ligne
j = colonne

# Codage du trésor du tombeau
if tableau3 == [i+1] or [j+1] == "P":
    game = True
    print("Bravo vous avez trouvé le trésor !!!")

# Codage du piège du tombeau
if tableau3 == [i+1] or [j+1] == "O" or "|" or "B":
    game = False
    print("Vous avez péri  ")
    
# Création du tombeau
tableau3 [13][13]="A"

tableau3 [2][2]="T"

tableau3 [1][5]="|"
tableau3 [1][6]="|"

tableau3 [1][7]="!"

tableau3 [1][9]="|"
tableau3 [1][10]="O"
tableau3 [1][13]="|"

tableau3 [2][3]="|"
tableau3 [2][7]="|"
tableau3 [2][8]="|"
tableau3 [2][13]="|"

tableau3 [3][2]="|"
tableau3 [3][3]="|"
tableau3 [3][4]="|"
tableau3 [3][5]="|"
tableau3 [3][7]="|"
tableau3 [3][8]="|"
tableau3 [3][13]="|"

tableau3 [4][3]="|"
tableau3 [4][4]="O"
tableau3 [4][10]="|"
tableau3 [4][11]="|"

tableau3 [5][1]="|"
tableau3 [5][7]="|"
tableau3 [5][8]="|"

tableau3 [6][1]="|"
tableau3 [6][2]="|"
tableau3 [6][3]="|"
tableau3 [6][4]="|"
tableau3 [6][7]="O"
tableau3 [6][8]="|"
tableau3 [6][9]="|"
tableau3 [6][10]="|"
tableau3 [6][11]="O"
tableau3 [6][12]="!"
tableau3 [6][13]="|"

tableau3 [7][6]="|"
tableau3 [7][7]="|"
tableau3 [7][8]="|"
tableau3 [7][9]="!"

tableau3 [8][2]="|"
tableau3 [8][4]="|"
tableau3 [8][8]="|"
tableau3 [8][9]="|"
tableau3 [8][10]="|"
tableau3 [8][13]="|"

tableau3 [9][2]="|"
tableau3 [9][4]="|"
tableau3 [9][5]="|"
tableau3 [9][7]="|"
tableau3 [9][10]="|"
tableau3 [9][12]="|"

tableau3 [10][1]="O"
tableau3 [10][2]="|"
tableau3 [10][5]="|"
tableau3 [10][7]="|"
tableau3 [10][9]="|"
tableau3 [10][10]="|"

tableau3 [11][1]="|"
tableau3 [11][9]="|"
tableau3 [11][10]="!"
tableau3 [11][11]="|"
tableau3 [11][12]="|"

tableau3 [12][1]="|"
tableau3 [12][2]="O"
tableau3 [12][3]="|"
tableau3 [12][4]="|"
tableau3 [12][5]="|"
tableau3 [12][7]="|"
tableau3 [12][8]="|"
tableau3 [12][9]="|"
tableau3 [12][11]="|"
tableau3 [12][12]="|"

tableau3 [13][1]="|"
tableau3 [13][2]="|"
tableau3 [13][3]="!"
tableau3 [13][7]="|"
tableau3 [13][8]="O"



for i in range (0,14):
    tableau3 [i][0]="#"
for i in range (0,15):
    tableau3 [0][i]="#"
for i in range (0,14):
    tableau3 [14][i]="#"
for i in range (0,15):
    tableau3 [i][14]="#"

# Affichage du tombeau
liste = [tableau1, tableau2, tableau3]
résultat = choice(liste)
for i in range (len(résultat)) : 
    print (résultat[i])
A voir également:

4 réponses

jee pee Messages postés 39610 Date d'inscription mercredi 2 mai 2007 Statut Modérateur Dernière intervention 23 avril 2024 9 230
21 avril 2022 à 18:31
Bonjour,

Tu n'as pas expliqué quel est le soucis.

Enfin moi j'en vois un énorme, il n'y a aucun endroit où tu demandes à l'utilisateur ce qu'il veut faire.
0
J'ai trop de soucis, je suis qu'un débutant il y a des
print
s qui ne devraient pas s'exécuter, les déplacements ne fonctionnent pas???

Je vous mets le cahier des charges si vous le souhaitez :
 Le plateau de jeu (ou grille) sera de taille 10×10. minimum
  • Le joueur jouera seul (le but étant se déplacer sur le plateau de jeu).
  • Les déplacements se feront grâce aux touches Z, Q, S et D du clavier.
  • ATTENTION : Le joueur ne peut pas sortir du plateau de jeu. Le joueur doit être bloqué en bordure du plateau lorsqu’un déplacement l’amène à en sortir.
  • La grille de jeux doit comporter des pièges, des murs, des bonus, etc…..
  • Votre programme doit contenir plusieurs fonctions :
    • Des Fonctions déplacements (une pour chaque déplacement : haut, bas, gauche droite)
    • Une fonction qui prend en charge l’évolution du plateau de jeu après chaque tour (déplacement).
    • Une ou plusieurs fonction(s) relatives aux pièges, bonus, etc…….
0
jee pee Messages postés 39610 Date d'inscription mercredi 2 mai 2007 Statut Modérateur Dernière intervention 23 avril 2024 9 230
Modifié le 22 avril 2022 à 00:25
Je pense que tu es vraiment très loin d'une solution. Comme je te l'ai indiqué tu n'as même pas saisi le choix du joueur. Tu t'es concentré sur le dessin des plateaux, mais sans penser à algorithme, à la logique du programme.

Je ne suis pas super inspiré sur ce coup là. Mais je te propose une base, à compléter, à développer, qui te permet de boucler sur la saisie et d'afficher un plateau lisible.

from random import*


#Affichage du tombeau
def aff_tabl():
    print("")
    for i in range (len(tableau)) : 
        for j in range (len(tableau[i])) :
            c = tableau[i][j]
# pour les tests on laisse les objets cachés normalement
#            if c in ("0","T","P","B"): c = " "
            if c in ("0"): c = " "
            print(c," ",end="")
        print("")
    print("")

#recherche position du joueur
def pos_joueur():
    for i in range (len(tableau)):
        for j in range (len(tableau[i])):
            if tableau[i][j] == "A":
                return [i,j]
    return [-1,-1]

#fonctions qui gèrent les déplacements 
def depl_haut():
    if tableau[position[0]-1][position[1]] in("#",'|'):
        print("Déplacement impossible")
        return
    if tableau[position[0]-1][position[1]] == "0":
        tableau[position[0]][position[1]] = "0"
        tableau[position[0]-1][position[1]] = "A"
        position[0] = position[0]-1
        position[1] = position[1]
        return
    if tableau[position[0]-1][position[1]] == "B":
        bonus()
        return
    if tableau[position[0]-1][position[1]] == "T":
        print("Vous avez trouvé le trésor")
        tableau[position[0]][position[1]] = "0"
        tableau[position[0]-1][position[1]] = "X"
        position[0] = position[0]-1
        position[1] = position[1]
        game = False
        return
    if tableau[position[0]-1][position[1]] == "P":
        print("Perdu, vous êtes tombé dans un piège")
        tableau[position[0]][position[1]] = "0"
        tableau[position[0]-1][position[1]] = "-"
        position[0] = position[0]-1
        position[1] = position[1]
        game = False
        return
    

    return
def depl_bas():
    return
def depl_droite():
    return
def depl_gauche():
    return

def bonus():
    return
        

#########################################################################################
# Définition de la taille du tombeau facile

ligne = 12
colonne =12
tableau1 = [["0"]*colonne for i in range (ligne)]

tableau1 [3][1]="A"
tableau1 [2][2]="|"
tableau1 [3][2]="|"
tableau1 [4][2]="|"
tableau1 [1][2]="B"
tableau1 [5][2]="|"
tableau1 [6][2]="|"
tableau1 [7][2]="|"
tableau1 [8][2]="|"
tableau1 [10][1]="B"
tableau1 [10][2]="|"
tableau1 [10][3]="P"
tableau1 [3][4]="|"
tableau1 [4][4]="|"
tableau1 [5][4]="|"
tableau1 [6][4]="|"
tableau1 [7][4]="|"
tableau1 [8][4]="|"
tableau1 [9][4]="|"
tableau1 [10][4]="|"
tableau1 [1][5]="P"
tableau1 [1][4]="|"
tableau1 [1][3]="|"
tableau1 [1][6]="|"
tableau1 [2][6]="|"
tableau1 [3][6]="|"
tableau1 [4][6]="|"
tableau1 [5][6]="|"
tableau1 [6][6]="|"
tableau1 [7][6]="|"
tableau1 [8][6]="|"
tableau1 [9][6]="|"
tableau1 [2][8]="|"
tableau1 [3][8]="|"
tableau1 [4][8]="|"
tableau1 [5][8]="|"
tableau1 [6][8]="|"
tableau1 [7][8]="|"
tableau1 [8][8]="|"
tableau1 [9][8]="|"
tableau1 [10][8]="|"
tableau1 [2][10]="|"
tableau1 [3][10]="|"
tableau1 [4][10]="|"
tableau1 [5][10]="|"
tableau1 [6][10]="|"
tableau1 [7][10]="|"
tableau1 [8][10]="|"
tableau1 [9][10]="|"
tableau1 [1][10]="|"
tableau1 [10][10]="T"

for i in range (0,11):
    tableau1 [i][0]="#"
for i in range (0,12):
    tableau1 [0][i]="#"
for i in range (0,11):
    tableau1 [11][i]="#"
for i in range (0,12):
    tableau1 [i][11]="#"

#########################################################################################
# Définition de la taille tu tombeau moyen

ligne = 12
colonne = 12
tableau2 = [["0"]*colonne for i in range (ligne)]

tableau2 [5][1]="A"

tableau2 [2][10]="T"

tableau2 [1][2]="|"
tableau2 [2][2]="|"
tableau2 [3][2]="|"
tableau2 [4][2]="|"
tableau2 [5][2]="|"
tableau2 [6][2]="|"
tableau2 [8][2]="|"
tableau2 [8][1]="|"

tableau2 [9][1]="|"

tableau2 [10][1]="|"
tableau2 [6][3]="|"
tableau2 [2][4]="|"
tableau2 [4][4]="|"
tableau2 [6][4]="|"
tableau2 [7][4]="|"

tableau2 [8][4]="P"

tableau2 [2][5]="|"
tableau2 [4][5]="|"
tableau2 [6][5]="|"
tableau2 [10][5]="|"
tableau2 [2][6]="|"
tableau2 [4][6]="|"
tableau2 [6][6]="|"

tableau2 [8][6]="P"

tableau2 [9][6]="|"
tableau2 [10][6]="|"
tableau2 [2][7]="|"
tableau2 [4][7]="|"
tableau2 [6][7]="|"
tableau2 [10][7]="|"
tableau2 [2][8]="|"
tableau2 [4][8]="|"
tableau2 [6][8]="|"
tableau2 [7][8]="|"
tableau2 [8][8]="|"
tableau2 [10][8]="|"
tableau2 [3][9]="|"
tableau2 [4][9]="|"
tableau2 [6][9]="|"

tableau2 [7][9]="|"

tableau2 [8][9]="|"
tableau2 [10][9]="|"
tableau2 [1][10]="|"
tableau2 [3][10]="|"
tableau2 [4][10]="|"
tableau2 [10][10]="|"

            #

for i in range (0,11):
    tableau2 [i][0]="#"
for i in range (0,12):
    tableau2 [0][i]="#"
for i in range (0,11):
    tableau2 [11][i]="#"
for i in range (0,12):
    tableau2 [i][11]="#"
    
###################################################################################
# Définition du tombeau difficile

ligne = 15
colonne =15
tableau3 = [["0"]*colonne for i in range (ligne)]

# Création du tombeau
tableau3 [13][13]="A"

tableau3 [2][2]="T"

tableau3 [1][5]="|"
tableau3 [1][6]="|"

tableau3 [1][7]="|"

tableau3 [1][9]="|"
tableau3 [1][10]="O"
tableau3 [1][13]="|"

tableau3 [2][3]="|"
tableau3 [2][7]="|"
tableau3 [2][8]="|"
tableau3 [2][13]="|"

tableau3 [3][2]="|"
tableau3 [3][3]="|"
tableau3 [3][4]="|"
tableau3 [3][5]="|"
tableau3 [3][7]="|"
tableau3 [3][8]="|"
tableau3 [3][13]="|"

tableau3 [4][3]="|"
tableau3 [4][4]="O"
tableau3 [4][10]="|"
tableau3 [4][11]="|"

tableau3 [5][1]="|"
tableau3 [5][7]="|"
tableau3 [5][8]="|"

tableau3 [6][1]="|"
tableau3 [6][2]="|"
tableau3 [6][3]="|"
tableau3 [6][4]="|"
tableau3 [6][7]="O"
tableau3 [6][8]="|"
tableau3 [6][9]="|"
tableau3 [6][10]="|"
tableau3 [6][11]="O"
tableau3 [6][12]="|"
tableau3 [6][13]="|"

tableau3 [7][6]="|"
tableau3 [7][7]="|"
tableau3 [7][8]="|"
tableau3 [7][9]="|"

tableau3 [8][2]="|"
tableau3 [8][4]="|"
tableau3 [8][8]="|"
tableau3 [8][9]="|"
tableau3 [8][10]="|"
tableau3 [8][13]="|"

tableau3 [9][2]="|"
tableau3 [9][4]="|"
tableau3 [9][5]="|"
tableau3 [9][7]="|"
tableau3 [9][10]="|"
tableau3 [9][12]="|"

tableau3 [10][1]="O"
tableau3 [10][2]="|"
tableau3 [10][5]="|"
tableau3 [10][7]="|"
tableau3 [10][9]="|"
tableau3 [10][10]="|"

tableau3 [11][1]="|"
tableau3 [11][9]="|"
tableau3 [11][10]="|"
tableau3 [11][11]="|"
tableau3 [11][12]="|"

tableau3 [12][1]="|"
tableau3 [12][2]="O"
tableau3 [12][3]="|"
tableau3 [12][4]="|"
tableau3 [12][5]="|"
tableau3 [12][7]="|"
tableau3 [12][8]="|"
tableau3 [12][9]="|"
tableau3 [12][11]="|"
tableau3 [12][12]="|"

tableau3 [13][1]="|"
tableau3 [13][2]="|"
tableau3 [13][3]="|"
tableau3 [13][7]="|"
tableau3 [13][8]="O"

for i in range (0,14):
    tableau3 [i][0]="#"
for i in range (0,15):
    tableau3 [0][i]="#"
for i in range (0,14):
    tableau3 [14][i]="#"
for i in range (0,15):
    tableau3 [i][14]="#"

# 

tableau = choice([tableau1, tableau2, tableau3])
position=pos_joueur()
print("position du joueur",position)

print("Bienvenue dans 'Le Tombeau'")
print("Vous êtes un archéologue coincé dans un tombeau,")
print("trouvez le trésor pour enfin sortir du tombeau !!!")
print("Mais attention aux pièges mortels dispersés dans le tombeau.")
print("Egalemeent plusieurs bonus sont eparpillés donnant un grand avantage")
print("Z = haut, Q = gauche, D = droite, S = bas")
aff_tabl()

game=True

while game:
    direction=""
    while direction not in ("Z","Q","D","S"):
        direction=input("Votre déplacement Z/Q/D/S : ")
    if direction == "Z": depl_haut()
    if direction == "Q": depl_gauche()
    if direction == "D": depl_droite()
    if direction == "S": depl_bas()

    print("\nposition du joueur",position)
    aff_tabl()
   


A quelle action va correspondre cette valeur ? tableau3 [1][10]="O" , et que fait le bonus et le piège ?

0
mamiemando Messages postés 33079 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 avril 2024 7 749
23 avril 2022 à 00:27
Bonjour,

Je pense que ton message est mal formulé pour recevoir une aide efficace. On ne va évidemment pas faire ton programme à ta place (l'exercice perdrait tout son intérêt) et tu dis être confronté à plusieurs problèmes.

Or généralement sur un forum, on pose un (petit) problème par fil de discussion, idéalement avec un bout de code minimal qui permet de reproduire le problème. Ensuite il faut essayer d'être précis. Tu dis par exemple que des
print
s s'exécutent. Lesquels ? Sous quelles conditions (que dois faire l'utilisateur pour provoquer le bug)...

Ensuite quelques recommandations :
  • tes fonctions devraient prendre les variables significatives en paramètre. Par exemple
    tableau
    devrait être un paramètre de la fonction
    aff_tabl
    (et selon moi la position du joueur)
  • essaye de faire une fonction par tâche (par exemple, l'initilisation du tableau devrait être fait dans une fonction)


Bonne chance
0
J'ai réglé mon problème merci ^^
0