Skip to content

Commit 8f7d8c9

Browse files
committed
Added 2048 game file
1 parent b6a718f commit 8f7d8c9

File tree

7 files changed

+302
-0
lines changed

7 files changed

+302
-0
lines changed

Diff for: PyGamesScripts/2048/2048 try.py

+268
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
import tkinter
2+
import random
3+
from tkinter import Label, messagebox
4+
5+
#assigining background colours
6+
bg_grid_color = {
7+
2: "light blue",
8+
4: "light green",
9+
8: "yellow",
10+
16: "orange",
11+
32: "red",
12+
64: "magenta",
13+
128: "blue",
14+
256: "green",
15+
512: "#00FFFF",
16+
1024: "#856ff8",
17+
2048: "pink"
18+
}
19+
#assigning colours for the number
20+
number_colour ={
21+
2: "red",
22+
4: "blue",
23+
8: "#856ff8",
24+
16: "black",
25+
32: "yellow",
26+
64: "green",
27+
128: "light green",
28+
256: "light blue",
29+
512: "red",
30+
1024: "#00FFFF",
31+
2048: "magenta"
32+
}
33+
34+
class Game(tkinter.Frame):
35+
#creating the framework
36+
def __init__(self):
37+
tkinter.Frame.__init__(self)
38+
self.grid()
39+
self.master.title('2048')
40+
41+
self.mainGrid = tkinter.Frame(
42+
self, bg="grey", bd=3, width=400, height=400)
43+
self.mainGrid.grid(pady=(100, 0))
44+
self.make_grid()
45+
self.begin_game()
46+
#commands to be followed when corresponding keys r pressed
47+
self.master.bind("<Left>", self.left)
48+
self.master.bind("<Right>", self.right)
49+
self.master.bind("<Up>", self.up)
50+
self.master.bind("<Down>", self.down)
51+
52+
self.mainloop()
53+
54+
55+
#creating the grids
56+
def make_grid(self):
57+
self.cells = []
58+
#making a 4 X 4 grid
59+
for i in range(4):
60+
row = []
61+
for j in range(4):
62+
cell_frame = tkinter.Frame(
63+
self.mainGrid,
64+
bg="azure4",
65+
width=100,
66+
height=100)
67+
cell_frame.grid(row=i, column=j, padx=5, pady=5)
68+
cell_number = tkinter.Label(self.mainGrid, bg="azure4")
69+
cell_number.grid(row=i, column=j)
70+
cell_data = {"frame": cell_frame, "value": cell_number}
71+
row.append(cell_data)
72+
self.cells.append(row)
73+
74+
# Displaying the score
75+
score_frame = tkinter.Frame(self)
76+
score_frame.place(relx=0.5, y=45, anchor="center")
77+
tkinter.Label(
78+
score_frame,
79+
text="Score"
80+
).grid(
81+
row=0)
82+
self.score_label = tkinter.Label(score_frame, text="0")
83+
self.score_label.grid(row=1)
84+
85+
86+
def begin_game(self):
87+
88+
self.matrix = [[0] * 4 for i in range(4)]
89+
90+
# rondomly placing 2 2's in 2 grids
91+
row = random.randint(0, 3)
92+
column = random.randint(0, 3)
93+
#changing their bg colour and displaying the value
94+
self.matrix[row][column] = 2
95+
self.cells[row][column]["frame"].configure(bg=bg_grid_color[2])
96+
self.cells[row][column]["value"].configure(
97+
bg=bg_grid_color[2],
98+
fg=number_colour[2],
99+
text="2")
100+
while(self.matrix[row][column] != 0):
101+
row = random.randint(0, 3)
102+
col = random.randint(0, 3)
103+
#changing their bg colour and displaying the value
104+
self.matrix[row][column] = 2
105+
self.cells[row][column]["frame"].configure(bg=bg_grid_color[2])
106+
self.cells[row][column]["value"].configure(
107+
bg=bg_grid_color[2],
108+
fg=number_colour[2],
109+
text="2")
110+
111+
self.score = 0
112+
113+
114+
115+
116+
def stack(self):
117+
#finding out all grids which are empty
118+
filled_box_matrix = [[0] * 4 for i in range(4)]
119+
for i in range(4):
120+
pos = 0
121+
for j in range(4):
122+
if (self.matrix[i][j] != 0):
123+
filled_box_matrix[i][pos] = self.matrix[i][j]
124+
pos += 1
125+
self.matrix = filled_box_matrix
126+
127+
128+
def combine(self):
129+
#combining values in 2 grids if they have the same value
130+
for i in range(4):
131+
for j in range(3):
132+
if (self.matrix[i][j] != 0 and self.matrix[i][j] == self.matrix[i][j + 1]):
133+
self.matrix[i][j] *= 2
134+
self.matrix[i][j + 1] = 0
135+
self.score += self.matrix[i][j]
136+
137+
138+
#to reverse the order of each row
139+
def reverse(self):
140+
newMat = []
141+
for i in range(4):
142+
newMat.append([])
143+
for j in range(4):
144+
newMat[i].append(self.matrix[i][3 - j])
145+
self.matrix = newMat
146+
147+
148+
#To flip the matrix over diagonal
149+
def transpose(self):
150+
tempMat = [[0] * 4 for i in range(4)]
151+
for i in range(4):
152+
for j in range(4):
153+
tempMat[i][j] = self.matrix[j][i]
154+
self.matrix = tempMat
155+
156+
157+
# Add a new 2 or 4 tile randomly to an empty cell
158+
159+
def new_tile(self):
160+
row = random.randint(0, 3)
161+
column = random.randint(0, 3)
162+
while(self.matrix[row][column] != 0):
163+
row = random.randint(0, 3)
164+
column = random.randint(0, 3)
165+
self.matrix[row][column] = random.choice([2, 4])
166+
167+
168+
#updating the matrix
169+
def updation(self):
170+
for i in range(4):
171+
for j in range(4):
172+
cell_value = self.matrix[i][j]
173+
if (cell_value == 0):
174+
self.cells[i][j]["frame"].configure(bg="azure4")
175+
self.cells[i][j]["value"].configure(bg="azure4",text=" ")
176+
else:
177+
self.cells[i][j]["frame"].configure(bg=bg_grid_color[cell_value])
178+
self.cells[i][j]["value"].configure(
179+
bg=bg_grid_color[cell_value],
180+
fg=number_colour[cell_value],
181+
text=str(cell_value))
182+
self.score_label.configure(text=self.score)
183+
self.update_idletasks()
184+
185+
186+
187+
188+
#when you press left
189+
def left(self, event):
190+
self.stack()
191+
self.combine()
192+
self.stack()
193+
self.new_tile()
194+
self.updation()
195+
self.check_end()
196+
197+
198+
#when you press right
199+
def right(self, event):
200+
self.reverse()
201+
self.stack()
202+
self.combine()
203+
self.stack()
204+
self.reverse()
205+
self.new_tile()
206+
self.updation()
207+
self.check_end()
208+
209+
210+
#when you press up
211+
def up(self, event):
212+
self.transpose()
213+
self.stack()
214+
self.combine()
215+
self.stack()
216+
self.transpose()
217+
self.new_tile()
218+
self.updation()
219+
self.check_end()
220+
221+
222+
#when you press down
223+
def down(self, event):
224+
self.transpose()
225+
self.reverse()
226+
self.stack()
227+
self.combine()
228+
self.stack()
229+
self.reverse()
230+
self.transpose()
231+
self.new_tile()
232+
self.updation()
233+
self.check_end()
234+
235+
236+
237+
238+
#To check if any moves r left
239+
def hori_possible(self):
240+
for i in range(4):
241+
for j in range(3):
242+
if self.matrix[i][j] == self.matrix[i][j + 1]:
243+
return True
244+
return False
245+
246+
247+
def verti_possible(self):
248+
for i in range(3):
249+
for j in range(4):
250+
if self.matrix[i][j] == self.matrix[i + 1][j]:
251+
return True
252+
return False
253+
254+
255+
256+
257+
#checking if game over
258+
def check_end(self):
259+
if any(2048 in row for row in self.matrix):
260+
messagebox.showinfo("YOU WIN","your score is {}".format(self.score))
261+
elif not any(0 in row for row in self.matrix) and not self.hori_possible() and not self.verti_possible():
262+
messagebox.showinfo("YOU LOOSE","your final score is {}".format(self.score))
263+
264+
265+
266+
Game()
267+
268+

