[Echecs] Possibilité de redimensionner le canvas

Je pensais que je l'avais commité ce matin, mais les changements ne se
sont retrouvé que dans la branche tkResize...
This commit is contained in:
Geoffrey Frogeye 2014-12-13 23:11:25 +01:00
parent 3dc2cdb531
commit 87635bb8a6

View file

@ -3,13 +3,12 @@ from logique import LogiqueEchecs
class PlateauTk:
DECX = 0
DECY = 0
COTE_CASE = 50
MARGE_PIONS = 5
RATIO_PCE_CASE = .95
TEMPS_ANIM = 200
INTER_ANIM = 10
COTE_TOUT_DEFAUT = 500
def __init__(self, fen, can, statut, logique):
self.can = can
@ -31,19 +30,26 @@ class PlateauTk:
self.mvtsPossibles = []
self.logique = logique
self.redimCan()
self.can.bind('<Button-1>', self.clic)
self.importerImages()
self.redimImages()
self.coteCase = 0
self.importerImages()
self.redimCan(self.COTE_TOUT_DEFAUT)
self.can.bind('<Button-1>', self.clic)
self.statutPrendre()
def redimCan(self, cote):
self.can.delete(ALL)
self.coteCase = cote // self.logique.CASES_COTE
cote = self.logique.CASES_COTE * self.coteCase
self.can.config(width=cote, height=cote)
for i in self.imagesRedim:
del i
self.redimImages()
self.cDamier()
self.cGrille()
self.remplirGrille(self.logique.grille)
self.statutPrendre()
def redimCan(self):
self.can.config(width=self.COTE_CASE * self.logique.CASES_COTE, height=self.COTE_CASE * self.logique.CASES_COTE)
def nomPiece(self, piece):
tPiece = self.logique.tPiece(piece)
@ -65,7 +71,6 @@ class PlateauTk:
def importerImage(self, piece, couleur):
nom = 'sprites/'+self.nomPiece(piece)+couleur+'.gif'
self.imagesOriginales.update({piece:PhotoImage(file=nom)})
def importerImages(self):
for piece in self.logique.BLANCS:
@ -77,7 +82,7 @@ class PlateauTk:
self.imagesRedim.update({piece:self.imagesOriginales[piece].subsample(sample)})
def redimImages(self):
sample = int(504 / (PlateauTk.COTE_CASE - PlateauTk.MARGE_PIONS))
sample = int(504 // (self.coteCase * self.RATIO_PCE_CASE)) # TODO S'en sort pour être toujours plus grand
for piece in self.logique.BLANCS:
self.redimImage(piece, sample)
for piece in self.logique.NOIRS:
@ -97,7 +102,7 @@ class PlateauTk:
def cCase(self, x, y):
couleur = self.caseCouleur(self.logique.eCaseBlanche(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)
return self.can.create_rectangle(x * self.coteCase, y * self.coteCase, (x + 1) * self.coteCase, (y + 1) * self.coteCase)
def coulCase(self, x, y, contexte):
couleur = self.caseCouleur(self.logique.eCaseBlanche(x, y), contexte)
@ -113,13 +118,13 @@ class PlateauTk:
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))
colonne.append(self.cCase(x, y))
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])
self.grillePions[x][y] = self.can.create_image((x + .5) * self.coteCase, (y + .5) * self.coteCase, image=self.imagesRedim[piece])
else:
self.grillePions[x][y] = False
@ -168,7 +173,7 @@ class PlateauTk:
# TODO Opacité de i['pion']
# elif i['type'] == 'c':
# TODO Opacité de case
i['avancement'] += PlateauTk.INTER_ANIM
i['avancement'] += self.INTER_ANIM
animationsNv.append(i)
else:
if i['type'] == 'd':
@ -179,7 +184,7 @@ class PlateauTk:
self.coulCase(i['x'], i['y'], 0)
self.animations = animationsNv
if len(animationsNv):
self.fen.after(PlateauTk.INTER_ANIM, self.animation)
self.fen.after(self.INTER_ANIM, self.animation)
def animer(self, animation):
etaitVide = len(self.animations) < 1
@ -190,15 +195,15 @@ class PlateauTk:
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
if i['type'] == 'd' and 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['total'] = self.TEMPS_ANIM
i['avancement'] = 0
return
@ -209,7 +214,7 @@ class PlateauTk:
'y2': y2,
'pion': pion,
'type': 'd',
'total': PlateauTk.TEMPS_ANIM,
'total': self.TEMPS_ANIM,
'avancement': 0
}
self.can.tag_raise(pion) # Mise au premier plan
@ -219,7 +224,7 @@ class PlateauTk:
animation = {
'pion': pion,
'type': 'f',
'total': PlateauTk.TEMPS_ANIM,
'total': self.TEMPS_ANIM,
'avancement': 0
}
self.animer(animation)
@ -229,7 +234,7 @@ class PlateauTk:
'type': 'c',
'x': x,
'y': y,
'total': PlateauTk.TEMPS_ANIM,
'total': self.TEMPS_ANIM,
'avancement': 0
}
self.animer(animation)
@ -254,8 +259,8 @@ class PlateauTk:
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.animerD((d[0] + .5) * self.coteCase, (d[1] + .5) * self.coteCase, \
(d[2] + .5) * self.coteCase, (d[3] + .5) * self.coteCase, \
self.grillePions[d[2]][d[3]])
else:
@ -291,9 +296,8 @@ class PlateauTk:
self.animerC(self.dx2, self.dy2)
def clic(self, event):
x = event.x // self.COTE_CASE
y = event.y // self.COTE_CASE
self.dClic(x, y)
if event.x in range(0, self.coteCase * self.logique.CASES_COTE) and event.y in range(0, self.coteCase * self.logique.CASES_COTE):
self.dClic(event.x // self.coteCase, event.y // self.coteCase)
class FenetreTk: