307 lines
11 KiB
Python
307 lines
11 KiB
Python
|
from tkinter import *
|
||
|
|
||
|
class PlateauTk:
|
||
|
|
||
|
DECX = 0
|
||
|
DECY = 0
|
||
|
COTE_CASE = 50
|
||
|
MARGE_PIONS = 5
|
||
|
TEMPS_ANIM = 200
|
||
|
INTER_ANIM = 10
|
||
|
|
||
|
def __init__(self, logique):
|
||
|
|
||
|
self.fen = None
|
||
|
self.can = None
|
||
|
self.chaine = None
|
||
|
self.grilleDamier = []
|
||
|
self.imagesOriginales = []
|
||
|
self.imagesRedim = []
|
||
|
self.photos = []
|
||
|
self.grillePions = []
|
||
|
self.animations = []
|
||
|
|
||
|
self.dEtape = True
|
||
|
self.dx1 = -1
|
||
|
self.dy1 = -1
|
||
|
self.dx2 = -1
|
||
|
self.dy2 = -1
|
||
|
self.mvtsPossibles = []
|
||
|
self.logique = logique
|
||
|
|
||
|
self.creerFen()
|
||
|
self.importerImages()
|
||
|
self.redimImages()
|
||
|
|
||
|
self.cDamier()
|
||
|
self.cGrille()
|
||
|
self.remplirGrille(self.logique.grille)
|
||
|
|
||
|
self.fen.mainloop()
|
||
|
|
||
|
def creerFen(self):
|
||
|
self.fen = Tk()
|
||
|
self.fen.title("Jeu d'Échecs")
|
||
|
self.can = Canvas(self.fen, width=PlateauTk.COTE_CASE * self.logique.CASES_COTE,
|
||
|
height=PlateauTk.COTE_CASE * self.logique.CASES_COTE, bg="ivory")
|
||
|
self.can.grid(row=0, column=1, columnspan=3)
|
||
|
self.can.bind('<Button-1>', self.clic)
|
||
|
self.chaine = Label(self.fen, text="Aux blancs")
|
||
|
self.chaine.grid(row=2, column=2, padx=3, pady=3)
|
||
|
# Button(self.fen, text="Nv. Partie", command=f_nvPartie).grid(row=2, \
|
||
|
# column=1, padx=3, pady=3)
|
||
|
Button(self.fen, text="Quitter", command=self.fen.destroy).grid(row=2,
|
||
|
column=3, padx=3, pady=3)
|
||
|
|
||
|
|
||
|
def statut(self, texte, delai=0):
|
||
|
self.chaine.config(text=texte)
|
||
|
# TODO Timeout effacer si parametre / Liste
|
||
|
|
||
|
def importerImages(self):
|
||
|
for piece in range(0, 21):
|
||
|
nom = 'sprites/'
|
||
|
if piece % 10 == 1:
|
||
|
nom += 'pion'
|
||
|
elif piece % 10 == 2:
|
||
|
nom += 'tour'
|
||
|
elif piece % 10 == 3:
|
||
|
nom += 'cavalier'
|
||
|
elif piece % 10 == 4:
|
||
|
nom += 'fou'
|
||
|
elif piece % 10 == 5:
|
||
|
nom += 'dame'
|
||
|
elif piece % 10 == 6:
|
||
|
nom += 'roi'
|
||
|
else:
|
||
|
self.imagesOriginales.append('')
|
||
|
continue
|
||
|
if piece < 10:
|
||
|
nom += 'B'
|
||
|
else:
|
||
|
nom += 'N'
|
||
|
nom += '.gif'
|
||
|
self.imagesOriginales.append(PhotoImage(file=nom))
|
||
|
|
||
|
def redimImages(self):
|
||
|
sample = int(504 / (PlateauTk.COTE_CASE - PlateauTk.MARGE_PIONS))
|
||
|
for piece in range(0, 21):
|
||
|
if self.imagesOriginales[piece] != '':
|
||
|
self.imagesRedim.append(self.imagesOriginales[piece].
|
||
|
subsample(sample))
|
||
|
else:
|
||
|
self.imagesRedim.append('')
|
||
|
|
||
|
# Dessin
|
||
|
@staticmethod
|
||
|
def caseCouleur(blanc, contexte):
|
||
|
if contexte == 1: # Sélectionné
|
||
|
return '#a0cefe' if blanc else '#478bd1'
|
||
|
elif contexte == 2: # Possible
|
||
|
return '#bafea0' if blanc else '#6ed147'
|
||
|
elif contexte == 3: # Impossible
|
||
|
return '#fea0ab' if blanc else '#d14758'
|
||
|
else: # Normal
|
||
|
return '#ffce9e' if blanc else '#d18b47'
|
||
|
|
||
|
def cCase(self, x, y):
|
||
|
couleur = self.caseCouleur(not self.logique.eNoir(x, y), 0)
|
||
|
return self.can.create_rectangle(x * PlateauTk.COTE_CASE, y * PlateauTk.COTE_CASE, (x + 1) * PlateauTk.COTE_CASE, (y + 1) * PlateauTk.COTE_CASE)
|
||
|
|
||
|
def coulCase(self, x, y, contexte):
|
||
|
couleur = self.caseCouleur(not self.logique.eNoir(x, y), contexte)
|
||
|
self.can.itemconfig(self.grilleDamier[x][y], fill=couleur, outline=couleur)
|
||
|
|
||
|
def coulDamier(self):
|
||
|
for x in range(0, self.logique.CASES_COTE):
|
||
|
for y in range(0, self.logique.CASES_COTE):
|
||
|
self.coulCase(x, y, 0)
|
||
|
|
||
|
def cDamier(self):
|
||
|
self.grilleDamier = []
|
||
|
for x in range(0, self.logique.CASES_COTE):
|
||
|
colonne = []
|
||
|
for y in range(0, self.logique.CASES_COTE):
|
||
|
colonne.append(self.cCase(x + PlateauTk.DECX, y + PlateauTk.DECY))
|
||
|
self.grilleDamier.append(colonne)
|
||
|
self.coulDamier()
|
||
|
|
||
|
def cPion(self, x, y, piece):
|
||
|
if piece > 0:
|
||
|
self.grillePions[x][y] = self.can.create_image((x + .5) * PlateauTk.COTE_CASE,
|
||
|
(y + .5) * PlateauTk.COTE_CASE, image=self.imagesRedim[piece])
|
||
|
else:
|
||
|
self.grillePions[x][y] = False
|
||
|
|
||
|
def cGrille(self):
|
||
|
self.grillePions = []
|
||
|
for x in range(0, self.logique.CASES_COTE): # Crée self.grillePions
|
||
|
colonne = []
|
||
|
for y in range(0, self.logique.CASES_COTE):
|
||
|
colonne.append(False)
|
||
|
self.grillePions.append(colonne)
|
||
|
|
||
|
def remplirGrille(self, j_grilleF):
|
||
|
for x in range(0, self.logique.CASES_COTE): # Remplis self.grillePions
|
||
|
for y in range(0, self.logique.CASES_COTE):
|
||
|
self.cPion(x, y, j_grilleF[x][y])
|
||
|
|
||
|
# Interaction
|
||
|
@staticmethod
|
||
|
def nomJoueur(joueur, pluriel=True):
|
||
|
if joueur:
|
||
|
nom = 'blanc'
|
||
|
else:
|
||
|
nom = 'noir'
|
||
|
if pluriel:
|
||
|
nom += 's'
|
||
|
return nom
|
||
|
|
||
|
def statutPrendre(self):
|
||
|
self.statut('Prendre (' + self.nomJoueur(self.logique.joueur) + ')')
|
||
|
|
||
|
@staticmethod
|
||
|
def animationDCoords(i):
|
||
|
x = i['x1'] + (i['x2']-i['x1']) * (i['avancement']/i['total'])
|
||
|
y = i['y1'] + (i['y2']-i['y1']) * (i['avancement']/i['total'])
|
||
|
return [x, y]
|
||
|
|
||
|
def animation(self):
|
||
|
animationsNv = []
|
||
|
for i in self.animations:
|
||
|
if i['avancement'] < i['total']:
|
||
|
if i['type'] == 'd':
|
||
|
coords = self.animationDCoords(i)
|
||
|
self.can.coords(i['pion'], coords[0], coords[1])
|
||
|
# elif i['type'] == 'f':
|
||
|
# TODO Opacité de i['pion']
|
||
|
# elif i['type'] == 'c':
|
||
|
# TODO Opacité de case
|
||
|
i['avancement'] += PlateauTk.INTER_ANIM
|
||
|
animationsNv.append(i)
|
||
|
else:
|
||
|
if i['type'] == 'd':
|
||
|
self.can.coords(i['pion'], i['x2'], i['y2'])
|
||
|
elif i['type'] == 'f':
|
||
|
self.can.delete(i['pion'])
|
||
|
elif i['type'] == 'c':
|
||
|
self.coulCase(i['x'], i['y'], 0)
|
||
|
self.animations = animationsNv
|
||
|
if len(animationsNv):
|
||
|
self.fen.after(PlateauTk.INTER_ANIM, self.animation)
|
||
|
|
||
|
def animer(self, animation):
|
||
|
etaitVide = len(self.animations) < 1
|
||
|
self.animations.append(animation)
|
||
|
if etaitVide:
|
||
|
self.animation()
|
||
|
|
||
|
def animerD(self, x1, y1, x2, y2, pion):
|
||
|
if len(self.animations):
|
||
|
for i in self.animations:
|
||
|
if i['pion'] == pion: # Si une animation pour ce pion existe
|
||
|
# déjà, on la reprend et on la modifie
|
||
|
coords = self.animationDCoords(i)
|
||
|
i['x1'] = coords[0]
|
||
|
i['y1'] = coords[1]
|
||
|
i['x2'] = x2
|
||
|
i['y2'] = y2
|
||
|
# i['total'] = i['total'] - i['avancement']
|
||
|
i['total'] = PlateauTk.TEMPS_ANIM
|
||
|
i['avancement'] = 0
|
||
|
return
|
||
|
|
||
|
animation = {
|
||
|
'x1': x1,
|
||
|
'y1': y1,
|
||
|
'x2': x2,
|
||
|
'y2': y2,
|
||
|
'pion': pion,
|
||
|
'type': 'd',
|
||
|
'total': PlateauTk.TEMPS_ANIM,
|
||
|
'avancement': 0
|
||
|
}
|
||
|
self.can.tag_raise(pion) # Mise au premier plan
|
||
|
self.animer(animation)
|
||
|
|
||
|
def animerF(self, pion): # Pion fade-out
|
||
|
animation = {
|
||
|
'pion': pion,
|
||
|
'type': 'f',
|
||
|
'total': PlateauTk.TEMPS_ANIM,
|
||
|
'avancement': 0
|
||
|
}
|
||
|
self.animer(animation)
|
||
|
|
||
|
def animerC(self, x ,y):
|
||
|
animation = {
|
||
|
'type': 'c',
|
||
|
'x': x,
|
||
|
'y': y,
|
||
|
'total': PlateauTk.TEMPS_ANIM,
|
||
|
'avancement': 0
|
||
|
}
|
||
|
self.animer(animation)
|
||
|
|
||
|
def victoire(self):
|
||
|
self.statut('Victoire des ' + self.nomJoueur(self.logique.victorieux) + ' !')
|
||
|
self.coulDamier()
|
||
|
for x in range(0, self.logique.CASES_COTE):
|
||
|
for y in range(0, self.logique.CASES_COTE):
|
||
|
pion = self.logique.grille[x][y]
|
||
|
if pion > 0:
|
||
|
if self.logique.ePionNoir(pion) ^ self.logique.victorieux:
|
||
|
self.coulCase(x, y, 2)
|
||
|
else:
|
||
|
self.coulCase(x, y, 3)
|
||
|
|
||
|
def dPion(self, x1, y1, x2, y2):
|
||
|
test = self.logique.dPion(x1, y1, x2, y2)
|
||
|
if test['valide'] == True: # Si déplacement possible
|
||
|
for s in test['supprimer']:
|
||
|
self.animerF(self.grillePions[s[0]][s[1]])
|
||
|
for d in test['deplacer']:
|
||
|
self.grillePions[d[2]][d[3]], self.grillePions[d[0]][d[1]] = \
|
||
|
self.grillePions[d[0]][d[1]], False
|
||
|
self.animerD((d[0] + .5) * PlateauTk.COTE_CASE, (d[1] + .5) * PlateauTk.COTE_CASE, \
|
||
|
(d[2] + .5) * PlateauTk.COTE_CASE, (d[3] + .5) * PlateauTk.COTE_CASE, \
|
||
|
self.grillePions[d[2]][d[3]])
|
||
|
|
||
|
else:
|
||
|
self.statut('Déplacment impossible ! (' + test['message'] + ')')
|
||
|
return test['valide']
|
||
|
|
||
|
def dClic(self, x, y):
|
||
|
if not self.logique.partieFinie:
|
||
|
if self.dEtape: # Prendre
|
||
|
self.dx1, self.dy1 = x, y
|
||
|
self.coulDamier() # Effacement des surbrillances
|
||
|
if self.logique.aSonTour(self.logique.grille[self.dx1][self.dy1]): # Si possible jouer
|
||
|
self.coulCase(self.dx1, self.dy1, 1)
|
||
|
self.mvtPossibleSansEchecs = self.logique.mvtsPossibles(self.dx1, self.dy1) # Surbrillance bleue
|
||
|
for i in self.mvtPossibleSansEchecs: # Surbrillances vertes
|
||
|
self.coulCase(i[0], i[1], 2)
|
||
|
self.statut('Poser')
|
||
|
self.dEtape = not self.dEtape
|
||
|
else: # Si pas pssible jouer
|
||
|
self.coulCase(self.dx1, self.dy1, 3)
|
||
|
self.animerC(self.dx1, self.dy1)
|
||
|
else: # Poser
|
||
|
self.dx2, self.dy2 = x, y
|
||
|
if self.dPion(self.dx1, self.dy1, self.dx2, self.dy2) or (self.dx1 == self.dx2 and self.dy1 == self.dy2): # Si déplacement fait / Annule dépalcement
|
||
|
self.coulDamier() # Effacer Surbrillance
|
||
|
self.dEtape = not self.dEtape
|
||
|
if self.logique.partieFinie:
|
||
|
self.victoire()
|
||
|
else:
|
||
|
self.statutPrendre()
|
||
|
else: # Si mauvais déplacement
|
||
|
self.coulCase(self.dx2, self.dy2, 3)
|
||
|
self.animerC(self.dx2, self.dy2)
|
||
|
|
||
|
def clic(self, event):
|
||
|
x = event.x // PlateauTk.COTE_CASE
|
||
|
y = event.y // PlateauTk.COTE_CASE
|
||
|
self.dClic(x, y)
|
||
|
|
||
|
# TODO Un jeu (canvas) et un frontend (fenetre)
|