[Echecs] Plus d'animations et modifications diverses

This commit is contained in:
Geoffrey Frogeye 2014-12-06 21:52:56 +01:00
parent 94ee3dbbff
commit 55680ad032

View file

@ -49,48 +49,88 @@ class LogiqueEchecs:
self.grille[x][y] = piece self.grille[x][y] = piece
return True 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): def mvtPossible(self, x1, y1, x2, y2):
pion = self.grille[x1][y1] pion = self.grille[x1][y1]
if (pion >= 10 and self.joueur == False) or \ if self.aSonTour(pion):
(pion < 10 and self.joueur == True): if pion > 0:
if pion > 0 and self.grille[x2][y2] <= 0:
tPion = pion % 10 tPion = pion % 10
if tPion == 1: if tPion == 1: # Pion
if x1 == x2: if x1 == x2 and self.grille[x2][y2] <= 0: # Avance
if self.joueur: if self.joueur:
if y2 == y1 - 1: if y2 == y1 - 1:
return True return [None, True]
elif y1 == 6 and y2 == 4:
return [None, True]
else: else:
return -4 return -4
else: else:
if y2 == y1 + 1: if y2 == y1 + 1:
return True return [None, True]
elif y1 == 1 and y2 == 3:
return [None, True]
else: else:
return -4 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: else:
return -4 return -4
elif tPion == 6: 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): and (x1 != x2 or y1 != y2):
return True return [None, True]
else: else:
return -4 return -4
else: else:
return -3 return -3
return True return [None, True]
else: else:
return -2 return -2
else: else:
return -1 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): def dPion(self, x1, y1, x2, y2):
test = self.mvtPossible(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[x2][y2] = self.grille[x1][y1]
self.grille[x1][y1] = 0 self.grille[x1][y1] = 0
# self.joueur = not self.joueur # DEBUG if test[1]:
return True self.joueur = not self.joueur
return test
else: else:
return test return test
@ -124,6 +164,7 @@ class PlateauTk:
self.dy1 = -1 self.dy1 = -1
self.dx2 = -1 self.dx2 = -1
self.dy2 = -1 self.dy2 = -1
self.mvtsPossibles = []
self.logique = LogiqueEchecs() self.logique = LogiqueEchecs()
self.creerFen() self.creerFen()
@ -187,13 +228,29 @@ class PlateauTk:
self.imagesRedim.append('') self.imagesRedim.append('')
# Dessin # 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): def cCase(self, x, y):
if LogiqueEchecs.eNoir(x, y): couleur = self.caseCouleur(not LogiqueEchecs.eNoir(x, y), 0)
couleur = '#D18B47' return self.can.create_rectangle(x * COTE_CASE, y * COTE_CASE, (x + 1) * COTE_CASE, (y + 1) * COTE_CASE)
else:
couleur = '#FFCE9E' def coulCase(self, x, y, contexte):
return self.can.create_rectangle(x * COTE_CASE, y * COTE_CASE, couleur = self.caseCouleur(not self.logique.eNoir(x, y), contexte)
(x + 1) * COTE_CASE, (y + 1) * COTE_CASE, fill=couleur) 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): def cDamier(self):
self.grilleDamier = [] self.grilleDamier = []
@ -202,6 +259,7 @@ class PlateauTk:
for y in range(0, CASES_COTE): for y in range(0, CASES_COTE):
colonne.append(self.cCase(x + DECX, y + DECY)) colonne.append(self.cCase(x + DECX, y + DECY))
self.grilleDamier.append(colonne) self.grilleDamier.append(colonne)
self.coulDamier()
def cPion(self, x, y, piece): def cPion(self, x, y, piece):
if piece > 0: if piece > 0:
@ -232,44 +290,47 @@ class PlateauTk:
self.statut('Prendre (' + joueur + ')') self.statut('Prendre (' + joueur + ')')
@staticmethod @staticmethod
def dPionAnimCoords(i): def animationDCoords(i):
x = i['x1'] + (i['x2']-i['x1']) * (i['avancement']/i['total']) x = i['x1'] + (i['x2']-i['x1']) * (i['avancement']/i['total'])
y = i['y1'] + (i['y2']-i['y1']) * (i['avancement']/i['total']) y = i['y1'] + (i['y2']-i['y1']) * (i['avancement']/i['total'])
return [x, y] return [x, y]
def dPionAnim(self): def animation(self):
animationsNv = [] animationsNv = []
for i in self.animations: for i in self.animations:
if i['avancement'] < i['total']: if i['avancement'] < i['total']:
coords = self.dPionAnimCoords(i) if i['type'] == 'd':
coords = self.animationDCoords(i)
self.can.coords(i['pion'], coords[0], coords[1]) 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 i['avancement'] += INTER_ANIM
animationsNv.append(i) animationsNv.append(i)
else: else:
if i['type'] == 'd':
self.can.coords(i['pion'], i['x2'], i['y2']) 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 self.animations = animationsNv
if len(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): def animer(self, animation):
animation = { etaitVide = len(self.animations) < 1
'x1': x1,
'y1': y1,
'x2': x2,
'y2': y2,
'pion': pion,
'total': TEMPS_ANIM,
'avancement': 0
}
if not len(self.animations):
self.animations.append(animation) self.animations.append(animation)
self.dPionAnim() if etaitVide:
else: self.animation()
def animerD(self, x1, y1, x2, y2, pion):
if len(self.animations):
for i in self.animations: for i in self.animations:
if i['pion'] == pion: # Si une animation pour ce pion existe if i['pion'] == pion: # Si une animation pour ce pion existe
# déjà, on la reprend et on la modifie # déjà, on la reprend et on la modifie
coords = self.dPionAnimCoords(i) coords = self.animationDCoords(i)
i['x1'] = coords[0] i['x1'] = coords[0]
i['y1'] = coords[1] i['y1'] = coords[1]
i['x2'] = x2 i['x2'] = x2
@ -278,15 +339,48 @@ class PlateauTk:
i['total'] = TEMPS_ANIM i['total'] = TEMPS_ANIM
i['avancement'] = 0 i['avancement'] = 0
return 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): def dPion(self, x1, y1, x2, y2):
test = self.logique.dPion(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[x2][y2], self.grillePions[x1][y1] = \
self.grillePions[x1][y1], False self.grillePions[x1][y1], False
self.cdPionAnim((x1 + .5) * COTE_CASE, (y1 + .5) * COTE_CASE, \ self.animerD((x1 + .5) * COTE_CASE, (y1 + .5) * COTE_CASE, \
(x2 + .5) * COTE_CASE, (y2 + .5) * COTE_CASE, \ (x2 + .5) * COTE_CASE, (y2 + .5) * COTE_CASE, \
self.grillePions[x2][y2]) self.grillePions[x2][y2])
return True return True
@ -297,15 +391,29 @@ class PlateauTk:
def dClic(self, x, y): def dClic(self, x, y):
# if not len(self.animations): # if not len(self.animations):
if self.dEtape: if self.dEtape: # Prendre
self.dx1, self.dy1 = x, y self.dx1, self.dy1 = x, y
# TODO Colorer case 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.statut('Poser')
else:
self.dx2, self.dy2 = x, y
self.dPion(self.dx1, self.dy1, self.dx2, self.dy2)
self.statutPrendre()
self.dEtape = not self.dEtape 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
self.statutPrendre()
else: # Si mauvais déplacement
self.coulCase(self.dx2, self.dy2, 3)
self.animerC(self.dx2, self.dy2)
def clic(self, event): def clic(self, event):
x = event.x // COTE_CASE x = event.x // COTE_CASE