-
Notifications
You must be signed in to change notification settings - Fork 0
/
2048.py
150 lines (132 loc) · 5.13 KB
/
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
from tkinter import *
import random
class Game:
def __init__(self, main, dim):
self.values = []
self.squares = []
self.dim = dim
self.rands = [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
self.score_val = 0
self.colours = {
2: "#EEE4DA",
4: "#EDE0C8",
8: "#F2B179",
16: "#F59563",
32: "#F67C5F",
64: "#F65E3B",
128: "#EDCF72",
256: "#EDCC61",
512: "#EDC850",
1024: "#EDC53F",
2048: "#EDC22E"
}
self.title = Label(main, text = "2048", padx = 100, pady = 30, font = ("Helvetica", 28), bg = "#D3D3D3", fg = "#3A3B3C")
self.score = Label(main, text = "SCORE\n" + str(self.score_val), padx = 95, pady = 25, font = ("Helvetica", 15), bg = "#3A3B3C", fg = "#D3D3D3")
self.title.grid(row = 0, column = 0, columnspan = 2, sticky = N+S+E+W)
self.score.grid(row = 0, column = 2, columnspan = 2, padx = 5, pady = 5, sticky = N+S+E+W)
self.left_btn = Button(main, text = "LEFT", padx = 50, pady = 20, command = self.left)
self.right_btn = Button(main, text = "RIGHT", padx = 50, pady = 20, command = self.right)
self.up_btn = Button(main, text = " UP ", padx = 50, pady = 20, command = self.up)
self.down_btn = Button(main, text = "DOWN", padx = 50, pady = 20, command = self.down)
self.left_btn.grid(row = 5, column = 0, sticky = N+S+E+W)
self.right_btn.grid(row = 5, column = 3, sticky = N+S+E+W)
self.up_btn.grid(row = 5, column = 1, sticky = N+S+E+W)
self.down_btn.grid(row = 5, column = 2, sticky = N+S+E+W)
for i in range(self.dim):
self.squares.append([])
for j in range(self.dim):
self.squares[i].append(Label(main, padx = 45, pady = 45, font = ("Helvetica", 15)))
self.squares[i][j].grid(row = i + 1, column = j, sticky = N+S+E+W, padx = 5, pady = 5)
self.create_grid()
self.update()
def create_grid(self):
for i in range(self.dim):
self.values.append([])
for j in range(self.dim):
rand_index = random.randint(0, len(self.rands) - 1)
rand = self.rands[rand_index]
self.values[i].append(rand)
self.rands.pop(rand_index)
def update(self):
for i in range(self.dim):
for j in range(self.dim):
if self.values[i][j] == 0:
self.squares[i][j]['text'] = ""
self.squares[i][j]['bg'] = "#f0f0ed"
else:
self.squares[i][j]['text'] = str(self.values[i][j])
self.squares[i][j]['bg'] = self.colours[self.values[i][j]]
self.score['text'] = "SCORE\n" + str(self.score_val)
def add_new(self):
empty_spaces = []
for i in range(self.dim):
for j in range(self.dim):
if self.values[i][j] == 0:
empty_spaces.append([i, j])
try:
rand = random.randint(0, len(empty_spaces) - 1)
self.values[empty_spaces[rand][0]][empty_spaces[rand][1]] = 2
except Exception as e:
return
def merge(self):
for i in range(self.dim):
for j in range(self.dim - 1):
if self.values[i][j] == self.values[i][j + 1]:
self.values[i][j] *= 2
self.score_val += self.values[i][j]
self.values[i][j + 1] = 0
def shift(self):
for i in range(self.dim):
for j in range(self.dim - 1):
for k in range(self.dim - j - 1):
if self.values[i][j] == 0:
for l in range(j, self.dim - 1):
self.values[i][l] = self.values[i][l + 1]
self.values[i][3] = 0
def reverse(self):
for i in range(self.dim):
for j in range(self.dim // 2):
self.values[i][j], self.values[i][self.dim - j - 1] = self.values[i][self.dim - j - 1], self.values[i][j]
def rotate(self):
for i in range(self.dim - 1):
for j in range(i + 1, self.dim):
self.values[j][i], self.values[i][j] = self.values[i][j], self.values[j][i]
def left(self):
self.shift()
self.merge()
self.shift()
self.add_new()
self.update()
def right(self):
self.reverse()
self.shift()
self.merge()
self.shift()
self.reverse()
self.add_new()
self.update()
def up(self):
self.rotate()
self.shift()
self.merge()
self.shift()
self.rotate()
self.add_new()
self.update()
def down(self):
self.rotate()
self.reverse()
self.shift()
self.merge()
self.shift()
self.reverse()
self.rotate()
self.add_new()
self.update()
def check_end():
return
root = Tk()
root.title("Play 2048!")
root.configure(bg = "#D3D3D3")
game = Game(root, 4)
root.mainloop()