Diff for: PyGamesScripts/2048/Images/lost.PNG

30.7 KB
Loading

Diff for: PyGamesScripts/2048/Images/playing.PNG

19.8 KB
Loading

Diff for: PyGamesScripts/2048/Images/startingWords.PNG

15.7 KB
Loading

Diff for: PyGamesScripts/2048/Images/youlose.PNG

11.5 KB
Loading

Diff for: PyGamesScripts/2048/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 2048 Game
2+
2048 a game developed in python programming Language using random module and tkinter module.
3+
4+
## About the game
5+
* It is a single player sliding tile puzzle game
6+
* The user can keep playing till he gets a 2048 tile or all the grids are full and no grids can be merged
7+
8+
## How to play?
9+
* The user has to use arrow keys to play the game
10+
* The user has to press the arrow key such that the adjacent tiles get merged.
11+
* The tiles get merged only if the initial 2 tiles have the same value
12+
* If there is no possiblity for a merge then pressing any key will bring up a new tile
13+
* The user will win if he gets a 2048 tile
14+
* The user looses when there are no merges left and all tiles are full
15+
16+
## Setup instructions
17+
1. Install Python 3.x (recommended) from https://www.python.org/downloads/
18+
2. Download this repository as zip and extract.
19+
3. Run the code and start playing.
20+
4. Have fun!!
21+
22+
## Real time use and purpose
23+
* It is used to play 2048 game which sharpens your mind and is also a source of distraction and relaxation.
24+
25+
## Output
26+
![GitHub Logo](Images/playing.PNG)
27+
28+
29+
## Author
30+
K.Harshitha
31+

Diff for: PyGamesScripts/2048/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import tkinter
2+
import random
3+
from tkinter import Label, messagebox

0 commit comments

Comments
 (0)