1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| # -*- coding: utf-8 -*-
#APPEL DU MODULE TKINTER
from Tkinter import *
#INSTANCIATION DE L'OBJET CANVAS PAR LA CLASSE CAN
class Can(Canvas):
def __init__(self):
#ARGUMENTS VARIABLES (GAMEPLAY)
self.cases = [] #CASES DEJA COLORIEES
self.listebordeau = [] #LISTE DES CASES BORDEAUX
self.listeturquoise = [] #LISTE DES CASES TURQUOISES
self.running = 1 #PARTIE EN COURS
self.couleur = ["firebrick", "light sea green"]
self.color = ["firebrick", "light sea green"]
#ARGUMENTS D'INTERFACE (GUI)
self.clair = "grey"
self.fonce = "black"
self.police = "Neuropol 10"
self.police1 = "Neuropol 14 bold"
self.can = Canvas.__init__(self, width =446, height = 430, bg=self.fonce, bd=0)
self.grid(row=1, columnspan=5)
self.textcolor = "black"
self.textcolor1 = "light sea green"
self.textcolor2 = "firebrick"
#COULEUR LIEE A CHAQUE DES JOUEURS
self.joueur = 1
self.create_text(10, 400, text ="JOUEUR 1", anchor = NW, fill = self.textcolor1, font= self.police1)
self.create_text(10, 412, text ="JOUEUR 2", anchor = NW, fill = self.textcolor2, font= self.police1)
#BOUTON RECOMMENCER
self.create_rectangle(335,400,440,425,fill=self.clair)
self.create_text(340, 405, text =" RECOMMENCER", anchor = NW, fill = self.textcolor, font= self.police)
#CASES
self.rectangle = []
for y in range(10, 390, 55):
for x in range(10, 437, 63):
self.rectangle.append(self.create_rectangle(x, y, x + 50, y + 50 , fill= "light grey"))
#ASSIGNATION DE L'EVENEMENT LIE AU CLICK GAUCHE DE LA SOURIS
self.bind("<Button-1>", self.click)
#ARGUMENT ASSIGNANT LES COORDONNEES DES CASES AUX CENTRES DE CELLES CI
self.coordscentres = []
#DICTIONNAIRE DE RECONNAISSANCE
self.dictionnaire = {}
v = 0
for y in range(10, 390, 55):
for x in range(10, 437, 63):
self.dictionnaire[(x, y, x + 50, y + 50)] = v
v += 1
self.coordscentres.append((x + 25, y + 25))
#LORS D'UN CLICK
#RECOMMENCER
def click(self,event):
if 330 < event.x and 400 < event.y and event.x < 420 and event.y < 425:
self.new()
#COLORIER UNE CASE
else :
if self.running != 0:
for (w, x, y, z) in self.dictionnaire:
if event.x > (w, x, y, z)[0] and event.y >(w, x, y, z)[1] and event.x < (w, x, y, z)[2] and event.y < (w, x, y, z)[3]:
self.colorier(self.dictionnaire[(w, x, y, z)])
#METHODE PERMETTANT LA COLORATION D'UNE CASE
def colorier(self, n, nb=0):
#UNE CASE DEJA COLORIEE RESTE TELLE QUELLE
if n in self.cases : return
#ON COLORIE LA CASE LA PLUS BASSE
if n + 7 not in self.cases and n + 7 < 49:
self.colorier(n+7)
#SINON ON COLORIE CELLE CI
else:
self.itemconfigure(self.rectangle[n], fill = self.color[self.joueur])
self.cases.append(n)
self.color[self.joueur] == 'firebrick' and self.listebordeau.append(n) or self.listeturquoise.append(n)
self.listeturquoise = [case for case in self.listeturquoise if case not in self.listebordeau]
self.verif(n)
#ALTERNANCE DE JOUEUR
self.joueur = [0,1][[0,1].index(self.joueur)-1]
#VERIFICATION D'ALIGNEMENT
def verif(self, n):
if self.running == 0 : return
#D'ABORD POUR LES JETONS BORDEAUX
#A LA VERTICALE
if n in self.listebordeau and n+7 in self.listebordeau and n+14 in self.listebordeau and n+21 in self.listebordeau:
liste=[n, n+7, n+14, n+21]
if self.gagnantes(liste) : self.win("firebrick", liste[0],liste[3])
return
#A L'HORIZONTALE ET EN DIAGONALE
for x in (1,-6,8):
if n in self.listebordeau and n+x in self.listebordeau and n+x*2 in self.listebordeau and n+x*3 in self.listebordeau:
liste=[n, n+x, n+x*2, n+x*3]
if self.gagnantes(liste) : self.win("firebrick", liste[0],liste[3])
return
for x in (-1,6,-8):
if n in self.listebordeau and n+x in self.listebordeau and n+x*2 in self.listebordeau and n+x*3 in self.listebordeau:
liste=[n, n+x, n+x*2, n+x*3]
if self.gagnantes(liste) : self.win("firebrick", liste[0],liste[3])
return
#PUIS POUR LES JETONS TURQUOISES
#A LA VERTICALE
if n in self.listeturquoise and n+7 in self.listeturquoise and n+14 in self.listeturquoise and n+21 in self.listeturquoise:
liste=[n, n+7, n+14, n+21]
if self.gagnantes(liste) : self.win("light sea green", liste[0],liste[3])
return
#A L'HORIZONTALE ET EN DIAGONALE
for x in (1,-6,8):
if n in self.listeturquoise and n+x in self.listeturquoise and n+x*2 in self.listeturquoise and n+x*3 in self.listeturquoise:
liste=[n, n+x, n+x*2, n+x*3]
if self.gagnantes(liste) : self.win("light sea green", liste[0],liste[3])
return
for x in (-1,6,-8):
if n in self.listeturquoise and n+x in self.listeturquoise and n+x*2 in self.listeturquoise and n+x*3 in self.listeturquoise:
liste=[n, n+x, n+x*2, n+x*3]
if self.gagnantes(liste) : self.win("light sea green", liste[0],liste[3])
return
#METHODE DEFINISSANT UNE PARTIE GAGNÉE
def win(self, qui, p, d):
#ON TRACE UNE LIGNE LORSQUE QUATRE JETONS SONT ALIGNES
self.create_line(self.coordscentres[p][0], self.coordscentres[p][1],
self.coordscentres[d][0], self.coordscentres[d][1],
fill="goldenrod2", width="7")
#METHODE AJOUTANT LES JETONS GAGNANTS DANS UNE LISTE
def gagnantes(self, liste=[]):
return 1
#METHODE PERMETTANT DE RECOMMENCER
def new(self):
#ARGUMENTS DE LANCEMENT DU JEU
self.destroy()
self.__init__()
if __name__ == "__main__" :
fen = Tk()
fen.title("Cyberpuissance 4 project bac")
fen.config(bg="grey")
lecan = Can()
fen.mainloop() |
Partager