From 55680ad03269f981cbfde1ccea92a414caf1e557 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Sat, 6 Dec 2014 21:52:56 +0100 Subject: [PATCH] [Echecs] Plus d'animations et modifications diverses --- S1/Echecs/echecs.py | 214 +++++++++++++++++++++++++++++++++----------- 1 file changed, 161 insertions(+), 53 deletions(-) diff --git a/S1/Echecs/echecs.py b/S1/Echecs/echecs.py index 705d137..6a4ce56 100755 --- a/S1/Echecs/echecs.py +++ b/S1/Echecs/echecs.py @@ -49,48 +49,88 @@ class LogiqueEchecs: self.grille[x][y] = piece return True + @staticmethod + def ePionBlanc(pion): + return pion in range(1, 7) + + @staticmethod + def ePionNoir(pion): + return pion in range(11, 17) + + + def aSonTour(self, pion): + return (self.ePionNoir(pion) and self.joueur == False) or \ + (self.ePionBlanc(pion) and self.joueur == True) + def mvtPossible(self, x1, y1, x2, y2): pion = self.grille[x1][y1] - if (pion >= 10 and self.joueur == False) or \ - (pion < 10 and self.joueur == True): - if pion > 0 and self.grille[x2][y2] <= 0: + if self.aSonTour(pion): + if pion > 0: tPion = pion % 10 - if tPion == 1: - if x1 == x2: + if tPion == 1: # Pion + if x1 == x2 and self.grille[x2][y2] <= 0: # Avance if self.joueur: if y2 == y1 - 1: - return True + return [None, True] + elif y1 == 6 and y2 == 4: + return [None, True] else: return -4 else: if y2 == y1 + 1: - return True + return [None, True] + elif y1 == 1 and y2 == 3: + return [None, True] else: return -4 + elif abs(x1-x2) == 1: # Saut + if self.joueur: + if y2 == y1 - 1 and \ + self.ePionNoir(self.grille[x2][y2]): + return [[x2, y2], True] # Saute un noir + else: + return -4 + else: + if y2 == y1 + 1 and \ + self.ePionBlanc(self.grille[x2][y2]): + return [[x2, y2], True] # Saute un blanc + else: + return -4 + else: return -4 elif tPion == 6: - if x2 <= x1 + 1 and x2 >= x1 - 1 and y2 <= y1 + 1 and y2 >= y1 - 1 \ + if x2 <= x1 + 1 and x2 >= x1 - 1 and y2 <= y1 + 1 and \ + y2 >= y1 - 1 \ and (x1 != x2 or y1 != y2): - return True + return [None, True] else: return -4 else: return -3 - return True + return [None, True] else: return -2 else: return -1 + def mvtsPossibles(self, x1, y1): + tableau = [] + for x2 in range(0, CASES_COTE): + for y2 in range(0, CASES_COTE): + if type(self.mvtPossible(x1, y1, x2, y2)) is list: + tableau.append([x2, y2]) + return tableau + def dPion(self, x1, y1, x2, y2): test = self.mvtPossible(x1, y1, x2, y2) - if test == True: + if type(test) is list: self.grille[x2][y2] = self.grille[x1][y1] self.grille[x1][y1] = 0 - # self.joueur = not self.joueur # DEBUG - return True + if test[1]: + self.joueur = not self.joueur + return test else: return test @@ -124,6 +164,7 @@ class PlateauTk: self.dy1 = -1 self.dx2 = -1 self.dy2 = -1 + self.mvtsPossibles = [] self.logique = LogiqueEchecs() self.creerFen() @@ -187,13 +228,29 @@ class PlateauTk: 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): - if LogiqueEchecs.eNoir(x, y): - couleur = '#D18B47' - else: - couleur = '#FFCE9E' - return self.can.create_rectangle(x * COTE_CASE, y * COTE_CASE, - (x + 1) * COTE_CASE, (y + 1) * COTE_CASE, fill=couleur) + couleur = self.caseCouleur(not LogiqueEchecs.eNoir(x, y), 0) + return self.can.create_rectangle(x * COTE_CASE, y * COTE_CASE, (x + 1) * COTE_CASE, (y + 1) * 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, CASES_COTE): + for y in range(0, CASES_COTE): + self.coulCase(x, y, 0) def cDamier(self): self.grilleDamier = [] @@ -202,6 +259,7 @@ class PlateauTk: for y in range(0, CASES_COTE): colonne.append(self.cCase(x + DECX, y + DECY)) self.grilleDamier.append(colonne) + self.coulDamier() def cPion(self, x, y, piece): if piece > 0: @@ -232,44 +290,47 @@ class PlateauTk: self.statut('Prendre (' + joueur + ')') @staticmethod - def dPionAnimCoords(i): + 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 dPionAnim(self): + def animation(self): animationsNv = [] for i in self.animations: if i['avancement'] < i['total']: - coords = self.dPionAnimCoords(i) - self.can.coords(i['pion'], coords[0], coords[1]) + 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'] += INTER_ANIM animationsNv.append(i) else: - self.can.coords(i['pion'], i['x2'], i['y2']) + 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(INTER_ANIM, self.dPionAnim) + self.fen.after(INTER_ANIM, self.animation) - def cdPionAnim(self, x1, y1, x2, y2, pion): - animation = { - 'x1': x1, - 'y1': y1, - 'x2': x2, - 'y2': y2, - 'pion': pion, - 'total': TEMPS_ANIM, - 'avancement': 0 - } + def animer(self, animation): + etaitVide = len(self.animations) < 1 + self.animations.append(animation) + if etaitVide: + self.animation() - if not len(self.animations): - self.animations.append(animation) - self.dPionAnim() - else: + 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.dPionAnimCoords(i) + coords = self.animationDCoords(i) i['x1'] = coords[0] i['y1'] = coords[1] i['x2'] = x2 @@ -278,17 +339,50 @@ class PlateauTk: i['total'] = TEMPS_ANIM i['avancement'] = 0 return - self.animations.append(animation) + + animation = { + 'x1': x1, + 'y1': y1, + 'x2': x2, + 'y2': y2, + 'pion': pion, + 'type': 'd', + 'total': 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': TEMPS_ANIM, + 'avancement': 0 + } + self.animer(animation) + + def animerC(self, x ,y): + animation = { + 'type': 'c', + 'x': x, + 'y': y, + 'total': TEMPS_ANIM, + 'avancement': 0 + } + self.animer(animation) def dPion(self, x1, y1, x2, y2): test = self.logique.dPion(x1, y1, x2, y2) - if test == True: + if type(test) is list: # Si déplacement possible + if type(test[0]) is list: # Si saut → Animation effacement + self.animerF(self.grillePions[test[0][0]][test[0][1]]) self.grillePions[x2][y2], self.grillePions[x1][y1] = \ - self.grillePions[x1][y1], False - self.cdPionAnim((x1 + .5) * COTE_CASE, (y1 + .5) * COTE_CASE, \ - (x2 + .5) * COTE_CASE, (y2 + .5) * COTE_CASE, \ - self.grillePions[x2][y2]) + self.grillePions[x1][y1], False + self.animerD((x1 + .5) * COTE_CASE, (y1 + .5) * COTE_CASE, \ + (x2 + .5) * COTE_CASE, (y2 + .5) * COTE_CASE, \ + self.grillePions[x2][y2]) return True else: # TODO Messages corrects @@ -297,15 +391,29 @@ class PlateauTk: def dClic(self, x, y): # if not len(self.animations): - if self.dEtape: + if self.dEtape: # Prendre self.dx1, self.dy1 = x, y - # TODO Colorer case - self.statut('Poser') - else: + 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.mvtPossibles = self.logique.mvtsPossibles(self.dx1, self.dy1) # Surbrillance bleue + for i in self.mvtPossibles: # 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 - self.dPion(self.dx1, self.dy1, self.dx2, self.dy2) - self.statutPrendre() - self.dEtape = not self.dEtape + 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 + 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 // COTE_CASE