-
Notifications
You must be signed in to change notification settings - Fork 1
/
2048V2.py
153 lines (131 loc) · 3.53 KB
/
2048V2.py
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
#2048 Graphique
#Wep c'est ça le talent!
#Marc est génial
from tkinter import *
import math
import random
import time
#Initialisation
gameOver=False
IA=False
Width = 500
Height = 500
cx=8
cy=6
l=Width//4//2
L=Height//4//2
Grille= [0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0]
Entrée=["g","h","d","b"]
Couleurs=["white","green","blue","purple","pink","red","orange","yellow","grey","black"]
m="0"
p=0
#Fin Initialisation
def Génération():
Indice=[]
Random=1
for a in range(0,16):
if Grille[a]==0:
Indice.append(a)
Random=random.randint(0,len(Indice)-1)
Grille[Indice[Random]]=2*random.randint(1,2)
def Affichage(board):
for y in range(0,4):
for x in range(0,4):
i=x+4*y
j=0
a=board[i]
while a>=2:
j=j+1
a=a/2
rectangle = canvas.create_rectangle((x+1/2)*Width//4-l+cx,(y+1/2)*Height//4-L+cy, (x+1/2)*Width//4+l+cx, (y+1/2)*Height//4+L+cy, fill=Couleurs[j])
txt = canvas.create_text((x+1/2)*Width//4+cx,(y+1/2)*Height//4+cy, text=board[i], font="Arial 40 italic", fill=Couleurs[j+1])
canvas.pack()
def Décalage():
for x in range(0,4):
Décal1=[]
for y in range(0,4):
if m=="h" or m=="b":
i=x+4*y
if m=="g" or m=="d":
i=4*x+y
Décal1.append(Grille[i])
if m=="b" or m=="d":
Décal1.reverse()
Décal2=[]
for a in range(0,4):
if Décal1[a]!=0:
Décal2.append(Décal1[a])
a=len(Décal2)
for i in range(0,a-1):
if Décal2[i]==Décal2[i+1]:
Décal2[i]=2*Décal2[i]
Décal2[i+1]=0
for j in range(0,a-1):
if Décal2[j]==0:
del Décal2[j]
Décal3=Décal2+(4-len(Décal2))*[0]
if m=="b" or m=="d":
Décal3.reverse()
for y in range(0,4):
if m=="h" or m=="b":
i=x+4*y
if m=="g" or m=="d":
i=4*x+y
Grille[i]=Décal3[y]
def GameOver() :
global Grille,m
p=1
Grille2=Grille
for n in range(0,4):
m=Entrée[n]
Décalage()
if Grille.count(0)==0:
window.quit()
Grille=Grille2
def GameOver1():
if Grille.count(0)==0:
window.quit()
def clavier(event):
global m,Entrée
touche=event.keysym
if IA:
while IA :
m=Entrée[random.randint(0,3)]
Génération()
Affichage(Grille)
GameOver1()
else:
if touche=="Up":
m="h"
if touche=="Down":
m="b"
if touche=="Right":
m="d"
if touche=="Left":
m="g"
if touche=="Escape":
window.quit()
Décalage()
Génération()
Affichage(Grille)
GameOver1()
window = Tk()
canvas = Canvas(window, width=Width+cx, height=Height+cy, bg="#ffffff")
canvas.pack()
img = PhotoImage(width=Width+cx, height=Height+cy)
canvas.create_image((Width//2+cx, Height//2+cy), image=img, state="normal")
for x in range(0,4):
for y in range(0,4):
rectangle = canvas.create_rectangle((x+1/2)*Width//4-l+cx,(y+1/2)*Height//4-L+cy, (x+1/2)*Width//4+l+cx, (y+1/2)*Height//4+L+cy, fill="white")
canvas.pack()
Génération()
Génération()
Affichage(Grille)
canvas.focus_set()
canvas.bind("<Key>", clavier)
window.mainloop()
window.destroy()
print("La partie est finie")