from tkinter import * from random import randrange,choice from a import * ### PARAMETRE DU LABYRINTHE : x=30 # nbr de lignes dans le labyrinthe y=30 # nbr de colonnes dans le labyrinthe eh = 10 #ecart haut eg = 10 #ecart gauche lc = 20 #largeur cellule lt = 2 #largeur trait pe = 60 # police d'ecriture b = 10 # bordure du cadre ## cg = randrange(4) gag1 = 0 gag2 = 0 gag3 = 0 gag4 = 0 defi = 0 JEU = 0 g = 0 if cg == 0: Xpj2 = 0 Ypj2 = 0 Xpj1 = x-1 Ypj1 = y-1 elif cg == 1: Xpj2 = x-1 Ypj2 = 0 Xpj1 = 0 Ypj1 = y-1 elif cg == 2: Xpj2 = x-1 Ypj2 = y-1 Xpj1 = 0 Ypj1 = 0 elif cg == 3: Xpj2 = 0 Ypj2 = y-1 Xpj1 = x-1 Ypj1 = 0 def lab(x,y): labyrinthe = [] LA = [[1] * x] *y for i in range(y): l = [] for j in range(x): l.append(0) labyrinthe.append(l) X = randrange(x) Y = randrange(y) labyrinthe[Y][X] = 1 p_verti = [] p_horiz = [] historique = [[X,Y]] for i in range(y): l = [] for j in range(x-1): l.append(1) p_verti.append(l) for i in range(y-1): l = [] for j in range(x): l.append(1) p_horiz.append(l) while LA != labyrinthe: X = historique[-1][0] Y = historique[-1][1] possibilite = [] if (Y-1 >= 0): if (labyrinthe[Y-1][X]== 0): possibilite.append(0) if (X+1 < x): if (labyrinthe[Y][X+1]== 0): possibilite.append(1) if (Y+1 < y): if (labyrinthe[Y+1][X]== 0): possibilite.append(2) if (X-1 >= 0): if (labyrinthe[Y][X-1]== 0): possibilite.append(3) if possibilite != []: dire = choice(possibilite) if dire == 0: X1 = X Y1 = Y-1 p_horiz[Y-1][X] = 0 if dire == 1: X1 = X+1 Y1 = Y p_verti[Y][X] = 0 if dire == 2: X1 = X Y1 = Y+1 p_horiz[Y][X] = 0 if dire == 3: X1 = X-1 Y1 = Y p_verti[Y][X-1] = 0 labyrinthe[Y1][X1] = 1 if len(possibilite) >= 1: historique.append([X1,Y1]) else: del historique[-1] laby = [p_verti,p_horiz] return laby def resoudre(p_verti,p_horiz,Xa,Ya,Xd,Yd): y = len(p_verti) x = len(p_horiz[0]) trajet = [] for i in range(y): l = [] for j in range(x): l.append(0) trajet.append(l) trajet[Ya][Xa] = 2 trajet[Yd][Xd] = 1 histo = [[Xd,Yd]] X = Xd Y = Yd while trajet[Y][X] != 2: X = histo[-1][0] Y = histo[-1][1] possibilite = [] if (Y-1 >= 0): if (p_horiz[Y-1][X] == 0) and (trajet[Y-1][X] != -1) and (trajet[Y-1][X] != 1): possibilite.append(0) if (X+1 < x): if (p_verti[Y][X] == 0) and (trajet[Y][X+1] != -1) and (trajet[Y][X+1] != 1): possibilite.append(1) if (Y+1 < y): if (p_horiz[Y][X] == 0) and (trajet[Y+1][X] != -1) and (trajet[Y+1][X] != 1): possibilite.append(2) if (X-1 >= 0): if (p_verti[Y][X-1] == 0) and (trajet[Y][X-1] != -1) and (trajet[Y][X-1] != 1): possibilite.append(3) if possibilite != []: dire = possibilite[-1] if dire == 0: X1 = X Y1 = Y-1 if dire == 1: X1 = X+1 Y1 = Y if dire == 2: X1 = X Y1 = Y+1 if dire == 3: X1 = X-1 Y1 = Y if trajet[Y1][X1] != 2: trajet[Y1][X1] = 1 X = X1 Y = Y1 histo.append([X1,Y1]) else: trajet[Y][X] = -1 del histo[-1] for i in range(y): for j in range(x): if trajet[i][j] != 1: trajet[i][j] = 0 trajet[Ya][Xa] = 2 trajet[Yd][Xd] = 2 return trajet L = lab(x,y) S = resoudre(L[0],L[1],Xpj2,Ypj2,Xpj1,Ypj1) def fctHaut(ev=None): global Xpj1 global Ypj1 global g global gag1, gag2, gag3, gag4 Xd = Xpj1 Yd = Ypj1-1 if (Xd>= 0 and Xd < x and Yd>= 0 and Yd < y) and (L[1][Ypj1-1][Xpj1] == 0) and g == 0: can.coords(perso1,lc-lt-1+eg+Xd*lc,lc-lt-1+eh+Yd*lc,eg+Xd*lc+lt+1,eh+Yd*lc+lt+1) Xpj1 = Xd Ypj1 = Yd if Xpj1 == Xa and Ypj1 == Ya: g = 1 texte = Label(text="Le Joueur 1 gagne !",font = pe,relief="ridge",borderwidth = b,background ="cyan",padx= b//2, pady= b//2) texte.place(x=(eg*2+x*lc)//2, y=(eh*2+y*lc)//2,anchor="center") if Xpj1 == Xsp1 and Ypj1 == Ysp1 and gag1 == 0: while gag1 == 0: gag1 = jeu1() #le probleme est ici(est sur les 7 autres fonctions de deplacement), j'aimerais que le programme bloque sur cette fonction tant que jeu1() n'a pas retourné de valeur (1 ou 2) if Xpj1 == Xsp2 and Ypj1 == Ysp2 and gag2 == 0: gag2 = jeu2() if Xpj1 == Xsp3 and Ypj1 == Ysp3 and gag3 == 0: gag3 = jeu3() if Xpj1 == Xsp4 and Ypj1 == Ysp4 and gag4 == 0: gag4 = jeu4() def fctBas(ev=None): global Xpj1 global Ypj1 global g global gag1, gag2, gag3, gag4 Xd = Xpj1 Yd = Ypj1+1 if (Xd>= 0 and Xd < x and Yd>= 0 and Yd < y) and (L[1][Ypj1][Xpj1] == 0) and g == 0: can.coords(perso1,lc-lt-1+eg+Xd*lc,lc-lt-1+eh+Yd*lc,eg+Xd*lc+lt+1,eh+Yd*lc+lt+1) Xpj1 = Xd Ypj1 = Yd if Xpj1 == Xa and Ypj1 == Ya: g = 1 texte = Label(text="Le Joueur 1 gagne !",font = pe,relief="ridge",borderwidth = b,background ="cyan",padx= b//2, pady= b//2) texte.place(x=(eg*2+x*lc)//2, y=(eh*2+y*lc)//2,anchor="center") if Xpj1 == Xsp1 and Ypj1 == Ysp1 and gag1 == 0: while gag1 == 0: gag1 = jeu1() if Xpj1 == Xsp2 and Ypj1 == Ysp2 and gag2 == 0: gag2 = jeu2() if Xpj1 == Xsp3 and Ypj1 == Ysp3 and gag3 == 0: gag3 = jeu3() if Xpj1 == Xsp4 and Ypj1 == Ysp4 and gag4 == 0: gag4 = jeu4() def fctDroite(ev=None): global Xpj1 global Ypj1 global g global gag1, gag2, gag3, gag4 Xd = Xpj1+1 Yd = Ypj1 if (Xd>= 0 and Xd < x and Yd>= 0 and Yd < y) and (L[0][Ypj1][Xpj1] == 0) and g == 0: can.coords(perso1,lc-lt-1+eg+Xd*lc,lc-lt-1+eh+Yd*lc,eg+Xd*lc+lt+1,eh+Yd*lc+lt+1) Xpj1 = Xd Ypj1 = Yd if Xpj1 == Xa and Ypj1 == Ya: g = 1 texte = Label(text="Le Joueur 1 gagne !",font = pe,relief="ridge",borderwidth = b,background ="cyan",padx= b//2, pady= b//2) texte.place(x=(eg*2+x*lc)//2, y=(eh*2+y*lc)//2,anchor="center") if Xpj1 == Xsp1 and Ypj1 == Ysp1 and gag1 == 0: while gag1 == 0: gag1 = jeu1() if Xpj1 == Xsp2 and Ypj1 == Ysp2 and gag2 == 0: gag2 = jeu2() if Xpj1 == Xsp3 and Ypj1 == Ysp3 and gag3 == 0: gag3 = jeu3() if Xpj1 == Xsp4 and Ypj1 == Ysp4 and gag4 == 0: gag4 = jeu4() def fctGauche(ev=None): global Xpj1 global Ypj1 global g global gag1, gag2, gag3, gag4 Xd = Xpj1-1 Yd = Ypj1 if (Xd>= 0 and Xd < x and Yd>= 0 and Yd < y) and (L[0][Ypj1][Xpj1-1] == 0) and g == 0: can.coords(perso1,lc-lt-1+eg+Xd*lc,lc-lt-1+eh+Yd*lc,eg+Xd*lc+lt+1,eh+Yd*lc+lt+1) Xpj1 = Xd Ypj1 = Yd if Xpj1 == Xa and Ypj1 == Ya: g = 1 texte = Label(text="Le Joueur 1 gagne !",font = pe,relief="ridge",borderwidth = b,background ="cyan",padx= b//2, pady= b//2) texte.place(x=(eg*2+x*lc)//2, y=(eh*2+y*lc)//2,anchor="center") if Xpj1 == Xsp1 and Ypj1 == Ysp1 and gag1 == 0: while gag1 == 0: gag1 = jeu1() if Xpj1 == Xsp2 and Ypj1 == Ysp2 and gag2 == 0: gag2 = jeu2() if Xpj1 == Xsp3 and Ypj1 == Ysp3 and gag3 == 0: gag3 = jeu3() if Xpj1 == Xsp4 and Ypj1 == Ysp4 and gag4 == 0: gag4 = jeu4() def fctZ(ev=None): global Xpj2 global Ypj2 global g global gag1, gag2, gag3, gag4 Xd = Xpj2 Yd = Ypj2-1 if (Xd>= 0 and Xd < x and Yd>= 0 and Yd < y) and (L[1][Ypj2-1][Xpj2] == 0) and g == 0: can.coords(perso2,lc-lt-1+eg+Xd*lc,lc-lt-1+eh+Yd*lc,eg+Xd*lc+lt+1,eh+Yd*lc+lt+1) Xpj2 = Xd Ypj2 = Yd if Xpj2 == Xa and Ypj2 == Ya: g = 1 texte = Label(text="Le Joueur 2 gagne !",font = pe,relief="ridge",borderwidth = b,background ="cyan",padx= b//2, pady= b//2) texte.place(x=(eg*2+x*lc)//2, y=(eh*2+y*lc)//2,anchor="center") if Xpj2 == Xsp1 and Ypj2 == Ysp1 and gag1 == 0: while gag1 == 0: gag1 = jeu1() if Xpj2 == Xsp2 and Ypj2 == Ysp2 and gag2 == 0: gag2 = jeu2() if Xpj2 == Xsp3 and Ypj2 == Ysp3 and gag3 == 0: gag3 = jeu3() if Xpj2 == Xsp4 and Ypj2 == Ysp4 and gag4 == 0: gag4 = jeu4() def fctS(ev=None): global Xpj2 global Ypj2 global g global gag1, gag2, gag3, gag4 Xd = Xpj2 Yd = Ypj2+1 if (Xd>= 0 and Xd < x and Yd>= 0 and Yd < y) and (L[1][Ypj2][Xpj2] == 0) and g == 0: can.coords(perso2,lc-lt-1+eg+Xd*lc,lc-lt-1+eh+Yd*lc,eg+Xd*lc+lt+1,eh+Yd*lc+lt+1) Xpj2 = Xd Ypj2 = Yd if Xpj2 == Xa and Ypj2 == Ya: g = 1 texte = Label(text="Le Joueur 2 gagne !",font = pe,relief="ridge",borderwidth = b,background ="cyan",padx= b//2, pady= b//2) texte.place(x=(eg*2+x*lc)//2, y=(eh*2+y*lc)//2,anchor="center") if Xpj2 == Xsp1 and Ypj2 == Ysp1 and gag1 == 0: while gag1 == 0: gag1 = jeu1() if Xpj2 == Xsp2 and Ypj2 == Ysp2 and gag2 == 0: gag2 = jeu2() if Xpj2 == Xsp3 and Ypj2 == Ysp3 and gag3 == 0: gag3 = jeu3() if Xpj2 == Xsp4 and Ypj2 == Ysp4 and gag4 == 0: gag4 = jeu4() def fctD(ev=None): global Xpj2 global Ypj2 global g global gag1, gag2, gag3, gag4 Xd = Xpj2+1 Yd = Ypj2 if (Xd>= 0 and Xd < x and Yd>= 0 and Yd < y) and (L[0][Ypj2][Xpj2] == 0) and g == 0: can.coords(perso2,lc-lt-1+eg+Xd*lc,lc-lt-1+eh+Yd*lc,eg+Xd*lc+lt+1,eh+Yd*lc+lt+1) Xpj2 = Xd Ypj2 = Yd if Xpj2 == Xa and Ypj2 == Ya: g = 1 texte = Label(text="Le Joueur 2 gagne !",font = pe,relief="ridge",borderwidth = b,background ="cyan",padx= b//2, pady= b//2) texte.place(x=(eg*2+x*lc)//2, y=(eh*2+y*lc)//2,anchor="center") if Xpj2 == Xsp1 and Ypj2 == Ysp1 and gag1 == 0: while gag1 == 0: gag1 = jeu1() if Xpj2 == Xsp2 and Ypj2 == Ysp2 and gag2 == 0: gag2 = jeu2() if Xpj2 == Xsp3 and Ypj2 == Ysp3 and gag3 == 0: gag3 = jeu3() if Xpj2 == Xsp4 and Ypj2 == Ysp4 and gag4 == 0: gag4 = jeu4() def fctQ(ev=None): global Xpj2 global Ypj2 global g global gag1, gag2, gag3, gag4 Xd = Xpj2-1 Yd = Ypj2 if (Xd>= 0 and Xd < x and Yd>= 0 and Yd < y) and (L[0][Ypj2][Xpj2-1] == 0) and g == 0: can.coords(perso2,lc-lt-1+eg+Xd*lc,lc-lt-1+eh+Yd*lc,eg+Xd*lc+lt+1,eh+Yd*lc+lt+1) Xpj2 = Xd Ypj2 = Yd if Xpj2 == Xa and Ypj2 == Ya: g = 1 texte = Label(text="Le Joueur 2 gagne !",font = pe,relief="ridge",borderwidth = b,background ="cyan",padx= b//2, pady= b//2) texte.place(x=(eg*2+x*lc)//2, y=(eh*2+y*lc)//2,anchor="center") if Xpj2 == Xsp1 and Ypj2 == Ysp1 and gag1 == 0: while gag1 == 0: gag1 = jeu1() if Xpj2 == Xsp2 and Ypj2 == Ysp2 and gag2 == 0: gag2 = jeu2() if Xpj2 == Xsp3 and Ypj2 == Ysp3 and gag3 == 0: gag3 = jeu3() if Xpj2 == Xsp4 and Ypj2 == Ysp4 and gag4 == 0: gag4 = jeu4() fen=Tk() can=Canvas(fen,width=eg*2+x*lc,height=eh*2+y*lc,bg='ivory') can.pack() b1=Button(fen,text='arret',command=fen.destroy) b1.pack() long = 0 for i in range(len(S)): for j in range(len(S[i])): if S[i][j] == 1: long += 1 X = Xpj2 Y = Ypj2 T = S for i in range(long): ch = 0 if (Y-1 >= 0) and ch == 0 and S[Y-1][X] == 1: if (L[1][Y-1][X] == 0): Y += -1 ch = 1 if (X+1 < x) and ch == 0 and S[Y][X+1] == 1: if (L[0][Y][X] == 0): X += 1 ch = 1 if (Y+1 < y) and ch == 0 and S[Y+1][X] == 1: if (L[1][Y][X] == 0): Y += 1 ch = 1 if (X-1 >= 0) and ch == 0 and S[Y][X-1] == 1: if (L[0][Y][X-1] == 0): X += -1 ch = 1 if i == long//6 * 3 - 1: Xa = X Ya = Y if i == (long)//6 - 1: Xsp1 = X Ysp1 = Y if i == (long)//6 * 2 - 1: Xsp2 = X Ysp2 = Y if i == (long)//6 * 4 - 1: Xsp3 = X Ysp3 = Y if i == (long)//6 * 5 -1: Xsp4 = X Ysp4 = Y S[Y][X] = 0 S = T can.create_line(eg,eh,eg+x*lc,eh,fill='black',width=lt) can.create_line(eg,eh,eg,eh+y*lc,fill='black',width=lt) can.create_line(eg+x*lc,eh+y*lc,eg+x*lc,eh,fill='black',width=lt) can.create_line(eg,eh+y*lc,eg+x*lc,eh+y*lc,fill='black',width=lt) for i in range(len(L[0])): for j in range(len(L[0][i])): if L[0][i][j]: can.create_line(eg+lc+j*lc,eh+i*lc,eg+lc+j*lc,eh+lc+i*lc,fill='black',width=lt) for i in range(len(L[1])): for j in range(len(L[1][i])): if L[1][i][j]: can.create_line(eg+j*lc,eh+lc+i*lc,eg+lc+j*lc,eh+lc+i*lc,fill='black',width=lt) can.create_rectangle(lt//2+eg+Xa*lc,lt//2+eh+Ya*lc,eg+Xa*lc+lc-lt//2,eh+Ya*lc+lc-lt//2,fill='#00C82F', width= 0) can.create_rectangle(lt//2+eg+Xsp1*lc,lt//2+eh+Ysp1*lc,eg+Xsp1*lc+lc-lt//2,eh+Ysp1*lc+lc-lt//2,fill='grey', width= 0) can.create_rectangle(lt//2+eg+Xsp2*lc,lt//2+eh+Ysp2*lc,eg+Xsp2*lc+lc-lt//2,eh+Ysp2*lc+lc-lt//2,fill='grey', width= 0) can.create_rectangle(lt//2+eg+Xsp3*lc,lt//2+eh+Ysp3*lc,eg+Xsp3*lc+lc-lt//2,eh+Ysp3*lc+lc-lt//2,fill='grey', width= 0) can.create_rectangle(lt//2+eg+Xsp4*lc,lt//2+eh+Ysp4*lc,eg+Xsp4*lc+lc-lt//2,eh+Ysp4*lc+lc-lt//2,fill='grey', width= 0) perso1 =can.create_oval(lc-lt-1+eg+Xpj1*lc,lc-lt-1+eh+Ypj1*lc,eg+Xpj1*lc+lt+1,eh+Ypj1*lc+lt+1,fill='red') perso2 =can.create_oval(lc-lt-1+eg+Xpj2*lc,lc-lt-1+eh+Ypj2*lc,eg+Xpj2*lc+lt+1,eh+Ypj2*lc+lt+1,fill='blue') can.bind_all('',fctHaut) can.bind_all('',fctBas) can.bind_all('',fctDroite) can.bind_all('',fctGauche) can.bind_all('',fctZ) can.bind_all('',fctS) can.bind_all('',fctD) can.bind_all('',fctQ) fen.mainloop()