-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path2048_.py
321 lines (282 loc) · 9.08 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
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
import random, pygame, sys
from pygame.locals import *
from random import randint
import copy
import math
#defining the window size and other different specifications of the window
FPS = 5
WINDOWWIDTH = 640
WINDOWHEIGHT = 640
boxsize = min(WINDOWWIDTH,WINDOWHEIGHT)//4;
margin = 5
thickness = 0
#defining the RGB for various colours used
WHITE= (255, 255, 255)
BLACK= ( 0, 0, 0)
RED = (255, 0, 0)
GREEN= ( 0, 255, 0)
DARKGREEN= ( 0, 155, 0)
DARKGRAY= ( 40, 40, 40)
LIGHTSALMON=(255, 160, 122)
ORANGE=(221, 118, 7)
LIGHTORANGE=(227,155,78)
CORAL=(255, 127, 80)
BLUE = (0, 0, 255)
LIGHTBLUE = (0, 0, 150)
colorback=(189,174,158)
colorblank=(205,193,180)
colorlight=(249,246,242)
colordark=(119,110,101)
fontSize=[100,85,70,55,40]
dictcolor1={
0:colorblank,
2:(238,228,218),
4:(237,224,200),
8:(242,177,121),
16:(245,149,99),
32:(246,124,95),
64:(246,95,59),
128:(237,207,114),
256:(237,204,97),
512:(237,200,80),
1024:(237,197,63),
2048:(237,194,46),
4096:(237,190,30),
8192:(239,180,25) }
dictcolor2={
2:colordark,
4:colordark,
8:colorlight,
16:colorlight,
32:colorlight,
64:colorlight,
128:colorlight,
256:colorlight,
512:colorlight,
1024:colorlight,
2048:colorlight,
4096:colorlight,
8192:colorlight }
BGCOLOR = LIGHTORANGE
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
TABLE=[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
def main():
global FPSCLOCK, screen, BASICFONT
pygame.init()
FPSCLOCK = pygame.time.Clock()
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
pygame.display.set_caption('2048')
showStartScreen()
while True:
runGame(TABLE)
gameover()
def createButton(text, x, y, width, height, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+width > mouse[0] > x and y+height > mouse[1] > y:
pygame.draw.rect(screen, LIGHTBLUE, (x,y,width,height))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(screen, BLUE, (x,y,width,height))
smallText = pygame.font.Font('freesansbold.ttf',50)
TextSurf = smallText.render(text,True,WHITE)
TextRect = TextSurf.get_rect()
TextRect.center = ((x+(width/2)),(y+(height/2)))
screen.blit(TextSurf, TextRect)
def showStartScreen():
#the start screen
titleFont = pygame.font.Font('freesansbold.ttf', 100)
titleSurf1 = titleFont.render('2048', True, WHITE, ORANGE)
while True:
screen.fill(BGCOLOR)
display_rect = pygame.transform.rotate(titleSurf1, 0)
rectangle = display_rect.get_rect()
rectangle.center = (WINDOWWIDTH / 2, WINDOWHEIGHT/8)
screen.blit(display_rect, rectangle)
createButton("NEW GAME", 80, 180, 480, 80, newGame)
createButton("HIGHSCORE", 80, 340, 480, 80, leaderboard)
createButton("QUIT", 80, 500, 480, 80, terminate)
if checkForKeyPress():
pygame.event.get()
return
pygame.display.update()
FPSCLOCK.tick(FPS)
def newGame():
runGame(TABLE)
def randomfill(TABLE):
# search for zero in the game table and randomly fill the places
flatTABLE = sum(TABLE,[])
if 0 not in flatTABLE:
return TABLE
empty=False
w=0
while not empty:
w=randint(0,15)
if TABLE[w//4][w%4] == 0:
empty=True
z=randint(1,5)
if z==5:
TABLE[w//4][w%4] = 4
else:
TABLE[w//4][w%4] = 2
return TABLE
def gameOver(TABLE):
# returns False if a box is empty or two boxes can be merged
x = [-1, 0, 1, 0 ]
y = [0 , 1, 0, -1]
for pi in range(4):
for pj in range(4):
if TABLE[pi][pj] == 0:
return False
for point in range(4):
if pi+x[point] > -1 and pi+x[point] < 4 and pj+y[point] > -1 and pj+y[point] < 4 and TABLE[pi][pj] == TABLE[pi+x[point]][pj+y[point]]:
return False
return True
def showGameOverMessage():
# to show game over screen
titleFont = pygame.font.Font('freesansbold.ttf', 60)
titleSurf1 = titleFont.render('Game Over', True, WHITE, ORANGE)
showMainMenu()
while True:
screen.fill(BGCOLOR)
display_rect = pygame.transform.rotate(titleSurf1, 0)
rectangle = display_rect.get_rect()
rectangle.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 2)
screen.blit(display_rect, rectangle)
showMainMenu()
pygame.display.update()
if checkForKeyPress():
if len(pygame.event.get()) > 0:
main()
FPSCLOCK.tick(FPS)
def showMainMenu():
# to display main menu
pressKeySurf = BASICFONT.render('Press a key for main menu', True, WHITE)
pressKeyRect = pressKeySurf.get_rect()
pressKeyRect.topleft = (WINDOWWIDTH - 250, WINDOWHEIGHT - 30)
screen.blit(pressKeySurf, pressKeyRect)
def checkForKeyPress():
#checking if a key is pressed or not
if len(pygame.event.get(QUIT)) > 0:
terminate()
keyUpEvents = pygame.event.get(KEYUP)
if len(keyUpEvents) == 0:
return None
if keyUpEvents[0].key == K_ESCAPE:
terminate()
return keyUpEvents[0].key
def show(TABLE):
#showing the table
screen.fill(colorback)
myfont = pygame.font.SysFont("Arial", 60, bold=True)
for i in range(4):
for j in range(4):
pygame.draw.rect(screen, dictcolor1[TABLE[i][j]], (j*boxsize+margin,
i*boxsize+margin,
boxsize-2*margin,
boxsize-2*margin),
thickness)
if TABLE[i][j] != 0:
order=int(math.log10(TABLE[i][j]))
myfont = pygame.font.SysFont("Arial", fontSize[order] , bold=True)
label = myfont.render("%4s" %(TABLE[i][j]), 1, dictcolor2[TABLE[i][j]] )
screen.blit(label, (j*boxsize+2*margin, i*boxsize+9*margin))
pygame.display.update()
def runGame(TABLE):
TABLE=randomfill(TABLE)
TABLE=randomfill(TABLE)
show(TABLE)
running=True
while True:
for event in pygame.event.get():
if event.type == QUIT:
print "quit"
pygame.quit(); sys.exit()
if event.type == pygame.KEYDOWN:
if running:
desired_key = None
if event.key == pygame.K_UP : desired_key = "w"
if event.key == pygame.K_DOWN : desired_key = "s"
if event.key == pygame.K_LEFT : desired_key = "a"
if event.key == pygame.K_RIGHT : desired_key = "d"
if desired_key is None:
continue
new_table = key(desired_key, copy.deepcopy(TABLE))
if new_table != TABLE:
TABLE=randomfill(new_table)
show(TABLE)
if gameOver(TABLE):
showGameOverMessage()
def key(DIRECTION,TABLE):
if DIRECTION =='w':
for pi in range(1,4):
for pj in range(4):
if TABLE[pi][pj] !=0: TABLE=moveup(pi,pj,TABLE)
elif DIRECTION =='s':
for pi in range(2,-1,-1):
for pj in range(4):
if TABLE[pi][pj] !=0: TABLE=movedown(pi,pj,TABLE)
elif DIRECTION =='a':
for pj in range(1,4):
for pi in range(4):
if TABLE[pi][pj] !=0: TABLE=moveleft(pi,pj,TABLE)
elif DIRECTION =='d':
for pj in range(2,-1,-1):
for pi in range(4):
if TABLE[pi][pj] !=0: TABLE=moveright(pi,pj,TABLE)
return TABLE
def movedown(pi,pj,T):
justcomb=False
while pi < 3 and (T[pi+1][pj] == 0 or (T[pi+1][pj] == T[pi][pj] and not justcomb)):
if T[pi+1][pj] == 0:
T[pi+1][pj] = T[pi][pj]
elif T[pi+1][pj]==T[pi][pj]:
T[pi+1][pj] += T[pi][pj]
justcomb=True
T[pi][pj]=0
pi+=1
return T
def moveleft(pi,pj,T):
justcomb=False
while pj > 0 and (T[pi][pj-1] == 0 or (T[pi][pj-1] == T[pi][pj] and not justcomb)):
if T[pi][pj-1] == 0:
T[pi][pj-1] = T[pi][pj]
elif T[pi][pj-1]==T[pi][pj]:
T[pi][pj-1] += T[pi][pj]
justcomb=True
T[pi][pj]=0
pj-=1
return T
def moveright(pi,pj,T):
justcomb=False
while pj < 3 and (T[pi][pj+1] == 0 or (T[pi][pj+1] == T[pi][pj] and not justcomb)):
if T[pi][pj+1] == 0:
T[pi][pj+1] = T[pi][pj]
elif T[pi][pj+1]==T[pi][pj]:
T[pi][pj+1] += T[pi][pj]
justcomb=True
T[pi][pj] = 0
pj+=1
return T
def moveup(pi,pj,T):
justcomb=False
while pi > 0 and (T[pi-1][pj] == 0 or (T[pi-1][pj] == T[pi][pj] and not justcomb)):
if T[pi-1][pj] == 0:
T[pi-1][pj] = T[pi][pj]
elif T[pi-1][pj]==T[pi][pj]:
T[pi-1][pj] += T[pi][pj]
justcomb=True
T[pi][pj]=0
pi-=1
return T
def leaderboard():
s = 'to show leaderboard'
def terminate():
pygame.quit()
sys.exit()
main()