Skip to content

Commit 03f082c

Browse files
committed
initial 2048 commit
1 parent 3a1225b commit 03f082c

File tree

4 files changed

+281
-0
lines changed

4 files changed

+281
-0
lines changed

Projects/2048/2048.py

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
import pygame
2+
import random
3+
4+
pygame.init()
5+
6+
# initial set up
7+
WIDTH = 400
8+
HEIGHT = 500
9+
screen = pygame.display.set_mode([WIDTH, HEIGHT])
10+
pygame.display.set_caption("2048")
11+
timer = pygame.time.Clock()
12+
fps = 60
13+
font = pygame.font.Font("freesansbold.ttf", 24)
14+
15+
# 2048 game color library
16+
colors = {
17+
0: (204, 192, 179),
18+
2: (238, 228, 218),
19+
4: (237, 224, 200),
20+
8: (242, 177, 121),
21+
16: (245, 149, 99),
22+
32: (246, 124, 95),
23+
64: (246, 94, 59),
24+
128: (237, 207, 114),
25+
256: (237, 204, 97),
26+
512: (237, 200, 80),
27+
1024: (237, 197, 63),
28+
2048: (237, 194, 46),
29+
"light text": (249, 246, 242),
30+
"dark text": (119, 110, 101),
31+
"other": (0, 0, 0),
32+
"bg": (187, 173, 160),
33+
}
34+
35+
# game variables initialize
36+
board_values = [[0 for _ in range(4)] for _ in range(4)]
37+
game_over = False
38+
spawn_new = True
39+
init_count = 0
40+
direction = ""
41+
score = 0
42+
file = open("2048\high_score.txt", "r")
43+
init_high = int(file.readline())
44+
file.close()
45+
high_score = init_high
46+
47+
# draw game over and restart text
48+
def draw_over():
49+
pygame.draw.rect(screen, "black", [50, 50, 300, 100], 0, 10)
50+
game_over_text1 = font.render("Game Over!", True, "white")
51+
game_over_text2 = font.render("Press Enter to Restart!", True, "white")
52+
screen.blit(game_over_text1, (130, 65))
53+
screen.blit(game_over_text2, (70, 105))
54+
55+
56+
# draw background for the board
57+
def draw_board():
58+
pygame.draw.rect(screen, colors["bg"], [0, 0, 400, 400], 0, 10)
59+
score_text = font.render(f"Score: {score}", True, "black")
60+
high_score_text = font.render(f"High Score: {high_score}", True, "black")
61+
screen.blit(score_text, (10, 410))
62+
screen.blit(high_score_text, (10, 450))
63+
pass
64+
65+
66+
# draw tiles for game
67+
def draw_pieces(board):
68+
for i in range(4):
69+
for j in range(4):
70+
value = board[i][j]
71+
if value > 8:
72+
value_color = colors["light text"]
73+
else:
74+
value_color = colors["dark text"]
75+
if value <= 2048:
76+
color = colors[value]
77+
else:
78+
color = color["other"]
79+
pygame.draw.rect(screen, color, [j * 95 + 20, i * 95 + 20, 75, 75], 0, 5)
80+
if value > 0:
81+
value_len = len(str(value))
82+
font = pygame.font.Font("freesansbold.ttf", 48 - (5 * value_len))
83+
value_text = font.render(str(value), True, value_color)
84+
text_rect = value_text.get_rect(center=(j * 95 + 57, i * 95 + 57))
85+
screen.blit(value_text, text_rect)
86+
pygame.draw.rect(
87+
screen, "black", [j * 95 + 20, i * 95 + 20, 75, 75], 2, 5
88+
)
89+
90+
91+
# function to check if there is any valid move
92+
# to decide if the game is over
93+
def is_game_over(board):
94+
for i in range(3):
95+
for j in range(3):
96+
if board[i][j] == board[i + 1][j] or board[i][j] == board[i][j + 1]:
97+
return False
98+
99+
for j in range(3):
100+
if board[3][j] == board[3][j + 1]:
101+
return False
102+
103+
for i in range(3):
104+
if board[i][3] == board[i + 1][3]:
105+
return False
106+
## if there's no valid move left, the game is over
107+
return True
108+
109+
110+
# spawn in new pieces randomly when turns start
111+
def new_pieces(board):
112+
count = 0
113+
full = False
114+
while any(0 in row for row in board) and count < 1:
115+
row = random.randint(0, 3)
116+
col = random.randint(0, 3)
117+
if board[row][col] == 0:
118+
count += 1
119+
if random.randint(1, 10) == 10:
120+
board[row][col] = 4
121+
else:
122+
board[row][col] = 2
123+
if count < 1:
124+
full = is_game_over(board)
125+
return board, full
126+
127+
128+
# take your turn based on direction
129+
def take_turn(direc, board):
130+
global score
131+
merged = [[False for _ in range(4)] for _ in range(4)]
132+
if direc == "UP":
133+
for i in range(4):
134+
for j in range(4):
135+
shift = 0
136+
if i > 0:
137+
for q in range(i):
138+
if board[q][j] == 0:
139+
shift += 1
140+
if shift > 0:
141+
board[i - shift][j] = board[i][j]
142+
board[i][j] = 0
143+
if (
144+
board[i - shift - 1][j] == board[i - shift][j]
145+
and not merged[i - shift][j]
146+
and not merged[i - shift - 1][j]
147+
):
148+
board[i - shift - 1][j] *= 2
149+
score += board[i - shift - 1][j]
150+
board[i - shift][j] = 0
151+
merged[i - shift - 1][j] = True
152+
elif direc == "DOWN":
153+
for i in range(3):
154+
for j in range(4):
155+
shift = 0
156+
for q in range(i + 1):
157+
if board[3 - q][j] == 0:
158+
shift += 1
159+
if shift > 0:
160+
board[2 - i + shift][j] = board[2 - i][j]
161+
board[2 - i][j] = 0
162+
if 3 - i + shift <= 3:
163+
if (
164+
board[2 - i + shift][j] == board[3 - i + shift][j]
165+
and not merged[3 - i + shift][j]
166+
and not merged[2 - i + shift][j]
167+
):
168+
board[3 - i + shift][j] *= 2
169+
score += board[3 - i + shift][j]
170+
board[2 - i + shift][j] = 0
171+
merged[3 - i + shift][j] = True
172+
elif direc == "LEFT":
173+
for i in range(4):
174+
for j in range(4):
175+
shift = 0
176+
for q in range(j):
177+
if board[i][q] == 0:
178+
shift += 1
179+
if shift > 0:
180+
board[i][j - shift] = board[i][j]
181+
board[i][j] = 0
182+
if (
183+
board[i][j - shift] == board[i][j - shift - 1]
184+
and not merged[i][j - shift - 1]
185+
and not merged[i][j - shift]
186+
):
187+
board[i][j - shift - 1] *= 2
188+
score += board[i][j - shift - 1]
189+
board[i][j - shift] = 0
190+
merged[i][j - shift - 1] = True
191+
elif direc == "RIGHT":
192+
for i in range(4):
193+
for j in range(4):
194+
shift = 0
195+
for q in range(j):
196+
if board[i][3 - q] == 0:
197+
shift += 1
198+
if shift > 0:
199+
board[i][3 - j + shift] = board[i][3 - j]
200+
board[i][3 - j] = 0
201+
if 4 - j + shift <= 3:
202+
if (
203+
board[i][4 - j + shift] == board[i][3 - j + shift]
204+
and not merged[i][3 - j + shift]
205+
and not merged[i][4 - j + shift]
206+
):
207+
board[i][4 - j + shift] *= 2
208+
score += board[i][4 - j + shift]
209+
board[i][3 - j + shift] = 0
210+
merged[i][4 - j + shift] = True
211+
212+
return board
213+
214+
215+
# main game loop
216+
run = True
217+
while run:
218+
timer.tick(fps)
219+
screen.fill("gray")
220+
draw_board()
221+
draw_pieces(board_values)
222+
if spawn_new or init_count < 2:
223+
board_values, game_over = new_pieces(board_values)
224+
spawn_new = False
225+
init_count += 1
226+
if direction != "":
227+
board_values = take_turn(direction, board_values)
228+
direction = ""
229+
spawn_new = True
230+
if game_over:
231+
draw_over()
232+
if high_score > init_high:
233+
file = open("2048\high_score.txt", "w")
234+
file.write(f"{high_score}")
235+
file.close()
236+
init_high = high_score
237+
238+
for event in pygame.event.get():
239+
if event.type == pygame.QUIT:
240+
run = False
241+
if event.type == pygame.KEYUP:
242+
if event.key == pygame.K_UP:
243+
direction = "UP"
244+
elif event.key == pygame.K_DOWN:
245+
direction = "DOWN"
246+
elif event.key == pygame.K_LEFT:
247+
direction = "LEFT"
248+
elif event.key == pygame.K_RIGHT:
249+
direction = "RIGHT"
250+
251+
if game_over:
252+
if event.key == pygame.K_RETURN:
253+
board_values = [[0 for _ in range(4)] for _ in range(4)]
254+
spawn_new = True
255+
init_count = 0
256+
score = 0
257+
direction = ""
258+
game_over = False
259+
260+
if score > high_score:
261+
high_score = score
262+
pygame.display.flip()
263+
pygame.quit()

Projects/2048/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 2048
2+
3+
The script is a recreation attempt of 2048 game using pygame library for GUI
4+
5+
## Installation
6+
7+
Use the package manager [pip](https://pip.pypa.io/en/stable/) to install pygame library or requirements.txt.
8+
9+
```bash
10+
pip install -r requirements.txt
11+
```
12+
13+
or
14+
15+
```bash
16+
pip install pygame
17+
```

Projects/2048/high_score.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

Projects/2048/requirements.txt

28 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)