-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048.py
More file actions
427 lines (397 loc) · 16.8 KB
/
Copy path2048.py
File metadata and controls
427 lines (397 loc) · 16.8 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import pygame
import random
import sys
pygame.init()
WIDTH = 700
HEIGHT = 700
EMPTY = (208, 192, 177)
WHITE = (250,250,250)
BLACK = (10,10,10)
COLOR2 = (238, 228, 218)
COLOR4 = (237, 224, 200)
COLOR8 = (242, 177, 121)
COLOR16 = (245, 149, 99)
COLOR32=(246, 124, 95)
COLOR64 = (246, 94, 59)
COLOR128 = (237, 207, 114)
COLOR256 = (237, 204,97)
COLOR512 = (237, 200, 80)
COLOR1024 = (237, 197, 63)
COLOR2048 = (237, 194, 46)
COLORELSE = BLACK
COLORFONT1 = (255, 243, 245)
COLORFONT2 = (115, 105, 95)
GREY = (50,50,50)
BACKCOLOR = (188, 172, 157)
ORANGE = (255, 165,0)
screen = pygame.display.set_mode((WIDTH,HEIGHT))
clock = pygame.time.Clock()
myFont =pygame.font.SysFont("monospace",100)
myFont1 =pygame.font.SysFont("monospace",130)
myFont2 =pygame.font.SysFont("monospace",90)
myFont3 =pygame.font.SysFont("monospace",35)
class two:
def __init__(self, size, value,spotX, spotY, canAdd = True):
self.size = size
self.spacing = 20
self.value = value
self.x = spotX*self.size
self.y = spotY*self.size
self.canAdd = canAdd
def moveRight(self):
## for i in range(10):
## self.x += self.size//10
## self.draw()
## pygame.display.update()
self.x += self.size
def moveLeft(self):
## for i in range(10):
## self.x -= self.size//10
## self.draw()
## pygame.display.update()
self.x -= self.size
def moveDown(self):
## for i in range(10):
## self.y += self.size//10
## self.draw()
## pygame.display.update()
self.y += self.size
def moveUp(self):
## for i in range(10):
## self.y -= self.size//10
## self.draw()
## pygame.display.update()
self.y -= self.size
def draw(self):
drawX = self.x/self.size*(self.size+self.spacing)+self.spacing
drawY = self.y/self.size*(self.size+self.spacing)+self.spacing
if self.value==2:
color = COLOR2
fontColor = COLORFONT2
fontX = 55
label = myFont.render(str(self.value), 1, fontColor)
elif self.value==4:
color = COLOR4
fontColor = COLORFONT2
fontX = 55
label = myFont.render(str(self.value), 1, fontColor)
elif self.value==8:
color = COLOR8
fontColor = COLORFONT1
fontX = 55
label = myFont.render(str(self.value), 1, fontColor)
elif self.value==16:
color = COLOR16
fontColor = COLORFONT1
fontX = 35
label = myFont.render(str(self.value), 1, fontColor)
elif self.value==32:
color = COLOR32
fontColor = COLORFONT1
fontX = 35
label = myFont.render(str(self.value), 1, fontColor)
elif self.value==64:
color = COLOR64
fontColor = COLORFONT1
fontX = 35
label = myFont.render(str(self.value), 1, fontColor)
elif self.value==128:
color = COLOR128
fontColor = COLORFONT1
fontX = 18
label = myFont.render(str(self.value), 1, fontColor)
elif self.value==256:
color = COLOR256
fontColor = COLORFONT1
fontX = 18
label = myFont.render(str(self.value), 1, fontColor)
elif self.value==512:
color = COLOR512
fontColor = COLORFONT1
fontX = 18
label = myFont.render(str(self.value), 1, fontColor)
elif self.value==1024:
color = COLOR1024
fontColor = COLORFONT1
fontX = 3
label = myFont2.render(str(self.value), 1, fontColor)
elif self.value==2048:
color = COLOR2048
fontColor = COLORFONT1
fontX = 3
label = myFont2.render(str(self.value), 1, fontColor)
else:
color = BLACK
fontColor = COLORFONT1
fontX = 3
label = myFont2.render(str(self.value), 1, fontColor)
pygame.draw.rect(screen,color,(drawX,drawY,self.size,self.size))
if self.size==150:
screen.blit(label,(drawX+fontX,drawY+45))
def popUp(self):
cornerX = self.x
cornerY = self.y
self.size = 50
for i in range(5):
for event in pygame.event.get():
if event.type== pygame.QUIT:
pygame.quit()
sys.exit()
break
self.size+=20
self.x = cornerX+(150-self.size)/2
self.y = cornerY+(150-self.size)/2
self.draw()
pygame.display.update()
self.x = cornerX
self.y = cornerY
def isOnButton(mousePos, buttonPos, buttonSize):
if mousePos[0]>buttonPos[0] and mousePos[0]<buttonPos[0]+buttonSize[0] and mousePos[1]>buttonPos[1] and mousePos[1]<buttonPos[1]+buttonSize[1]:
return True
return False
def fillBoard():
blocks = []
for i in range(4):
row= []
for x in range(4):
row.append(None)
blocks.append(row)
return blocks
def drawBack(blockSize):
screen.fill(BACKCOLOR)
for r in range(4):
for c in range(4):
drawX = c*(blockSize+20)+20
drawY = r*(blockSize+20)+20
pygame.draw.rect(screen,EMPTY,(drawX,drawY,blockSize, blockSize))
def checkForEnd(blocks):
over = True
for r in range(len(blocks)):
for c in range(len(blocks[r])-1):
if blocks[r][c] == None:
over = False
elif blocks[r][c+1]!=None:
if blocks[r][c+1].value == blocks[r][c].value:
over = False
for c in range(len(blocks[0])):
for r in range(len(blocks)-1):
if blocks[r][c] == None:
over = False
elif blocks[r+1][c] !=None:
if blocks[r+1][c].value == blocks[r][c].value:
over = False
return over
def printBoard(blocks):
for row in blocks:
rower = []
for block in row:
rower.append(block.value)
print(rower)
def endGame(blocks):
buttonPos = [WIDTH/2-90,HEIGHT/2]
buttonSize = [180,50]
buttony = buttonPos[1]
onButton = False
printBoard(blocks)
while True:
screen.fill(COLOR2)
label = myFont1.render("GAME OVER!", 1, COLORFONT2)
screen.blit(label,(WIDTH/2-275,150))
for event in pygame.event.get():
if event.type== pygame.QUIT:
pygame.quit()
sys.exit()
break
if pygame.mouse.get_pressed()[0] and onButton:
screen.fill(BACKCOLOR)
playGame()
if pygame.mouse.get_pressed()[0] and onButton1:
screen.fill(BACKCOLOR)
homeScreen()
mousePos = pygame.mouse.get_pos()
if isOnButton(mousePos, buttonPos, buttonSize):
onButton =True
buttony = buttonPos[1]+5
else:
onButton = False
buttony = buttonPos[1]
pygame.draw.rect(screen,COLOR2048,(buttonPos[0],buttony,buttonSize[0],buttonSize[1]))
label1 = myFont3.render("PLAY AGAIN", 1, COLORFONT1)
screen.blit(label1,(buttonPos[0]+12,buttony+15))
pygame.display.update()
def playGame():
blockSize = 150
blocks = []
blockLoc = []
newNeeded = True
secondNew = True
gameOver = False
screen.fill(BACKCOLOR)
#fill blank board in blocks
blocks = fillBoard()
while not gameOver:
drawBack(blockSize)
if newNeeded:
## for i in range(10000):
## i=0
spotX = random.randint(0,3)
spotY = random.randint(0,3)
valueChoices = [2,2,2,4, 2,2,2]
value = random.choice(valueChoices)
toAdd = two(blockSize, value, spotX, spotY)
toAdd.popUp()
indR = (toAdd.y)//(toAdd.size)
indC= (toAdd.x)//(toAdd.size)
while(blocks[indR][indC]!=None):
spotX = random.randint(0,3)
spotY = random.randint(0,3)
toAdd = two(blockSize, value, spotX, spotY)
indR = (toAdd.y)//(toAdd.size)
indC= (toAdd.x)//(toAdd.size)
blocks[indR][indC] = toAdd
blockLoc.append([toAdd.x, toAdd.y])
newNeeded = False
if secondNew:
spotX = random.randint(0,3)
spotY = random.randint(0,3)
toAdd = two(blockSize, 2, spotX, spotY)
indR = (toAdd.y)//(toAdd.size)
indC= (toAdd.x)//(toAdd.size)
while(blocks[indR][indC]!=None):
spotX = random.randint(0,3)
spotY = random.randint(0,3)
toAdd = two(blockSize, 2, spotX, spotY)
indR = (toAdd.y)//(toAdd.size)
indC= (toAdd.x)//(toAdd.size)
blocks[indR][indC] = toAdd
blockLoc.append([toAdd.x, toAdd.y])
secondNew = False
for event in pygame.event.get():
if event.type== pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RIGHT or event.key==pygame.K_LEFT or event.key==pygame.K_UP or event.key==pygame.K_DOWN:
newBlocks = fillBoard()
newLocs = []
if event.key ==pygame.K_RIGHT:
for row in blocks:
for numCol in range(1,len(row)+1):
block = row[-numCol]
while (block!=None) and ([block.x+block.size,block.y] not in newLocs) and (block.x<(WIDTH-(block.spacing*5+block.size))):
block.moveRight()
newNeeded = True
indR = (block.y)//(block.size)
indC = (block.x)//(block.size)
##newBlocks[indR][indC] = block
if (block!=None):
newLocs.append([block.x,block.y])
indR = int((block.y)//(block.size))
indC = int((block.x)//(block.size))
if indC <3:
if newBlocks[indR][indC+1].value==block.value:
if newBlocks[indR][indC+1].canAdd:
block1 = two(blockSize, block.value*2, indC+1, indR, False)
newBlocks[indR][indC+1] = block1
newLocs.remove([block.x,block.y])
newNeeded = True
else:
newBlocks[indR][indC] = block
else:
newBlocks[int(indR)][int(indC)] = block
else:
newBlocks[int(indR)][int(indC)] = block
##print(newBlocks)
elif event.key ==pygame.K_LEFT:
for row in blocks:
for block in row:
while (block!=None) and ([block.x-block.size,block.y] not in newLocs) and (block.x>0):
block.moveLeft()
newNeeded = True
indR = int((block.y)//(block.size))
indC= int((block.x)//(block.size))
##newBlocks[indR][indC] = block
if (block!=None):
newLocs.append([block.x,block.y])
indR = int((block.y)//(block.size))
indC = int((block.x)//(block.size))
if indC >0:
if newBlocks[indR][indC-1].value==block.value:
if newBlocks[indR][indC-1].canAdd:
block1 = two(blockSize, block.value*2, indC-1, indR, False)
newBlocks[indR][indC-1] = block1
newLocs.remove([block.x,block.y])
newNeeded = True
else:
newBlocks[indR][indC] = block
else:
newBlocks[int(indR)][int(indC)] = block
else:
newBlocks[int(indR)][int(indC)] = block
elif event.key ==pygame.K_UP:
for row in blocks:
for block in row:
while (block!=None) and ([block.x,block.y-block.size] not in newLocs) and (block.y>0):
block.moveUp()
newNeeded = True
indR = int((block.y)//(block.size))
indC= int((block.x)//(block.size))
##newBlocks[indR][indC] = block
if block!=None:
newLocs.append([block.x,block.y])
indR = int((block.y)//(block.size))
indC = int((block.x)//(block.size))
if indR >0:
if newBlocks[indR-1][indC].value==block.value:
if newBlocks[indR-1][indC].canAdd:
block1 = two(blockSize, block.value*2, indC, indR-1, False)
newBlocks[indR-1][indC] = block1
newLocs.remove([block.x,block.y])
newNeeded = True
else:
newBlocks[indR][indC] = block
else:
newBlocks[int(indR)][int(indC)] = block
else:
newBlocks[int(indR)][int(indC)] = block
elif event.key ==pygame.K_DOWN:
for numRow in range(1,len(blocks)+1):
for block in blocks[-numRow]:
while (block!=None) and ([block.x,block.y+block.size] not in newLocs) and (block.y<HEIGHT-block.size-block.spacing*5):
block.moveDown()
newNeeded = True
indR = int((block.y)//(block.size))
indC= int((block.x)//(block.size))
##newBlocks[indR][indC] = block
if block!=None:
newLocs.append([block.x,block.y])
indR = int((block.y)//(block.size))
indC = int((block.x)//(block.size))
if indR<3 :
if newBlocks[indR+1][indC].value==block.value:
if newBlocks[indR+1][indC].canAdd:
block1 = two(blockSize, block.value*2, indC, indR+1, False)
newBlocks[indR+1][indC] = block1
newLocs.remove([block.x,block.y])
newNeeded = True
else:
newBlocks[indR][indC] = block
else:
newBlocks[int(indR)][int(indC)] = block
else:
newBlocks[int(indR)][int(indC)] = block
if event.key==pygame.K_RIGHT or event.key==pygame.K_LEFT or event.key==pygame.K_UP or event.key==pygame.K_DOWN:
if newBlocks!=fillBoard():
blocks = newBlocks
if event.key == pygame.K_r:
playGame()
for r in range(len(blocks)):
for c in range(len(blocks[r])):
block = blocks[r][c]
if block != None:
block.draw()
block.canAdd = True
gameOver = checkForEnd(blocks)
pygame.display.update()
endGame(blocks)
playGame()