-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_2048.py
331 lines (306 loc) · 14.8 KB
/
game_2048.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
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
import tkinter as tk
import random
import math
# function to start the game, main function
def start_2048():
class Game(tk.Tk):
# board variable
board = []
# to create probability distribution to randomly select next tile number
new_tile_selection = [2, 2, 2, 2, 2, 2, 4]
# tile colors for different numbered tiles
tile_colors = ["#f5f5f5", "#e0f2f8", "#b8dbe5", "#71b1bd", "#27819f", "#0073b9", "#7fa8d7", "#615ea6",
"#2f3490",
"#1c1862", "#9c005d", "#c80048"]
score = 0
highscore = 0
scorestring = 0
highscorestring = 0
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.buttonframe = tk.Frame(self)
self.square = {}
self.scorestring = tk.StringVar(self)
self.scorestring.set("0")
self.highscorestring = tk.StringVar(self)
self.highscorestring.set("0")
self.create_widgets()
# size of window
self.canvas = tk.Canvas(
self, width=800, height=800, borderwidth=5, highlightthickness=0)
self.canvas.pack(side="top", fill="both", expand="false")
# start new game
self.new_game()
def add_new_tile(self):
# select random index from probability distribution
index = random.randint(0, 6)
while not self.is_full():
x = random.randint(0, 3)
y = random.randint(0, 3)
# retry if block of board is filled
if self.board[x][y] != 0:
continue
self.board[x][y] = self.new_tile_selection[index]
# print new tile
x1 = y * 210
y1 = x * 210
x2 = x1 + 210 - 5
y2 = y1 + 210 - 5
num = self.board[x][y]
if num == 2:
self.square[x, y] = self.canvas.create_rectangle(x1, y1, x2, y2, fill="#e0f2f8", tags="rect",
outline="", width=0)
self.canvas.create_text(
(x1 + x2) / 2, (y1 + y2) / 2, font=("Arial", 36), fill="#ffb459", text="2")
elif num == 4:
self.square[x, y] = self.canvas.create_rectangle(x1, y1, x2, y2, fill="#b8dbe5", tags="rect",
outline="", width=0)
self.canvas.create_text(
(x1 + x2) / 2, (y1 + y2) / 2, font=("Arial", 36), fill="#ffb459", text="4")
break
# Returns True if board is full
def is_full(self):
for i in range(0, 4):
for j in range(0, 4):
if self.board[i][j] == 0:
return False
return True
# Prints game board
def print_board(self):
cellwidth = 210
cellheight = 210
for column in range(4):
for row in range(4):
x1 = column * cellwidth
y1 = row * cellheight
x2 = x1 + cellwidth - 5
y2 = y1 + cellheight - 5
num = self.board[row][column]
self.print_tile(row, column, x1, y1, x2, y2, num)
# prints a single tile
def print_tile(self, row, column, x1, y1, x2, y2, num):
current_tile_color = self.tile_colors[0]
if num > 0:
current_tile_color = self.tile_colors[int(math.log2(num))]
self.square[row, column] = self.canvas.create_rectangle(x1, y1, x2, y2,
fill=current_tile_color, tags="rect",
outline="")
if num != 0:
text_color = "#494949"
if num > 4:
text_color = "white"
self.canvas.create_text(
(x1 + x2) / 2, (y1 + y2) / 2, font=("Arial", 36), fill=text_color, text=str(num))
def create_widgets(self):
self.buttonframe = tk.Frame(self)
self.buttonframe.grid(row=2, column=0, columnspan=4)
tk.Button(self.buttonframe, text="New Game",
command=self.new_game).grid(row=0, column=0)
tk.Label(self.buttonframe, text="Score:").grid(row=0, column=1)
tk.Label(self.buttonframe, textvariable=self.scorestring).grid(
row=0, column=2)
tk.Label(self.buttonframe, text="Record:").grid(row=0, column=3)
tk.Label(self.buttonframe, textvariable=self.highscorestring).grid(
row=0, column=4)
self.buttonframe.pack(side="top")
# actions to perform for which key pressed
def key_pressed(self, event):
# move all tiles downward if pressed down arrow key
if event.keysym == 'Down':
for j in range(0, 4):
shift = 0
for i in range(3, -1, -1):
if self.board[i][j] == 0:
shift += 1
else:
if i - 1 >= 0 and self.board[i - 1][j] == self.board[i][j]:
self.board[i][j] *= 2
self.score += self.board[i][j]
self.board[i - 1][j] = 0
elif i - 2 >= 0 and self.board[i - 1][j] == 0 and self.board[i - 2][j] == self.board[i][j]:
self.board[i][j] *= 2
self.score += self.board[i][j]
self.board[i - 2][j] = 0
elif i == 3 and self.board[2][j] + self.board[1][j] == 0 and self.board[0][j] == \
self.board[3][
j]:
self.board[3][j] *= 2
self.score += self.board[3][j]
self.board[0][j] = 0
if shift > 0:
self.board[i + shift][j] = self.board[i][j]
self.board[i][j] = 0
self.print_board()
self.add_new_tile()
self.is_over()
# move all tiles in right direction if pressed right arrow key
elif event.keysym == 'Right':
for i in range(0, 4):
shift = 0
for j in range(3, -1, -1):
if self.board[i][j] == 0:
shift += 1
else:
if j - 1 >= 0 and self.board[i][j - 1] == self.board[i][j]:
self.board[i][j] *= 2
self.score += self.board[i][j]
self.board[i][j - 1] = 0
elif j - 2 >= 0 and self.board[i][j - 1] == 0 and self.board[i][j - 2] == self.board[i][j]:
self.board[i][j] *= 2
self.score += self.board[i][j]
self.board[i][j - 2] = 0
elif j == 3 and self.board[i][2] + self.board[i][1] == 0 and self.board[0][j] == \
self.board[3][
j]:
self.board[i][3] *= 2
self.score += self.board[i][3]
self.board[i][0] = 0
if shift > 0:
self.board[i][j + shift] = self.board[i][j]
self.board[i][j] = 0
self.print_board()
self.add_new_tile()
self.is_over()
# move all tiles in left direction if left arrow key is pressed
elif event.keysym == 'Left':
for i in range(0, 4):
shift = 0
for j in range(0, 4):
if self.board[i][j] == 0:
shift += 1
else:
if j + 1 < 4 and self.board[i][j + 1] == self.board[i][j]:
self.board[i][j] *= 2
self.score += self.board[i][j]
self.board[i][j + 1] = 0
elif j + 2 < 4 and self.board[i][j + 1] == 0 and self.board[i][j + 2] == self.board[i][j]:
self.board[i][j] *= 2
self.score += self.board[i][j]
self.board[i][j + 2] = 0
elif j == 0 and self.board[i][1] + self.board[i][2] == 0 and self.board[i][3] == \
self.board[i][
0]:
self.board[i][0] *= 2
self.score += self.board[i][0]
self.board[i][3] = 0
if shift > 0:
self.board[i][j - shift] = self.board[i][j]
self.board[i][j] = 0
self.print_board()
self.add_new_tile()
self.is_over()
# move all tiles in up direction is up arrow key is pressed
elif event.keysym == 'Up':
for j in range(0, 4):
shift = 0
for i in range(0, 4):
if self.board[i][j] == 0:
shift += 1
else:
if i + 1 < 4 and self.board[i + 1][j] == self.board[i][j]:
self.board[i][j] *= 2
self.score += self.board[i][j]
self.board[i + 1][j] = 0
elif i + 2 < 4 and self.board[i + 1][j] == 0 and self.board[i + 2][j] == self.board[i][j]:
self.board[i][j] *= 2
self.score += self.board[i][j]
self.board[i + 2][j] = 0
elif i == 0 and self.board[1][j] + self.board[2][j] == 0 and self.board[3][j] == \
self.board[0][
j]:
self.board[0][j] *= 2
self.score += self.board[0][j]
self.board[3][j] = 0
if shift > 0:
self.board[i - shift][j] = self.board[i][j]
self.board[i][j] = 0
self.print_board()
self.add_new_tile()
self.is_over()
self.scorestring.set(str(self.score))
if self.score > self.highscore:
self.highscore = self.score
self.highscorestring.set(str(self.highscore))
# initialize and start a new game
def new_game(self):
self.score = 0
self.scorestring.set("0")
# clean and reinitialize board
self.board = []
self.board.append([0, 0, 0, 0])
self.board.append([0, 0, 0, 0])
self.board.append([0, 0, 0, 0])
self.board.append([0, 0, 0, 0])
while True:
x = random.randint(0, 3)
y = random.randint(0, 3)
if self.board[x][y] == 0:
self.board[x][y] = 2
break
index = random.randint(0, 6)
while not self.is_full():
x = random.randint(0, 3)
y = random.randint(0, 3)
if self.board[x][y] == 0:
self.board[x][y] = self.new_tile_selection[index]
break
self.print_board()
# check and take action if game is over
def is_over(self):
# conditions at which game is over
for i in range(0, 4):
for j in range(0, 4):
if self.board[i][j] == 2048:
self.you_won()
for i in range(0, 4):
for j in range(0, 4):
if self.board[i][j] == 0:
return False
for i in range(0, 4):
for j in range(0, 3):
if self.board[i][j] == self.board[i][j + 1]:
return False
for j in range(0, 4):
for i in range(0, 3):
if self.board[i][j] == self.board[i + 1][j]:
return False
# print game over
gameover = [["G", "A", "M", "E", ], ["O", "V", "E", "R"], [
"", "", "", ""], ["", "", "", ""]]
cellwidth = 210
cellheight = 210
self.square = {}
for column in range(4):
for row in range(4):
x1 = column * cellwidth
y1 = row * cellheight
x2 = x1 + cellwidth - 10
y2 = y1 + cellheight - 10
self.square[row, column] = self.canvas.create_rectangle(x1, y1, x2, y2, fill="#e0f2f8", tags="rect",
outline="")
self.canvas.create_text((x1 + x2) / 2, (y1 + y2) / 2, font=("Arial", 36), fill="#494949",
text=gameover[row][column])
return True
# print you won
def you_won(self):
gameover = [["Y", "O", "U", "", ], ["W", "O", "N", "!"], [
"", "", "", ""], ["", "", "", ""]]
cellwidth = 210
cellheight = 210
self.square = {}
for column in range(4):
for row in range(4):
x1 = column * cellwidth
y1 = row * cellheight
x2 = x1 + cellwidth - 5
y2 = y1 + cellheight - 5
self.square[row, column] = self.canvas.create_rectangle(x1, y1, x2, y2, fill="#e0f2f8", tags="rect",
outline="")
self.canvas.create_text((x1 + x2) / 2, (y1 + y2) / 2, font=("Arial", 36), fill="#494949",
text=gameover[row][column])
app = Game()
app.bind_all('<Key>', app.key_pressed)
app.wm_title("2048 Game")
app.minsize(840, 850)
app.maxsize(840, 850)
app.mainloop()