from tkinter import * def impression(m, l, h) : for i in range(0,haut) : print(' ') for j in range (0,larg) : print ( m[i] [j], end =' ') #On définit la classe "Matrice" qui sera ici le support du jeu class Matrice: #On initie / définie les valeurs nécessaires à la construction de l'objet def __init__(self) : self.x = 45 #On définit maintenant ce qu'est la fenêtre dans son ensemble def fenetre(self): #Création de la fenêtre self.main_window = Tk() self.main_window.title('Labyrinthe') self.main_window.resizable(False, False) #Création du support du Canevas qui va contenir la matrice #Création du Canevas self.can = Canvas(self.main_window, width = self.x * 20, height = self.x * 18, bg = 'white') self.can.grid(row = 0, column = 0, columnspan = 1) #Fonction ayant pour but de lire chaque ligne de la matrice et d'afficher différentes images en fonction du contenu de la matrice def affichage_case(self,matrice1): self.i = PhotoImage(file="1.png") self.i2 = PhotoImage(file="2.png") self.i3 = PhotoImage(file="3.png") for i in range(0,18) : for i2 in range(0,20): if matrice1[i][i2]=='B': self.can.create_image(self.x * (i2+0.5), self.x * (i+0.5), image=self.i) if matrice1[i][i2]=='0': self.can.create_image(self.x * (i2+0.5), self.x * (i+0.5), image=self.i2) if matrice1[i][i2]=='A': self.can.create_image(self.x * (i2+0.5), self.x * (i+0.5), image=self.i3) if matrice1[i][i2]=='2': self.can.create_rectangle(self.x * i2-1,self.x * i,self.x * (i2 + 1),self.x * (i+1),fill='blue',width=0) if matrice1[i][i2]=='3': self.can.create_rectangle(self.x * i2,self.x * i,self.x * (i2 + 1),self.x * (i+1),fill='yellow',width=0) #On définit maintenant les caractéristiques d'un personnage jouable class Personnage: #On initie / définit les valeurs nécessaires à la construction de l'objet def __init__(self): self.x = 1 self.y = 0 self.t = 45 self.relachement = True #On affiche le personnage dans le Canevas def affichage(self,canvas): global direction #On attribue différentes couleurs pour chaque direction qu'empreinte le personnage ( les couleurs seront remplacées par des images ) if direction == "droite": self.image=canvas.can.create_oval(self.t * (self.x + 0.25), self.t * (self.y + 0.25), self.t * (self.x + 0.75), self.t * (self.y + 0.75), fill = 'green') if direction == "gauche": self.image=canvas.can.create_oval(self.t * (self.x + 0.25), self.t * (self.y + 0.25), self.t * (self.x + 0.75), self.t * (self.y + 0.75), fill = 'pink') if direction == "haut": self.image=canvas.can.create_oval(self.t * (self.x + 0.25), self.t * (self.y + 0.25), self.t * (self.x + 0.75), self.t * (self.y + 0.75), fill = 'blue') if direction == "bas": self.image=canvas.can.create_oval(self.t * (self.x + 0.25), self.t * (self.y + 0.25), self.t * (self.x + 0.75), self.t * (self.y + 0.75), fill = 'orange') if direction == "départ": self.image=canvas.can.create_oval(self.t * (self.x + 0.25), self.t * (self.y + 0.25), self.t * (self.x + 0.75), self.t * (self.y + 0.75), fill = 'brown') #On définit les fonctions qui permettent le déplacement en fonction de la touche préssée def deplacement_haut(self,evt): global matrice1, direction, laby #On permet le déplacement seulement si la touche a été relâcher afin d'empêcher la réactivation de la fonction ( éviter un déplacement trop rapide ) if self.relachement == True: #Si la case de la matrice correspondante à l'endroit où le joueur désire aller est un mur, la fonction n'effectue rien et le programme attend de nouvelles instructions if matrice1[self.y-1][self.x] == 'B': pass #Si la case de la matrice correspondante à l'endroit où le joueur désire aller n'est pas un mur, la fonction va : elif matrice1[self.y-1][self.x] == '0' or 'A' or '2' or '3': direction = "haut" #Définir la direction du joueur self.y = self.y-1 #Changer les coordonnées du joueur laby.can.delete(self.image) #Supprimer l'image de l'ancienne position du joueur self.affichage(laby) #Réafficher le joueur à sa nouvelle position self.relachement = False #Définir que la touche est préssée else: pass if matrice1[self.y][self.x] == '2': victoire = Label(Toplevel(laby.main_window), text = 'Vous avez gagné',) victoire.pack() def deplacement_bas(self,evt): global matrice1, direction, laby if self.relachement == True: if matrice1[self.y][self.x] == '3': pass elif matrice1[self.y+1][self.x] == 'B': pass elif matrice1[self.y+1][self.x] == '0' or 'A' or '2': direction = "bas" self.y = self.y+1 laby.can.delete(self.image) self.affichage(laby) self.relachement = False else: pass def deplacement_gauche(self,evt): global matrice1, direction, laby if self.relachement == True: if matrice1[self.y][self.x-1] == 'B': pass elif matrice1[self.y][self.x-1] == '0' or 'A' or '2' or '3': direction = "gauche" self.x = self.x-1 laby.can.delete(self.image) self.affichage(laby) self.relachement = False else: pass def deplacement_droite(self,evt): global matrice1, direction, laby if self.relachement == True: if matrice1[self.y][self.x] == '3': pass elif matrice1[self.y][self.x+1] == 'B': pass elif matrice1[self.y][self.x+1] == '0' or 'A' or '2': direction = "droite" self.x = self.x+1 laby.can.delete(self.image) self.affichage(laby) self.relachement = False else: pass def relachement_touche(self,evt): self.relachement = True #MAIN #Création de la matrice et initialisation de la variable permettant au programme de déterminer la direction du joueur direction = "départ" larg = 20 haut = 18 ligne = [0 for i in range(0,larg)] matrice1 = [ligne[:] for i in range (0,haut)] #Valeur de chacunes des cases de la matrice : B => Mur, A => Départ, 0 => Chemin, 3 => Fausse sortie, 2 => Fin du niveau matrice1 [0][0] = 'B' matrice1 [0][1] = 'A' matrice1 [0][2] = 'B' matrice1 [0][3] = 'B' matrice1 [0][4] = 'B' matrice1 [0][5] = 'B' matrice1 [0][6] = 'B' matrice1 [0][7] = 'B' matrice1 [0][8] = 'B' matrice1 [0][9] = 'B' matrice1 [0][10] = 'B' matrice1 [0][11] = 'B' matrice1 [0][12] = 'B' matrice1 [0][13] = 'B' matrice1 [0][14] = 'B' matrice1 [0][15] = 'B' matrice1 [0][16] = 'B' matrice1 [0][17] = 'B' matrice1 [0][18] = 'B' matrice1 [0][19] = 'B' matrice1 [1][0] = 'B' matrice1 [1][1] = '0' matrice1 [1][2] = '0' matrice1 [1][3] = '0' matrice1 [1][4] = '0' matrice1 [1][5] = '0' matrice1 [1][6] = '0' matrice1 [1][7] = '0' matrice1 [1][8] = '0' matrice1 [1][9] = '0' matrice1 [1][10] = '0' matrice1 [1][11] = '0' matrice1 [1][12] = '0' matrice1 [1][13] = '0' matrice1 [1][14] = '0' matrice1 [1][15] = '0' matrice1 [1][16] = '0' matrice1 [1][17] = '0' matrice1 [1][18] = '0' matrice1 [1][19] = 'B' matrice1 [2][0] = 'B' matrice1 [2][1] = '0' matrice1 [2][2] = 'B' matrice1 [2][3] = 'B' matrice1 [2][4] = 'B' matrice1 [2][5] = 'B' matrice1 [2][6] = 'B' matrice1 [2][7] = 'B' matrice1 [2][8] = 'B' matrice1 [2][9] = '0' matrice1 [2][10] = 'B' matrice1 [2][11] = 'B' matrice1 [2][12] = 'B' matrice1 [2][13] = 'B' matrice1 [2][14] = '0' matrice1 [2][15] = 'B' matrice1 [2][16] = 'B' matrice1 [2][17] = 'B' matrice1 [2][18] = '0' matrice1 [2][19] = 'B' matrice1 [3][0] = 'B' matrice1 [3][1] = '0' matrice1 [3][2] = 'B' matrice1 [3][3] = '0' matrice1 [3][4] = '0' matrice1 [3][5] = '0' matrice1 [3][6] = '0' matrice1 [3][7] = 'B' matrice1 [3][8] = '0' matrice1 [3][9] = '0' matrice1 [3][10] = 'B' matrice1 [3][11] = 'B' matrice1 [3][12] = '0' matrice1 [3][13] = 'B' matrice1 [3][14] = '0' matrice1 [3][15] = '0' matrice1 [3][16] = '0' matrice1 [3][17] = 'B' matrice1 [3][18] = '0' matrice1 [3][19] = 'B' matrice1 [4][0] = 'B' matrice1 [4][1] = '0' matrice1 [4][2] = 'B' matrice1 [4][3] = '0' matrice1 [4][4] = 'B' matrice1 [4][5] = 'B' matrice1 [4][6] = '0' matrice1 [4][7] = 'B' matrice1 [4][8] = '0' matrice1 [4][9] = 'B' matrice1 [4][10] = 'B' matrice1 [4][11] = '0' matrice1 [4][12] = '0' matrice1 [4][13] = 'B' matrice1 [4][14] = '0' matrice1 [4][15] = 'B' matrice1 [4][16] = '0' matrice1 [4][17] = 'B' matrice1 [4][18] = '0' matrice1 [4][19] = 'B' matrice1 [5][0] = 'B' matrice1 [5][1] = '0' matrice1 [5][2] = 'B' matrice1 [5][3] = '0' matrice1 [5][4] = '0' matrice1 [5][5] = 'B' matrice1 [5][6] = '0' matrice1 [5][7] = 'B' matrice1 [5][8] = '0' matrice1 [5][9] = 'B' matrice1 [5][10] = '0' matrice1 [5][11] = '0' matrice1 [5][12] = 'B' matrice1 [5][13] = 'B' matrice1 [5][14] = '0' matrice1 [5][15] = 'B' matrice1 [5][16] = '0' matrice1 [5][17] = 'B' matrice1 [5][18] = '0' matrice1 [5][19] = 'B' matrice1 [6][0] = 'B' matrice1 [6][1] = '0' matrice1 [6][2] = 'B' matrice1 [6][3] = 'B' matrice1 [6][4] = 'B' matrice1 [6][5] = 'B' matrice1 [6][6] = '0' matrice1 [6][7] = 'B' matrice1 [6][8] = '0' matrice1 [6][9] = '0' matrice1 [6][10] = '0' matrice1 [6][11] = 'B' matrice1 [6][12] = 'B' matrice1 [6][13] = 'B' matrice1 [6][14] = '0' matrice1 [6][15] = 'B' matrice1 [6][16] = '0' matrice1 [6][17] = 'B' matrice1 [6][18] = '0' matrice1 [6][19] = 'B' matrice1 [7][0] = 'B' matrice1 [7][1] = '0' matrice1 [7][2] = '0' matrice1 [7][3] = '0' matrice1 [7][4] = '0' matrice1 [7][5] = '0' matrice1 [7][6] = '0' matrice1 [7][7] = 'B' matrice1 [7][8] = 'B' matrice1 [7][9] = 'B' matrice1 [7][10] = 'B' matrice1 [7][11] = 'B' matrice1 [7][12] = 'B' matrice1 [7][13] = '0' matrice1 [7][14] = '0' matrice1 [7][15] = '0' matrice1 [7][16] = '0' matrice1 [7][17] = 'B' matrice1 [7][18] = '0' matrice1 [7][19] = 'B' matrice1 [8][0] = 'B' matrice1 [8][1] = '0' matrice1 [8][2] = 'B' matrice1 [8][3] = 'B' matrice1 [8][4] = 'B' matrice1 [8][5] = 'B' matrice1 [8][6] = 'B' matrice1 [8][7] = 'B' matrice1 [8][8] = '0' matrice1 [8][9] = '0' matrice1 [8][10] = '0' matrice1 [8][11] = '0' matrice1 [8][12] = 'B' matrice1 [8][13] = '0' matrice1 [8][14] = 'B' matrice1 [8][15] = 'B' matrice1 [8][16] = '0' matrice1 [8][17] = 'B' matrice1 [8][18] = '0' matrice1 [8][19] = 'B' matrice1 [9][0] = 'B' matrice1 [9][1] = '0' matrice1 [9][2] = 'B' matrice1 [9][3] = '0' matrice1 [9][4] = '0' matrice1 [9][5] = '0' matrice1 [9][6] = '0' matrice1 [9][7] = '0' matrice1 [9][8] = '0' matrice1 [9][9] = 'B' matrice1 [9][10] = 'B' matrice1 [9][11] = '0' matrice1 [9][12] = 'B' matrice1 [9][13] = '0' matrice1 [9][14] = '0' matrice1 [9][15] = 'B' matrice1 [9][16] = '0' matrice1 [9][17] = 'B' matrice1 [9][18] = '0' matrice1 [9][19] = 'B' matrice1 [10][0] = 'B' matrice1 [10][1] = '0' matrice1 [10][2] = 'B' matrice1 [10][3] = '0' matrice1 [10][4] = 'B' matrice1 [10][5] = 'B' matrice1 [10][6] = 'B' matrice1 [10][7] = '0' matrice1 [10][8] = 'B' matrice1 [10][9] = 'B' matrice1 [10][10] = 'B' matrice1 [10][11] = '0' matrice1 [10][12] = 'B' matrice1 [10][13] = 'B' matrice1 [10][14] = '0' matrice1 [10][15] = 'B' matrice1 [10][16] = '0' matrice1 [10][17] = 'B' matrice1 [10][18] = '0' matrice1 [10][19] = 'B' matrice1 [11][0] = 'B' matrice1 [11][1] = 'B' matrice1 [11][2] = 'B' matrice1 [11][3] = '0' matrice1 [11][4] = '0' matrice1 [11][5] = '0' matrice1 [11][6] = 'B' matrice1 [11][7] = '0' matrice1 [11][8] = '0' matrice1 [11][9] = '0' matrice1 [11][10] = 'B' matrice1 [11][11] = '0' matrice1 [11][12] = '0' matrice1 [11][13] = 'B' matrice1 [11][14] = '0' matrice1 [11][15] = '0' matrice1 [11][16] = '0' matrice1 [11][17] = 'B' matrice1 [11][18] = '0' matrice1 [11][19] = 'B' matrice1 [12][0] = 'B' matrice1 [12][1] = '0' matrice1 [12][2] = '0' matrice1 [12][3] = '0' matrice1 [12][4] = 'B' matrice1 [12][5] = '0' matrice1 [12][6] = 'B' matrice1 [12][7] = 'B' matrice1 [12][8] = 'B' matrice1 [12][9] = '0' matrice1 [12][10] = 'B' matrice1 [12][11] = 'B' matrice1 [12][12] = '0' matrice1 [12][13] = 'B' matrice1 [12][14] = 'B' matrice1 [12][15] = 'B' matrice1 [12][16] = 'B' matrice1 [12][17] = 'B' matrice1 [12][18] = '0' matrice1 [12][19] = 'B' matrice1 [13][0] = 'B' matrice1 [13][1] = '0' matrice1 [13][2] = 'B' matrice1 [13][3] = 'B' matrice1 [13][4] = 'B' matrice1 [13][5] = '0' matrice1 [13][6] = '0' matrice1 [13][7] = '0' matrice1 [13][8] = 'B' matrice1 [13][9] = '0' matrice1 [13][10] = '0' matrice1 [13][11] = 'B' matrice1 [13][12] = '0' matrice1 [13][13] = '0' matrice1 [13][14] = '0' matrice1 [13][15] = '0' matrice1 [13][16] = '0' matrice1 [13][17] = '0' matrice1 [13][18] = '0' matrice1 [13][19] = 'B' matrice1 [14][0] = 'B' matrice1 [14][1] = '0' matrice1 [14][2] = 'B' matrice1 [14][3] = '2' matrice1 [14][4] = 'B' matrice1 [14][5] = 'B' matrice1 [14][6] = 'B' matrice1 [14][7] = '0' matrice1 [14][8] = 'B' matrice1 [14][9] = 'B' matrice1 [14][10] = '0' matrice1 [14][11] = 'B' matrice1 [14][12] = 'B' matrice1 [14][13] = 'B' matrice1 [14][14] = '0' matrice1 [14][15] = 'B' matrice1 [14][16] = 'B' matrice1 [14][17] = 'B' matrice1 [14][18] = 'B' matrice1 [14][19] = 'B' matrice1 [15][0] = 'B' matrice1 [15][1] = '0' matrice1 [15][2] = 'B' matrice1 [15][3] = '0' matrice1 [15][4] = 'B' matrice1 [15][5] = '0' matrice1 [15][6] = 'B' matrice1 [15][7] = '0' matrice1 [15][8] = 'B' matrice1 [15][9] = 'B' matrice1 [15][10] = '0' matrice1 [15][11] = '0' matrice1 [15][12] = '0' matrice1 [15][13] = 'B' matrice1 [15][14] = '0' matrice1 [15][15] = 'B' matrice1 [15][16] = '0' matrice1 [15][17] = '0' matrice1 [15][18] = '0' matrice1 [15][19] = '3' matrice1 [16][0] = 'B' matrice1 [16][1] = '0' matrice1 [16][2] = '0' matrice1 [16][3] = '0' matrice1 [16][4] = 'B' matrice1 [16][5] = '0' matrice1 [16][6] = '0' matrice1 [16][7] = '0' matrice1 [16][8] = '0' matrice1 [16][9] = 'B' matrice1 [16][10] = 'B' matrice1 [16][11] = 'B' matrice1 [16][12] = '0' matrice1 [16][13] = 'B' matrice1 [16][14] = '0' matrice1 [16][15] = '0' matrice1 [16][16] = '0' matrice1 [16][17] = 'B' matrice1 [16][18] = 'B' matrice1 [16][19] = 'B' matrice1 [17][0] = 'B' matrice1 [17][1] = 'B' matrice1 [17][2] = 'B' matrice1 [17][3] = 'B' matrice1 [17][4] = 'B' matrice1 [17][5] = 'B' matrice1 [17][6] = 'B' matrice1 [17][7] = 'B' matrice1 [17][8] = 'B' matrice1 [17][9] = 'B' matrice1 [17][10] = 'B' matrice1 [17][11] = 'B' matrice1 [17][12] = '3' matrice1 [17][13] = 'B' matrice1 [17][14] = 'B' matrice1 [17][15] = 'B' matrice1 [17][16] = 'B' matrice1 [17][17] = 'B' matrice1 [17][18] = 'B' matrice1 [17][19] = 'B' #On imprime la matrice afin de vérifier son contenu ( optionnel et non nécessaire ) impression(matrice1, larg, haut) #On définit la variable "laby" comme étant une fenêtre d'affichage laby = Matrice() laby.fenetre() #On affiche maintenant le labyrinthe grâce à la fonction "matrice" laby.affichage_case(matrice1) #On définit la variable "perso1" comme étant un Personnage perso1 = Personnage() #On affiche le personnage dans le labyrinthe perso1.affichage(laby) #On attribue les touches de déplacement aux fonctions correspondantes laby.can.focus_set() laby.can.bind('',perso1.deplacement_haut) laby.can.bind('',perso1.deplacement_gauche) laby.can.bind('',perso1.deplacement_bas) laby.can.bind('',perso1.deplacement_droite) laby.can.bind('',perso1.relachement_touche) laby.can.bind('',perso1.relachement_touche) laby.can.bind('',perso1.relachement_touche) laby.can.bind('',perso1.relachement_touche) laby.main_window.mainloop()