1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
# -*- coding: utf-8 -*-
from Tkinter import *
from random import *
from copy import *
def gen_alea_2(Tab): #Fonction permettant d obtenir un 2 aleatoirement sur une des cases vides
nb0=''
alea=0
for i in Tab:
if Tab[i]==0:
nb0=nb0+i
if nb0 != '':
x=int(randint(0,len(nb0)-1))
alea=randint(1,10)
if alea<10:
Tab[nb0[x]]=2
else:
Tab[nb0[x]]=4
return True
else:
return False
def verif_nb(nb): #Permet de ne pas afficher les 0 (a mettre dans chaque print) et d afficher les chiffres a peu pres au milieu (prend 4 caracteres de large)
if nb == 0:
return ''
else:
return nb
def dico2liste(Dico,direction): #Transforme le dico en une serie de listes : colonne si on va vers le haut ou le bas, ligne si on va vers la gauche ou la droite, pour gerer ligne par ligne. On change aussi l'ordre selon le chiffre (pour commencer par le 1 de chaque liste)
liste1=[]
liste2=[]
liste3=[]
liste4=[]
if direction == "Up":
liste1.append(Dico['a'])
liste1.append(Dico['e'])
liste1.append(Dico['i'])
liste1.append(Dico['m'])
liste2.append(Dico['b'])
liste2.append(Dico['f'])
liste2.append(Dico['j'])
liste2.append(Dico['n'])
liste3.append(Dico['c'])
liste3.append(Dico['g'])
liste3.append(Dico['k'])
liste3.append(Dico['o'])
liste4.append(Dico['d'])
liste4.append(Dico['h'])
liste4.append(Dico['l'])
liste4.append(Dico['p'])
elif direction=="Down":
liste1.append(Dico['m'])
liste1.append(Dico['i'])
liste1.append(Dico['e'])
liste1.append(Dico['a'])
liste2.append(Dico['n'])
liste2.append(Dico['j'])
liste2.append(Dico['f'])
liste2.append(Dico['b'])
liste3.append(Dico['o'])
liste3.append(Dico['k'])
liste3.append(Dico['g'])
liste3.append(Dico['c'])
liste4.append(Dico['p'])
liste4.append(Dico['l'])
liste4.append(Dico['h'])
liste4.append(Dico['d'])
elif direction=="Right":
liste1.append(Dico['d'])
liste1.append(Dico['c'])
liste1.append(Dico['b'])
liste1.append(Dico['a'])
liste2.append(Dico['h'])
liste2.append(Dico['g'])
liste2.append(Dico['f'])
liste2.append(Dico['e'])
liste3.append(Dico['l'])
liste3.append(Dico['k'])
liste3.append(Dico['j'])
liste3.append(Dico['i'])
liste4.append(Dico['p'])
liste4.append(Dico['o'])
liste4.append(Dico['n'])
liste4.append(Dico['m'])
elif direction=="Left":
liste1.append(Dico['a'])
liste1.append(Dico['b'])
liste1.append(Dico['c'])
liste1.append(Dico['d'])
liste2.append(Dico['e'])
liste2.append(Dico['f'])
liste2.append(Dico['g'])
liste2.append(Dico['h'])
liste3.append(Dico['i'])
liste3.append(Dico['j'])
liste3.append(Dico['k'])
liste3.append(Dico['l'])
liste4.append(Dico['m'])
liste4.append(Dico['n'])
liste4.append(Dico['o'])
liste4.append(Dico['p'])
return liste1,liste2,liste3,liste4
def bouge_bloc(liste,bloc): #bouge un bloc au maximum dans la direction voulu
k=bloc
if liste[k] != 0:
while liste[k-1]==0 and k !=0:
k=k-1
if k != bloc:
liste[k]=liste[bloc]
liste[bloc]=0
return k
def fusion(liste,bloc1,bloc2): #On fusionne 2 blocs, bloc1 etant le plus petit dans la liste
liste[bloc1]=liste[bloc1]+liste[bloc2]
liste[bloc2]=0
def control_fusion(case,liste_fus): #On verifie si on a pas deja fusionne cette case
if liste_fus==[]:
return True
for i in liste_fus:
if i==case:
return False
return True
def liste2dico(l1,l2,l3,l4,direction,Dico):
if direction=="Up":
Dico['a']=l1[0]
Dico['b']=l2[0]
Dico['c']=l3[0]
Dico['d']=l4[0]
Dico['e']=l1[1]
Dico['f']=l2[1]
Dico['g']=l3[1]
Dico['h']=l4[1]
Dico['i']=l1[2]
Dico['j']=l2[2]
Dico['k']=l3[2]
Dico['l']=l4[2]
Dico['m']=l1[3]
Dico['n']=l2[3]
Dico['o']=l3[3]
Dico['p']=l4[3]
if direction=="Down":
Dico['a']=l1[3]
Dico['b']=l2[3]
Dico['c']=l3[3]
Dico['d']=l4[3]
Dico['e']=l1[2]
Dico['f']=l2[2]
Dico['g']=l3[2]
Dico['h']=l4[2]
Dico['i']=l1[1]
Dico['j']=l2[1]
Dico['k']=l3[1]
Dico['l']=l4[1]
Dico['m']=l1[0]
Dico['n']=l2[0]
Dico['o']=l3[0]
Dico['p']=l4[0]
if direction=="Left":
Dico['a']=l1[0]
Dico['b']=l1[1]
Dico['c']=l1[2]
Dico['d']=l1[3]
Dico['e']=l2[0]
Dico['f']=l2[1]
Dico['g']=l2[2]
Dico['h']=l2[3]
Dico['i']=l3[0]
Dico['j']=l3[1]
Dico['k']=l3[2]
Dico['l']=l3[3]
Dico['m']=l4[0]
Dico['n']=l4[1]
Dico['o']=l4[2]
Dico['p']=l4[3]
if direction=="Right":
Dico['a']=l1[3]
Dico['b']=l1[2]
Dico['c']=l1[1]
Dico['d']=l1[0]
Dico['e']=l2[3]
Dico['f']=l2[2]
Dico['g']=l2[1]
Dico['h']=l2[0]
Dico['i']=l3[3]
Dico['j']=l3[2]
Dico['k']=l3[1]
Dico['l']=l3[0]
Dico['m']=l4[3]
Dico['n']=l4[2]
Dico['o']=l4[1]
Dico['p']=l4[0]
def verif_blocage(Tableau,Direction): #Fonction verifiant si le tableau est bloque ou non dans une direction dans une direction
Tableau1=deepcopy(Tableau)
Tableau2=deepcopy(Tableau)
l1,l2,l3,l4=dico2liste(Tableau1,Direction)
Liste1=[l1,l2,l3,l4]
for i in Liste1:
for j in range(1,4):
k=bouge_bloc(i,j)
if i[k]==i[k-1]:
fusion(i,k-1,k)
liste2dico(l1,l2,l3,l4,Direction,Tableau2)
if Tableau1 != Tableau2:
return True
return False
def trad_c2l(j,i): #Traduit les chiffres en lettres
if i==0:
if j==0:
return 'a'
if j==1:
return 'b'
if j==2:
return 'c'
if j==3:
return 'd'
elif i==1:
if j==0:
return 'e'
if j==1:
return 'f'
if j==2:
return 'g'
if j==3:
return 'h'
elif i==2:
if j==0:
return 'i'
if j==1:
return 'j'
if j==2:
return 'k'
if j==3:
return 'l'
elif i==3:
if j==0:
return 'm'
if j==1:
return 'n'
if j==2:
return 'o'
if j==3:
return 'p'
def couleur(bloc):#Fonction affichant une couleur differente en fonction de la valeur du bloc
if bloc==2:
return "purple"
elif bloc==4:
return "violet"
elif bloc==8:
return "darkblue"
elif bloc==16:
return "blue"
elif bloc==32:
return "darkgreen"
elif bloc==64:
return "lightgreen"
elif bloc==128:
return "yellow"
elif bloc==256:
return "gold"
elif bloc==512:
return "orange"
elif bloc==1024:
return "magenta"
elif bloc==2048:
return "red"
# Prog principal
#initialisation
# jeu
def Rejouer(): #fonction permettant de remettre à 0
Endgame.destroy()
Tableau={'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0}
zone_dessin.delete(ALL)
for j in range(0,4):
for i in range(0,4):
zone_dessin.create_rectangle(125*i,125*j,125*i+125,125*j+125) #dessine le cadre
gen_alea_2(Tableau)
for j in range(0,4): #Entre les valeurs du dico dans les cases du cadre
for i in range(0,4):
txt = zone_dessin.create_text(62.5+i*125, 62.5+j*125, text=verif_nb(Tableau[trad_c2l(i,j)]), font="Arial 32", fill=couleur(verif_nb(Tableau[trad_c2l(i,j)])))
def clavier(event):
Direction = event.keysym
if Direction != 'Right' and Direction != 'Left' and Direction != 'Up' and Direction != 'Down' :
return
if not verif_blocage(Tableau,Direction):
return
l1,l2,l3,l4=dico2liste(Tableau,Direction)
L_tot=[l1,l2,l3,l4]
fus_ctrl=[] #Liste (vide au debut) de control de fusion
for i in L_tot:
fus_ctrl=[]
for j in range(1,4):
k=bouge_bloc(i,j)
if i[k]==i[k-1]:
if control_fusion(i[k],fus_ctrl):
fusion(i,k-1,k)
fus_ctrl.append(k-1)
liste2dico(l1,l2,l3,l4,Direction,Tableau)
gen_alea_2(Tableau)
zone_dessin.delete(ALL)
for j in range(0,4):
for i in range(0,4):
zone_dessin.create_rectangle(125*i,125*j,125*i+125,125*j+125) #dessine le cadre
for j in range(0,4): #Entre les valeurs du dico dans les cases du cadre
for i in range(0,4):
txt = zone_dessin.create_text(62.5+i*125, 62.5+j*125, text=verif_nb(Tableau[trad_c2l(i,j)]), font="Arial 32", fill=couleur(verif_nb(Tableau[trad_c2l(i,j)])))
if not verif_blocage(Tableau,'Up') and not verif_blocage(Tableau,'Down') and not verif_blocage(Tableau,'Right') and not verif_blocage(Tableau,'Left'): #Afficher une fenetre permettant de quitter en cas de blocage
global Endgame
Endgame=Frame(racine,borderwidth=2,relief=GROOVE)
Endgame.pack(side=TOP,padx=50,pady=10)
Label(Endgame,text="Vous avez perdu",fg ='blue').pack(padx=10,pady=10)
Button_quit=Button(Endgame,text="Quitter",fg='navy',command=racine.destroy).pack(side = LEFT,padx=10,pady=10)
Button_rejouer=Button(Endgame,text="Rejouer",fg='navy',command=Rejouer).pack(side = RIGHT,padx=10,pady=10)
Tableau={'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0} #Dico reprenant les 16 cases (de a a p) du 2048
racine= Tk()
zone_dessin = Canvas(racine, width=500, height=500) #Définit les dimensions du canevas
zone_dessin.pack() #Affiche le canevas
racine.title("2048")
for j in range(0,4):
for i in range(0,4):
zone_dessin.create_rectangle(125*i,125*j,125*i+125,125*j+125) #dessine le cadre
zone_dessin.focus_set()
zone_dessin.bind("<Key>", clavier)
zone_dessin.pack()
gen_alea_2(Tableau)
for j in range(0,4): #Entre les valeurs du dico dans les cases du cadre
for i in range(0,4):
txt = zone_dessin.create_text(62.5+i*125, 62.5+j*125, text=verif_nb(Tableau[trad_c2l(i,j)]), font="Arial 32",fill=couleur(verif_nb(Tableau[trad_c2l(i,j)])))
racine.mainloop() |