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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
from tkinter import *
from tkinter import ttk # si on fait pas cet import ca marche pas..pourquoi?
from copy import deepcopy
# création de la grille de jeu
def create_board():
global listPos
decalLig=-cellSize
for i in range(nbCase):
decalLig+=cellSize
decalCol=0
listTemp=[]
for j in range(nbCase):
x1, y1 = 3+decalCol, 3+decalLig
x2, y2 = x1+cellSize, y1+cellSize
case = can.create_rectangle(x1, y1, x2, y2, fill='green',
width="2", tag="Case")
decalCol+=cellSize
# ajouter les id de cases dans une matrice (liste de listes).
listTemp.append(case)
listPos.append(listTemp)
# on place les 4 pions centraux en s'appuyant sur la coord du centre de la
# grille qui est invariable (car la taille du canevas est invariable)
def starting_pawns():
global listPos
pionHG = can.create_oval(244-(cellSize*11/12), 244-(cellSize*11/12), 244-(cellSize*1/12), 244-(cellSize*1/12), fill='white', width="2", tag="Blanc")
pionHD = can.create_oval(244+(cellSize*1/12), 244-(cellSize*11/12), 244+(cellSize*11/12), 244-(cellSize*1/12), fill='black', width="2", tag="Noir")
pionBG = can.create_oval(244-(cellSize*11/12), 244+(cellSize*1/12), 244-(cellSize*1/12), 244+(cellSize*11/12), fill='black', width="2", tag="Noir")
pionBD = can.create_oval(244+(cellSize*1/12), 244+(cellSize*1/12), 244+(cellSize*11/12), 244+(cellSize*11/12), fill='white', width="2", tag="Blanc")
listPos[int(nbCase/2-1)][int(nbCase/2-1)],listPos[int(nbCase/2-1)][int(nbCase/2)],listPos[int(nbCase/2)][int(nbCase/2-1)],listPos[int(nbCase/2)][int(nbCase/2)] = "B", "N", "N", "B"
# repérage de la case cliquée et du tour
def click_case(event):
# event.x et event.y contiennent les coordonnées du clic effectué
x1, y1 = event.x, event.y
# <find_closest> renvoie la référence du dessin le plus proche
selObject = can.find_closest(x1, y1)
a1, b1, a2, b2 = can.coords(selObject)
# detection si il n'y a pas déja un jeton dessiné sur la case
if selObject[0] in can.find_withtag("Case"):
# relever a quelle adresse de matrice correspond cette case
cpt=0
for i in range(nbCase):
for j in range(nbCase):
cpt+=1
if cpt==selObject[0]:
if turnBlanc==True:
s="B"
else:
s="N"
checking(i,j,s, selObject, a1, b1, a2, b2 )
# trouver les pions à retourner autour de la case cliquée
def checking(coordi, coordj, symbol, selCase, casX1, casY1, casX2, casY2):
global listPos
# vérif colone vers le haut-------------------------------------------------
# on double les coordonnées reçues
matY=coordi
matX=coordj
# on "clone" la matrice pour pouvoir y faire les modifs
# que l'on appliquera à la matrice listPos en fin de procédure
toBeFlipped=deepcopy(listPos)
# on "clone" à nouveau cette matrice pour la comparer en fin de procédure
toBeCompared=deepcopy(listPos)
# création d'un indicateur pour appliquer ou non les changements
flipHem=False
while matY>0:
matY-=1
if (listPos[matY][matX]=="B" or listPos[matY][matX]=="N") and listPos[matY][matX]!= symbol:
toBeFlipped[matY][matX]=symbol
elif listPos[matY][matX]==symbol:
flipHem=True
break
else:
toBeFlipped=listPos
break
if flipHem==True:
listPos=toBeFlipped
else:
toBeFlipped=listPos
if toBeCompared != listPos:
put_the_pawns(selCase, casX1, casY1, casX2, casY2)
print(toBeCompared,"\n",listPos)
else:
prompt.config(text="case inactive")
# on dessine le pion sur la case, on update la matrice avec ce nouveau pion puis
# on change la couleur de tous les pions
def put_the_pawns(clickedCase, x1, y1, x2, y2):
global listPos
# effacer le prompt (au cas ou il a été activé avant ce coup)
prompt.config(text="")
# si c'est au tour blanc
if turnBlanc == True:
# creation du jeton blanc
can.create_oval(x1+(cellSize*1/12), y1+(cellSize*1/12),
x2-(cellSize*1/12), y2-(cellSize*1/12),
fill="white", width="2", tag="Blanc")
# puis on positionne un B dans la matrice à l'endroit du clic
cpt=0
for i in range(nbCase):
for j in range(nbCase):
cpt+=1
if cpt==clickedCase[0]:
listPos[i][j]="B"
# on recherche toutes les lettres B de la matrice
cpt=0
for i in range(nbCase):
for j in range(nbCase):
cpt+=1
if listPos[i][j] == "B":
a,b,c,d = can.coords(cpt)
jeton = can.find_enclosed(a,b,c,d)
can.itemconfigure(jeton[0], fill="white")
calc_score()
elif turnNoir == True:
can.create_oval(x1+(cellSize*1/12), y1+(cellSize*1/12),
x2-(cellSize*1/12), y2-(cellSize*1/12),
fill="black", width="2", tag="Noir")
cpt=0
for i in range(nbCase):
for j in range(nbCase):
cpt+=1
if cpt==clickedCase[0]:
listPos[i][j]="N"
cpt=0
for i in range(nbCase):
for j in range(nbCase):
cpt+=1
if listPos[i][j] == "N":
a,b,c,d=can.coords(cpt)
jeton = can.find_enclosed(a,b,c,d)
can.itemconfigure(jeton[0], fill="black")
calc_score()
# calcul des scores
def calc_score():
global scoreB, scoreN
scoreB=0
for i in listPos:
for j in i:
if j=="B":
scoreB+=1
scoreN=0
for i in listPos:
for j in i:
if j=="N":
scoreN+=1
change_player(scoreB, scoreN)
# changement de joueur et modif des labels et scores
def change_player(pointsB, pointsN):
global turnBlanc, turnNoir, color4Blanc, color4Noir, scoreBLab, scoreNlab
scoreBLab.config(text="Point Blanc :"+str(pointsB))
scoreNLab.config(text="Point Noir :"+str(pointsN))
if turnBlanc == True:
# changement des fonds des labels
color4Blanc = "light gray"
color4Noir = "gray"
scoreBLab.config(bg=color4Blanc)
scoreNLab.config(bg=color4Noir)
# changement de joueur
turnBlanc = False
turnNoir = True
elif turnNoir == True:
# changement des fonds des labels
color4Blanc = "white"
color4Noir = "light gray"
scoreBLab.config(bg=color4Blanc)
scoreNLab.config(bg=color4Noir)
# changement de joueur
turnBlanc = True
turnNoir = False
def reload_game():
global nbCase
"""fonction pour recharger le canvas avec la nouvelle variable
nbCase mais ca fonctionne pas (reste en 8 cases et click ne fonctionne plus
non plus) et j'arrive pas à trouver pourquoi"""
# effacer le canvas
can.delete('all')
# recupérer la nouvelle valeur de nombre case"
nbCase = int(comboCase.get())
# metre le prompt a jour (montre qu'on est bien entré dans cette fonction)
prompt.config(text="avec "+str(nbCase))
# appels des fonctions pour créer la nouvelle grille
create_board()
starting_pawns()
#-------------------------Programme principal----------------------------------#
# creation de la fenetre tkinter
fen = Tk()
fen.title("Oh! t'es Low'")
fen.config (bg = "light gray")
# initialisations
scoreB = 0
scoreN = 0
listPos=[]
turnBlanc = True
turnNoir = False
color4Blanc = "white"
color4Noir = "light gray"
# label score et prompt
scoreBLab = Label(fen, text="Point Blanc :"+str(scoreB), bg=color4Blanc)
prompt = Label(fen, text="", bg="light gray")
scoreNLab = Label(fen, text="Point Noir :"+str(scoreN), bg=color4Noir)
scoreBLab.grid(row=0, column=0)
prompt.grid(row=0, column=1)
scoreNLab.grid(row=0, column=2)
# creation du systeme d'onglets
setOnglets = ttk.Notebook(fen)
setOnglets.grid(row=1, columnspan=3)
# ajout des l'onglets
onglet1 = ttk.Frame(setOnglets)
onglet1.pack()
onglet2 = ttk.Frame(setOnglets)
onglet2.pack()
onglet3 = ttk.Frame(setOnglets)
onglet3.pack()
# nom des onglets
setOnglets.add(onglet1, text='Grille de jeu')
setOnglets.add(onglet2, text='Paramètres')
setOnglets.add(onglet3, text='Règles')
# creation du canevas dans l'onglet1
canH = 483
canW = 483
can = Canvas(onglet1, height=canH, width=canW, bg="ivory")
can.pack()
# liaison d'événements <souris> au widget <canevas> :
can.bind("<Button-1>", click_case)
# labelFrame de l'onglet2
labFram1 = LabelFrame(onglet2, text="nombre de case")
labFram1.pack(fill="both", expand="No", padx=10)
# placer le combox dans ce container
comboCase = ttk.Combobox(labFram1,
values=["6",
"8",
"10",
"12",
"14",
"16",
"18",
"20"])
comboCase.current(1) # choix [1] (i.e. "8") dans la liste
comboCase.pack(side=LEFT, pady=20 )
# definition du nombre de cases par ligne de la grille
nbCase = int(comboCase.get())
# on définit la taille des cases en fonction de la taille du canevas (moins 3
# pour voir les bordures) divisée par le nombre de cases
cellSize = (canH-3)/nbCase
#☺ création des bouton en bas de fenetre
btnQuit = Button(fen, text='Quitter', command=fen.destroy)
btnQuit.grid(row=2, column=0)
btnReload = Button(fen, text='Nouvelle partie', command=reload_game)
btnReload.grid(row=2, column=2)
# creation de la grille via fonctions
create_board()
starting_pawns()
# boucler la fenetre tkinter
fen.mainloop() |
Partager