Echecs : Fin de POO et gestion multi-animation et re-animation

This commit is contained in:
Geoffrey Frogeye 2014-11-21 10:00:27 +01:00
parent 0dc988076e
commit c1765ed037

View file

@ -1,10 +1,12 @@
CASES_COTE = 8 CASES_COTE = 8
class LogiqueEchecs: class LogiqueEchecs:
# grille = None # grille = None
# joueur = True # joueur = True
def __init__(self, self): def __init__(self):
self.grille = [] self.grille = []
self.cGrille() self.cGrille()
self.remplirGrille() self.remplirGrille()
@ -21,14 +23,14 @@ class LogiqueEchecs:
if x == xD and y == yD: if x == xD and y == yD:
return i % 2 return i % 2
def cGrille(self, self): def cGrille(self):
for x in range(CASES_COTE): for x in range(CASES_COTE):
colonne = [] colonne = []
for y in range(CASES_COTE): for y in range(CASES_COTE):
colonne.append(0) colonne.append(0)
self.grille.append(colonne) self.grille.append(colonne)
def remplirGrille(self, self): def remplirGrille(self):
speciales = [2, 3, 4, 6, 5, 4, 3, 2] speciales = [2, 3, 4, 6, 5, 4, 3, 2]
for i in range(0, 8): for i in range(0, 8):
self.grille[i][0] = speciales[i] + 10 self.grille[i][0] = speciales[i] + 10
@ -36,18 +38,18 @@ class LogiqueEchecs:
self.grille[i][6] = 1 self.grille[i][6] = 1
self.grille[i][7] = speciales[i] self.grille[i][7] = speciales[i]
def nvPartie(self, self): def nvPartie(self):
self.cGrille() self.cGrille()
self.remplirGrille() self.remplirGrille()
self.joueur = True self.joueur = True
def cPion(self, self, x, y, piece): def cPion(self, x, y, piece):
""" """
""" """
self.grille[x][y] = piece self.grille[x][y] = piece
return True return True
def mvtPossible(self, 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 (pion >= 10 and self.joueur == False) or \
(pion < 10 and self.joueur == True): (pion < 10 and self.joueur == True):
@ -82,7 +84,7 @@ class LogiqueEchecs:
else: else:
return -1 return -1
def dPion(self, 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 test == True:
self.grille[x2][y2] = self.grille[x1][y1] self.grille[x2][y2] = self.grille[x1][y1]
@ -103,59 +105,47 @@ MARGE_PIONS = 5
TEMPS_ANIM = 200 TEMPS_ANIM = 200
INTER_ANIM = 10 INTER_ANIM = 10
class PlateauTkAnim:
def __init__(self, can, x1, x2, y1, y2, pion):
self.can = can
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.pion = pion
self.anim = anim
final()
# def dPionAnim(self):
# x = self.x1 + (self.x2-self.x1) * (self.anim/TEMPS_ANIM)
# y = self.y1 + (self.y2-self.y1) * (self.anim/TEMPS_ANIM)
# can.coords(self.pion, x, y)
# self.anim += INTER_ANIM
# if self.anim < TEMPS_ANIM:
# fen.after(INTER_ANIM, self.dPionAnim)
# else:
# self.dPionFinal()
def final(self):
self.can.coords(self.pion, self.x2, self.y2)
class PlateauTk: class PlateauTk:
def __init__(self): def __init__(self):
self.imagesOriginales = []
self.imagesRedim = []
self.grilleDamier = None
self.grillePions = None
self.photos = []
self.fen = None self.fen = None
self.can = None self.can = None
self.chaine = 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.logique = LogiqueEchecs()
self.creerFen() self.creerFen()
self.importerImages() self.importerImages()
self.redimImages() self.redimImages()
self.cDamier()
self.cGrille()
self.remplirGrille(self.logique.grille)
def creerFen(self): def creerFen(self):
self.fen = Tk() self.fen = Tk()
self.fen.title("Jeu d'Échecs") self.fen.title("Jeu d'Échecs")
self.can = Canvas(self.fen, width=COTE_CASE*CASES_COTE, \ self.can = Canvas(self.fen, width=COTE_CASE * CASES_COTE,
height=COTE_CASE * CASES_COTE, bg="ivory") height=COTE_CASE * CASES_COTE, bg="ivory")
self.can.grid(row=0, column=1, columnspan=3) self.can.grid(row=0, column=1, columnspan=3)
# self.can.bind('<Button-1>', f_clic) self.can.bind('<Button-1>', self.clic)
self.chaine = Label(self.fen, text="Aux blancs") self.chaine = Label(self.fen, text="Aux blancs")
self.chaine.grid(row=2, column=2, padx=3, pady=3) self.chaine.grid(row=2, column=2, padx=3, pady=3)
# Button(self.fen, text="Nv. Partie", command=f_nvPartie).grid(row=2, \ # Button(self.fen, text="Nv. Partie", command=f_nvPartie).grid(row=2, \
# column=1, padx=3, pady=3) # column=1, padx=3, pady=3)
Button(self.fen, text="Quitter", command=self.fen.destroy).grid(row=2, \ Button(self.fen, text="Quitter", command=self.fen.destroy).grid(row=2,
column=3, padx=3, pady=3) column=3, padx=3, pady=3)
def statut(self, texte, delai=0): def statut(self, texte, delai=0):
@ -191,17 +181,18 @@ class PlateauTk:
sample = int(504 / (COTE_CASE - MARGE_PIONS)) sample = int(504 / (COTE_CASE - MARGE_PIONS))
for piece in range(0, 21): for piece in range(0, 21):
if self.imagesOriginales[piece] != '': if self.imagesOriginales[piece] != '':
self.imagesRedim.append(self.imagesOriginales[piece].subsample(sample)) self.imagesRedim.append(self.imagesOriginales[piece].
subsample(sample))
else: else:
self.imagesRedim.append('') self.imagesRedim.append('')
# Dessin
def cCase(self, x, y): def cCase(self, x, y):
if LogiqueEchecs.eNoir(x, y): if LogiqueEchecs.eNoir(x, y):
couleur = '#D18B47' couleur = '#D18B47'
else: else:
couleur = '#FFCE9E' couleur = '#FFCE9E'
return self.can.create_rectangle(x*COTE_CASE, y*COTE_CASE, \ return self.can.create_rectangle(x * COTE_CASE, y * COTE_CASE,
(x + 1) * COTE_CASE, (y + 1) * COTE_CASE, fill=couleur) (x + 1) * COTE_CASE, (y + 1) * COTE_CASE, fill=couleur)
def cDamier(self): def cDamier(self):
@ -214,18 +205,11 @@ class PlateauTk:
def cPion(self, x, y, piece): def cPion(self, x, y, piece):
if piece > 0: if piece > 0:
self.grillePions[x][y] = can.create_image((x+.5)*COTE_CASE, \ self.grillePions[x][y] = self.can.create_image((x + .5) * COTE_CASE,
(y + .5) * COTE_CASE, image=self.imagesRedim[piece]) (y + .5) * COTE_CASE, image=self.imagesRedim[piece])
else: else:
self.grillePions[x][y] = False self.grillePions[x][y] = False
def dPion(self, x1, y1, x2, y2):
# TODO Bloquer l'entrée pendant l'anim
self.grillePions[x2][y2] = self.grillePions[x1][y1]
self.grillePions[x1][y1] = False
PlateauTkAnim(self.can, (x1+.5)*COTE_CASE, (y1+.5)*COTE_CASE, \
(x2+.5)*COTE_CASE, (y2+.5)*COTE_CASE, self.grillePions[x1][y1])
def cGrille(self): def cGrille(self):
self.grillePions = [] self.grillePions = []
for x in range(0, CASES_COTE): # Crée self.grillePions for x in range(0, CASES_COTE): # Crée self.grillePions
@ -238,3 +222,96 @@ class PlateauTk:
for x in range(0, CASES_COTE): # Remplis self.grillePions for x in range(0, CASES_COTE): # Remplis self.grillePions
for y in range(0, CASES_COTE): for y in range(0, CASES_COTE):
self.cPion(x, y, j_grilleF[x][y]) self.cPion(x, y, j_grilleF[x][y])
# Interaction
def statutPrendre(self):
if self.logique.joueur:
joueur = 'blancs'
else:
joueur = 'noirs'
self.statut('Prendre (' + joueur + ')')
@staticmethod
def dPionAnimCoords(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):
animationsNv = []
for i in self.animations:
if i['avancement'] < i['total']:
coords = self.dPionAnimCoords(i)
self.can.coords(i['pion'], coords[0], coords[1])
i['avancement'] += INTER_ANIM
animationsNv.append(i)
else:
self.can.coords(i['pion'], i['x2'], i['y2'])
self.animations = animationsNv
if len(animationsNv):
self.fen.after(INTER_ANIM, self.dPionAnim)
def cdPionAnim(self, x1, y1, x2, y2, pion):
animation = {
'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.dPionAnim()
else:
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)
i['x1'] = coords[0]
i['y1'] = coords[1]
i['x2'] = x2
i['y2'] = y2
# i['total'] = i['total'] - i['avancement']
i['total'] = TEMPS_ANIM
i['avancement'] = 0
return
self.animations.append(animation)
def dPion(self, x1, y1, x2, y2):
test = self.logique.dPion(x1, y1, x2, y2)
if test == True:
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])
return True
else:
# TODO Messages corrects
self.statut('Impossible ! (' + str(test) + ')')
return False
def dClic(self, x, y):
# if not len(self.animations):
if self.dEtape:
self.dx1, self.dy1 = x, y
# TODO Colorer case
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
def clic(self, event):
x = event.x // COTE_CASE
y = event.y // COTE_CASE
self.dClic(x, y)
p = PlateauTk()
# TODO Un jeu (canvas) et un frontend (fenetre)