Kyoko_2005
Messages postés1Date d'inscriptionsamedi 19 mars 2022StatutMembreDernière intervention19 mars 2022
-
Modifié le 19 mars 2022 à 17:56
Phil_1857
Messages postés1883Date d'inscriptionlundi 23 mars 2020StatutMembreDernière intervention28 février 2024
-
19 mars 2022 à 18:19
Bonjour,
Mon code ne marche pas et je ne comprends pas pourquoi, quelqu'un pourrait m'aider ? ;)
En vous remerciant par avance,
Kyoko ^^
Voici le code :
# Créé par Emmad'moiselle, le 01/02/2022 en Python 3.7
from tkinter import * #ici on a importé les modules dont on a besoin
from random import * #idem
from tkinter import messagebox
#ici code pour le premier mini-jeu
def jeu_labyrinte():
LARGEUR_BLOC = 50
NB_BLOCS = 12
CENTRE = LARGEUR_BLOC / 2
HAUTEUR_BORDURE = 20
fichniveau = "niveaux.lvl"
tab_plateau = []
for i in range (NB_BLOCS):
tab_plateau.append ([' '] * NB_BLOCS)
nouveau = Toplevel(page)
nouveau.config(bg="#FFFFFF")
nouveau.bind('<Escape>',lambda e: page.destroy())
nouveau.attributes('-fullscreen', True)
can1 = Canvas(nouveau, bg='dark gray', height = NB_BLOCS*LARGEUR_BLOC+HAUTEUR_BORDURE, \
width = NB_BLOCS*LARGEUR_BLOC)
can1.pack()
#Ici on va charger toutes les images necessaires qui sont toutes regroupées dans un même ficher
cleverte=PhotoImage(file="cleverte.gif")
clebleue = PhotoImage (file="clebleue.gif")
clerouge = PhotoImage (file="clerouge.gif")
cleor = PhotoImage (file="cleor.gif")
mur = PhotoImage (file="mur.gif")
porteverte = PhotoImage (file="porteverte.gif")
portebleue = PhotoImage (file="portebleue.gif")
porterouge = PhotoImage (file="porterouge.gif")
coffre = PhotoImage (file="coffre.gif")
#Ici on créer une classe Perso pour les mouvement du personnage qu'on a préalablement créer
class Perso(object):
def __init__(self):
self.bas = PhotoImage (file="Toad_devant.png")
self.haut = PhotoImage (file="Toad_deriere.png")
self.gauche = PhotoImage (file="Toad_gauche.png")
self.droite = PhotoImage (file="Toad_droite.png")
def existe(fname):
try: #En francais = essaie, permet au code de tester si ca marche sinon se référe à except dans le cas contraire
f = open(fname, 'r') #ouvre le fichier en lecture
f.close() #referme le fichier
return True #tout est ok
except: #code exécuté si ça n'a pas marché
return False #retourne: ça n'a pas marché
def interprete(ligne):
#interprete les codes de la ligne de niveau lue
i,j = 0,0
for lettre in ligne:
if lettre in ["C", "A", "M", "V", "B", "R", "E", "L", "O"]:
tab_plateau[i][j] = lettre
elif lettre == "X": #position initiale du personnage
perso.img = perso.bas
can1.create_image (CENTRE+i*LARGEUR_BLOC, CENTRE+j*LARGEUR_BLOC, image=perso.bas)
perso.x = i
perso.y = j
tab_plateau [i][j] = " "
#position du perso initial, inutile de la conserver, elle va changer
can1.pack()
if i < NB_BLOCS-1:
i = i+1
elif j < NB_BLOCS-1:
j = j+1
i = 0
def afficher(niveau):
#efface le dernier niveau sur l'écran et dans tab_plateau
can1.delete (ALL)
i,j = 0,0
for i in range (NB_BLOCS):
for j in range (NB_BLOCS):
tab_plateau [i][j] = " "
#ouvre le fichier s'il existe
if existe (fichniveau):
fich = open (fichniveau, "r")
i = 1
while i <= niveau:
ligne = fich.readline()
i = i+1
if ligne == "":
messagebox.showinfo ("GAGNE", "Vous avez atteint le dernier niveau disponible.")
else:
interprete (ligne)
fich.close
#récupération des événements clavier: flèches
nouveau.bind ("<Down>",bas)
nouveau.bind ("<Up>",haut)
nouveau.bind ("<Left>",gauche)
nouveau.bind ("<Right>",droite)
nouveau.bind ("<KeyPress-R>",recommencer)
else :
messagebox.showerror ("ERREUR", "fichier de niveau introuvable")
def verifgagne():
#verifier qu'il ne reste plus de portes sans clés et le coffre ouvert
for i in range (NB_BLOCS):
for j in range (NB_BLOCS):
if tab_plateau [i][j] in ["L", "O", "E", "C"]:
return (False)
break
return (True)
def rafraichir():
#rafraichit l'image après un déplacement
can1.delete(ALL)
#une petite aide en cas de blocage
can1.create_text (NB_BLOCS*LARGEUR_BLOC/2, NB_BLOCS*LARGEUR_BLOC+10, \
text="R pour recommencer ce niveau", font="Arial 10")
i,j=0,0
for i in range (NB_BLOCS):
for j in range (NB_BLOCS):
if tab_plateau [i][j] == "M":
can1.create_image (CENTRE+i*LARGEUR_BLOC, CENTRE+j*LARGEUR_BLOC, image=mur)
can1.pack()
elif tab_plateau[i][j] == "C":
can1.create_image(CENTRE+i*LARGEUR_BLOC, CENTRE+j*LARGEUR_BLOC, image=coffre)
can1.pack()
elif tab_plateau[i][j] == "A":
can1.create_image(CENTRE+i*LARGEUR_BLOC, CENTRE+j*LARGEUR_BLOC, image=cleor)
can1.pack()
elif tab_plateau[i][j] == "V":
can1.create_image(CENTRE+i*LARGEUR_BLOC, CENTRE+j*LARGEUR_BLOC, image=cleverte)
can1.pack()
elif tab_plateau [i][j] == "B":
can1.create_image (CENTRE+i*LARGEUR_BLOC, CENTRE+j*LARGEUR_BLOC, image=clebleue)
can1.pack()
elif tab_plateau [i][j] == "R":
can1.create_image (CENTRE+i*LARGEUR_BLOC, CENTRE+j*LARGEUR_BLOC, image=clerouge)
can1.pack()
elif tab_plateau [i][j] == "L":
can1.create_image (CENTRE+i*LARGEUR_BLOC, CENTRE+j*LARGEUR_BLOC, image=portebleue)
can1.pack()
elif tab_plateau [i][j] == "O":
can1.create_image (CENTRE+i*LARGEUR_BLOC, CENTRE+j*LARGEUR_BLOC, image=porterouge)
can1.pack()
elif tab_plateau[i][j] == "E":
can1.create_image (CENTRE+i*LARGEUR_BLOC, CENTRE+j*LARGEUR_BLOC, image=porteverte)
can1.pack()
#place perso
can1.create_image (CENTRE+perso.x*LARGEUR_BLOC, CENTRE+perso.y*LARGEUR_BLOC, image=perso.img)
can1.pack()
def pasUnBord(posx,posy):
if (posx >=0 and posy >=0 and \
posx <= NB_BLOCS-1 and posy <= NB_BLOCS-1):
return True
else:
return False
def cleVerteDevantPorteVerte(posx,posy,dx,dy):
if (tab_plateau [posx+dx][posy+dy] == "V") and \
(tab_plateau [posx+2*dx][posy+2*dy] == "E"):
return True
else:
return False
def cleRougeDevantPorteRouge(posx,posy,dx,dy):
if (tab_plateau[posx+dx][posy+dy] == "R") and \
(tab_plateau[posx+2*dx][posy+2*dy] == "O"):
return True
else:
return False
def cleBleueDevantPorteBleue(posx,posy,dx,dy):
if (tab_plateau[posx+dx][posy+dy] == "B") and \
(tab_plateau[posx+2*dx][posy+2*dy] == "L"):
return True
else:
return False
def cleOrDevantCoffre(posx,posy,dx,dy):
if (tab_plateau[posx+dx][posy+dy]=="A") and \
(tab_plateau[posx+2*dx][posy+2*dy]=="C"):
return True
else:
return False
def cleDevantVide(posx,posy,dx,dy):
if(tab_plateau[posx+dx][posy+dy] in ["V","B","R","A"]) and \
(tab_plateau[perso.x+2*dx][perso.y+2*dy]==" " ):
return True
else:
return False
def avance(xdirection,ydirection):
global niveau
avancePerso=False
if pasUnBord(perso.x+xdirection, perso.y+ydirection):
if tab_plateau[perso.x+xdirection][perso.y+ydirection] == " ":
#vide: le personnage peut avancer
avancePerso = True
elif pasUnBord (perso.x+2*xdirection, perso.y+2*ydirection >= 0):
#personnage avec la bonne clé devant la bonne porte
if cleVerteDevantPorteVerte(perso.x, perso.y, xdirection, ydirection) or \
cleRougeDevantPorteRouge(perso.x, perso.y, xdirection, ydirection) or \
cleBleueDevantPorteBleue(perso.x, perso.y, xdirection, ydirection) or \
cleOrDevantCoffre(perso.x, perso.y, xdirection, ydirection):
tab_plateau[perso.x+2*xdirection][perso.y+2*ydirection]=" "
avancePerso = True
tab_plateau[perso.x+xdirection][perso.y+ydirection] = " "
# personnage pousse une clé devant un vide
elif cleDevantVide(perso.x, perso.y, xdirection, ydirection):
#case vide déplace la clé
tab_plateau [perso.x+2*xdirection][perso.y+2*ydirection] = tab_plateau [perso.x+xdirection][perso.y+ydirection]
avancePerso = True
tab_plateau[perso.x+xdirection][perso.y+ydirection] = " "
#efface la place de l'ancienne clé
#déplace perso
if avancePerso == True:
perso.x = perso.x+ xdirection
perso.y = perso.y+ ydirection
rafraichir()
#si gagne ? niveau suivant
if verifgagne() == True:
print (str(niveau))
print ("Vous avez gagné!")
niveau = niveau+1
print ("niveau:" +str(niveau))
afficher (niveau)
def gauche (event):
#fleche gauche
perso.img=perso.gauche
avance (-1, 0)
def droite (event):
#fleche droite
perso.img = perso.droite
avance (1, 0)
def bas(event):
#fleche vers le bas
perso.img = perso.bas
avance (0, 1)
def haut (event):
perso.img = perso.haut
avance(0,-1)
def recommencer (event):
afficher (niveau)
#programme principal
perso = Perso()
niveau = 1
afficher (niveau)
#lancer la boucle
#ici on definit notre page "de garde"
def create():
nouveau = Toplevel(page)
nouveau.config(bg="#842e1b")
nouveau.bind('<Escape>',lambda e: page.destroy())
nouveau.attributes('-fullscreen', True)
frame2 = Frame(nouveau, bg="#842e1b")
nouveau.attributes('-fullscreen', True)
frame2.pack(expand=YES)
#label_intro = Label(frame2, text="Vous êtes actuellement prisonier d'un chateau.\n\nLe but du jeu est de vous y enfuir.\n\nPour ce faire, une suite de salles vous attend.\n\nPour les passer, rien de plus simple, gagnez le mini jeu et la salle suivant est votre.\n\nBonne chance, et que le sort vous soit favorable !", font=("Times New Roman", 25), fg='white', bg='#842e1b')
#Code bouton
lets_go = Button(frame2, text="J'ai compris", font=("Times New Roman", 30), fg='white', bg='#842e1b', command = jeu_labyrinte)
lets_go.pack()
#label_intro.pack()
# code page "start"
page = Tk()
page.attributes('-fullscreen', True)
page.config(background='#c0001a')
page.bind('<Escape>',lambda e: page.destroy())
#Code box
frame = Frame(page, bg='#c0001a')
frame.pack(expand=YES)
# code text
label_title = Label(frame, text="Bienvenue sur ce jeu", font=("Times New Roman", 70), fg='white', bg='#c0001a')
label_title.pack(expand=YES)
label_soustitle = Label(page, text="Par Emma et Sana", font=("Times New Roman", 30), fg='white', bg='#c0001a')
label_soustitle.pack(side=BOTTOM)
#Code bouton
start = Button(frame, text="Start !", font=("Times New Roman", 70), fg='#c0001a', bg='white', command = create)
start.pack(pady=25, fill=X)
page.mainloop()
jee pee
Messages postés39621Date d'inscriptionmercredi 2 mai 2007StatutModérateurDernière intervention23 avril 20249 232 19 mars 2022 à 18:14
Bonjour,
La plupart du temps "Mon code ne marche pas" ne fait pas une question pertinente. Sur un code de 4 lignes, nous allons pouvoir nous faire une idée du problème. Mais là, si tu ne détailles pas le soucis comment veux tu que l'on s'y intéresse.