-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2048.py
More file actions
614 lines (491 loc) · 19.1 KB
/
2048.py
File metadata and controls
614 lines (491 loc) · 19.1 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#Importing required modules
import pygame
import random
#Initializing pygame modules
pygame.init()
#Initializing font module
pygame.font.init()
war=[]
#Creating fonts
myfont = pygame.font.SysFont('Comic Sans MS', 50)
myfont_win=pygame.font.SysFont('Times New Roman', 80)
#Defining RGB values for colours
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = ( 255, 0, 0)
BLUE = ( 0, 0, 255)
GOLD=( 230, 215, 0)
screen = pygame.display.set_mode((800,600)) #Initialzing a screen for display
pygame.display.set_caption('2048')
clock = pygame.time.Clock()
#Lists to store game matrices
board = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
board_copy = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
board_undo = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
#Initializing variables
continue_after_win=0
draw_state=0
score=0
try:
highest_score=int(open('highscore.txt').read())
#TODO : "highscore.txt is a file in the current directory containing
# the highest score recorded. This is not erased when game is
# closed. Open the file and store the value of highest score in
# variable called highest_score
except:
highest_score=0
#To set initial state of game matrix and score.
def initial():
global score
arr = [2,4]
score=0
textsurface = myfont.render(str(score), 1, (255,255,255))
screen.blit(textsurface,(700,100))
i=0
while i < 2:
board[random.randint(0,3)][random.randint(0,3)]=random.choice(arr)
i=i+1
"""Fill in two random cells of the board with either 2 or 4.
Reset current score to zero and any other variables you might think
needs resetting."""
pass
#To draw the game matrix, scores and required buttons
def draw():
global highest_score,score
mouse=pygame.mouse.get_pos()#To get the mouse cursor position
click=pygame.mouse.get_pressed()#To get the state of the mouse buttons
#Drawing the game matrix
for i in range (0,5):
pygame.draw.line(screen, BLUE, (200,100*(i+1) ), (600, 100*(i+1)))
pygame.draw.line(screen, BLUE, (200+100*i,100 ), (200+100*i,500 ))
#Displaying the name of game, 'Score' and 'High Score'
textsurface = myfont.render('2048', False, WHITE)
screen.blit(textsurface,(50,50))
textsurface = myfont.render('Score', False, GREEN)
screen.blit(textsurface,(630,50))
textsurface = myfont.render('High', False, RED)
screen.blit(textsurface,(630,200))
textsurface = myfont.render('score', False, RED)
screen.blit(textsurface,(630,250))
if mouse[0] < 175 and mouse[0] > 50 and mouse[1] < 250 and mouse[1] > 200:
pygame.draw.rect(screen, GREEN,(50,200,125,50))#Change button colour to green
if click[0] == 1: #If button is clicked
reset() # Reset the matrix by calling reset() function
else:#Display colour of button as red
pygame.draw.rect(screen,RED,(50,200,125,50))
#Display 'RESET' on the button
textsurface = myfont.render('RESET', False, WHITE)
screen.blit(textsurface,(55,210))
if mouse[0] < 175 and mouse[0] > 50 and mouse[1] < 350 and mouse[1] > 300:
pygame.draw.rect(screen, GREEN,(50,300,125,50)) #Change button colour to green
if click[0]:#If button is clicked
undo() #Undo the prevoius move by calling undo() function
else:#Display colour of button as blue.
pygame.draw.rect(screen,BLUE,(50,300,125,50))
#Display 'UNDO' on the button
textsurface = myfont.render('UNDO', False, WHITE)
screen.blit(textsurface,(60,310))
for i in range (0,4):
for j in range (0,4):
if not board[i][j] == 0:
if board[i][j] < 1000:
textsurface = myfont.render(str(board[i][j]), False, WHITE)
screen.blit(textsurface,((j*100)+200+35,(i+1)*100+35))
else:
textsurface = myfont.render(str(board[i][j]), False, WHITE)
screen.blit(textsurface,((j*100)+200+15,(i+1)*100+35))
#TODO : Write code to display the score(Consult PyGame documentation for displaying Text)
textsurface = myfont.render(str(score), 1, (255,255,255))
screen.blit(textsurface,(660,130))
#TODO : Write code to display the highest score till date(Consult PyGame documentation for displaying Text)
textsurface= myfont.render(str(highest_score), 1, (255,255,255))
screen.blit(textsurface,(630,330))
#TODO : Write highest score to file(Consult PyGame documentation for displaying
if score > highest_score:
highest_score=score
target = open('highscore.txt','w')
target.write(str(highest_score))
target.close()
def loser():
screen.fill(BLACK)
textsurface = myfont.render('No matter what, you', False,(255,255,255))
screen.blit(textsurface,(50,50))
textsurface = myfont.render('can never lose,try again', False,(255,255,255))
screen.blit(textsurface,(130,100))
global quit
#TODO : Consult PyGame documentation and retrieve mouse position and mouse action state.
# Store them in variables mouse and click.
#Fill the screen to black
mouse=pygame.mouse.get_pos()#To get the mouse cursor position
click=pygame.mouse.get_pressed()#To get the state of the mouse buttons
#Display 'You won!' on screen
#TODO : Display a win message on screen. Get as creative as you want :)
if mouse[0] < 375 and mouse[0] > 200 and mouse[1] < 450 and mouse[1] > 400:#If mouse hovers over button
pygame.draw.rect(screen, GREEN,(200, 400,125,50))#Change button colour to green
if click[0]==1:#If button is clicked
quit=True #Change quit to true, thereby quitting the game
else:#Display colour of button as red
pygame.draw.rect(screen,RED,(200,400,125,50))
#Display 'QUIT' on the button
textsurface = myfont.render('QUIT', False, WHITE)
screen.blit(textsurface,(220,410))
if mouse[0] < 175 and mouse[0] > 50 and mouse[1] < 250 and mouse[1] > 200:
pygame.draw.rect(screen, GREEN,(50,200,125,50))#Change button colour to green
if click[0] == 1: #If button is clicked
reset() # Reset the matrix by calling reset() function
else:#Display colour of button as red
pygame.draw.rect(screen,RED,(50,200,125,50))
#Display 'RESET' on the button
textsurface = myfont.render('RESET', False, WHITE)
screen.blit(textsurface,(55,210))
def check():
adjacent_horizontal = adjacent_vertical = all_cells = 0
global draw_state
#TODO : Check if any two adjacent horizontal elements are equal
for i in range(0,4):
for j in range(0,3):
if board[i][j]==board[i][j+1]:
adjacent_horizontal=1
#TODO : Check if any two adjacent vertical elements are equal
for i in range(0,4):
for j in range(0,3):
if board[j][i]==board[j+1][i]:
adjacent_vertical=1
#TODO : Check if all cells are filled
for i in range(0,4):
for j in range(0,4):
if board[i][j]== 0:
all_cells=1
if adjacent_horizontal == 0 and adjacent_vertical == 0 and all_cells == 0:
screen.fill(BLACK)
textsurface = myfont.render("Game Over !!! Now GTFO...", False, RED)
screen.blit(textsurface,(300,300))
draw_state= -1
def undo():
"""
Restore board by undoing the last move.
"""
global board_undo,board
for i in range(4):
board[i]=board_undo[i]
pass
def right():
"""
Squash rows of board left when right key is pressed.
Guidelines
----------
You might have to break up this task into a "squash" and "replace" operation
The squash operation would accept a list and compute the new list after
values are squashed in that direction. Once that's computed for every row,
replace the rows with the new computed ones. Don't forget to add
a random element to the board once you're done with row-wise squash.
"""
global board_copy,board_undo,board,score
for i in range(4):
for j in range(4):
board_copy[i][j]=board[i][j]
board_undo[i][j]=board[i][j]
for i in range(4):
j=3
while j > 0:
k=3
while k > 0:
if board_copy[i][k]==0:
board_copy[i][k]=board_copy[i][k-1]
board_copy[i][k-1]=0
k=k-1
j=j-1
for i in range(4):
j=3
while j>0:
if board_copy[i][j]==board_copy[i][j-1]:
board_copy[i][j]=board_copy[i][j]+board_copy[i][j-1]
board_copy[i][j-1]=0
score=score+board_copy[i][j]
j=j-1
for i in range(4):
j=3
while j>0:
if board_copy[i][j]==0:
board_copy[i][j]=board_copy[i][j-1]
board_copy[i][j-1]=0
j=j-1
for i in range(4):
for j in range(4):
board[i][j]=board_copy[i][j]
add()
pass
def left():
"""
Squash rows of board left when left key is pressed.
Guidelines
----------
You might have to break up this task into a "squash" and "replace" operation
The squash operation would accept a list and compute the new list after
values are squashed in that direction. Once that's computed for every row,
replace the rows with the new computed ones. Don't forget to add
a random element to the board once you're done with row-wise squash.
"""
global board_copy,board_undo,board,score
for i in range(4):
for j in range(4):
board_copy[i][j]=board[i][j]
board_undo[i][j]=board[i][j]
for i in range(4):
j=0
while j < 3:
k=0
while k < 3:
if board_copy[i][k]==0:
board_copy[i][k]=board_copy[i][k+1]
board_copy[i][k+1]=0
k=k+1
j=j+1
for i in range(4):
j=0
while j<3:
if board_copy[i][j]==board_copy[i][j+1]:
board_copy[i][j]=board_copy[i][j]+board_copy[i][j+1]
board_copy[i][j+1]=0
score=score+ board_copy[i][j]
j=j+1
for i in range(4):
j=0
while j<3:
if board_copy[i][j]==0:
board_copy[i][j]=board_copy[i][j+1]
board_copy[i][j+1]=0
j=j+1
for i in range(4):
for j in range(4):
board[i][j]=board_copy[i][j]
add()
pass
def up():
"""
Squash columns of board up when up key is pressed.
Guidelines
----------
You might have to break up this task into a "squash" and "replace" operation
The squash operation would accept a list and compute the new list after
values are squashed in that direction. Once that's computed for every column,
replace the columns with the new computed ones. Don't forget to add
a random element to the board once you're done with columnwise squash.
"""
global board_copy,board_undo,board,score
for i in range(4):
for j in range(4):
board_copy[i][j]=board[i][j]
board_undo[i][j]=board[i][j]
for i in range(4):
j=3
while j > 0:
k=0
while k < 3:
if board_copy[k][i]==0:
board_copy[k][i]=board_copy[k+1][i]
board_copy[k+1][i]=0
k=k+1
j=j-1
for i in range(4):
j=0
while j<3:
if board_copy[j][i]==board_copy[j+1][i]:
board_copy[j][i]=board_copy[j][i]+board_copy[j+1][i]
board_copy[j+1][i]=0
score=score + board_copy[j][i]
j=j+1
for i in range(4):
j=0
while j<3:
if board_copy[j][i]==0:
board_copy[j][i]=board_copy[j+1][i]
board_copy[j+1][i]=0
j=j+1
for i in range(4):
for j in range(4):
board[i][j]=board_copy[i][j]
add()
pass
def down():
"""
Move columns of board down when down key is pressed.
Guidelines
----------
You might have to break up this task into a "squash" and "replace" operation
The squash operation would accept a list and compute the new list after
values are squashed in that direction. Once that's computed for every column,
replace the columns with the new computed ones. Don't forget to add
a random element to the board once you're done with columnwise squash.
"""
global board_copy,board_undo,board,score
for i in range(4):
for j in range(4):
board_copy[i][j]=board[i][j]
board_undo[i][j]=board[i][j]
for i in range(4):
j=3
while j > 0:
k=3
while k > 0:
if board_copy[k][i]==0:
board_copy[k][i]=board_copy[k-1][i]
board_copy[k-1][i]=0
k=k-1
j=j-1
for i in range(4):
j=3
while j>0:
if board_copy[j][i]==board_copy[j-1][i]:
board_copy[j][i]=board_copy[j][i]+board_copy[j-1][i]
board_copy[j-1][i]=0
score=score + board_copy[j][i]
j=j-1
for i in range(4):
j=3
while j>0:
if board_copy[j][i]==0:
board_copy[j][i]=board_copy[j-1][i]
board_copy[j-1][i]=0
j=j-1
for i in range(4):
for j in range(4):
board[i][j]=board_copy[i][j]
add()
pass
#To add element to game matrix
def add():
"""
Fills up one empty cell of the board
Guidelines
----------
The cell must be filled up randomly with either 2 or 4. Whether you
should favour 2 or 4 more is upto you. Experiment with both.
"""
global war
arr=[2,4]
war=[0,1,2,3,4,5,6,7,8,9,10,11,12]
count=0
i=0
while i<1:
t=random.choice(war)
q=int(t/4)
p=(t%4)
if board[q][p] == 0 :
board[q][p]=random.choice(arr)
i=i+1
else:
count=count+1
war.remove(t)
if count > 11:
check()
break
pass
#To reset the game matrix to initial state
def reset():
globals()['board'] = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
initial()
"""
Reset the board and restart the game.
"""
pass
def welcome_message():
"""
Display a welcome message at the start of the game
"""
screen.fill(BLACK)
textsurface = myfont.render('Welcome to the best thing', False,(255,255,255))
screen.blit(textsurface,(50,50))
textsurface = myfont.render('I have coded so far', False,(255,255,255))
screen.blit(textsurface,(130,100))
global quit, draw_state
#TODO : Consult PyGame documentation and retrieve mouse position and mouse action state.
# Store them in variables 'mouse' and 'click'.
click = pygame.mouse.get_pressed()
mouse = pygame.mouse.get_pos()
#Fill the screen black
#Display '2048'
textsurface=myfont_win.render('2048', False, GOLD)
screen.blit(textsurface, (300, 200))
if mouse[0] < 450 and mouse[0] > 330 and mouse[1] < 450 and mouse[1] > 400:#If mouse hovers over button
pygame.draw.rect(screen, GREEN,(310,400,130,50))#Change button colour to green
if click[0]:#If button is clicked
draw_state += 1
draw() #Draw the game matrix, thereby startng the game
else:#Display colour of button as blue
pygame.draw.rect(screen,BLUE,(310,400,130,50))
#Display 'PLAY' on the button
textsurface = myfont.render('PLAY', False, WHITE)
screen.blit(textsurface,(330,410))
def win_message():
"""
Display a victory message when 2048 is achieved
"""
screen.fill(BLACK)
textsurface = myfont.render('Completed the best thing', False,(255,255,255))
screen.blit(textsurface,(50,50))
textsurface = myfont.render('I have coded so far ,YOU WON', False,(255,255,255))
screen.blit(textsurface,(130,100))
global quit, continue_after_win
#TODO : Consult PyGame documentation and retrieve mouse position and mouse action state.
# Store them in variables mouse and click.
click = pygame.mouse.get_pressed()
mouse = pygame.mouse.get_pos()
#Fill the screen to black
#Display 'You won!' on screen
#TODO : Display a win message on screen. Get as creative as you want :)
if mouse[0] < 375 and mouse[0] > 200 and mouse[1] < 450 and mouse[1] > 400:#If mouse hovers over button
pygame.draw.rect(screen, GREEN,(200, 400,125,50))#Change button colour to green
if click[0]==1:#If button is clicked
quit=True #Change quit to true, thereby quitting the game
else:#Display colour of button as red
pygame.draw.rect(screen,RED,(200,400,125,50))
#Display 'QUIT' on the button
textsurface = myfont.render('QUIT', False, WHITE)
screen.blit(textsurface,(220,410))
if mouse[0] < 650 and mouse[0] > 430 and mouse[1] < 450 and mouse[1] > 400:#If mouse hovers over button
pygame.draw.rect(screen, GREEN,(410,400,220,50))#Change button colour to green
if click[0]==1:#If button is clicked
continue_after_win = 1
draw()#Draw the game matrix, therby continuing the game
else:#Display colour of button as blue
pygame.draw.rect(screen,BLUE,(410,400,220,50))
#Display 'CONTINUE' on the button
textsurface = myfont.render('CONTINUE', False, WHITE)
screen.blit(textsurface,(430,410))
initial() #Calling initial() function, thereby beginning the game
welcome_message() #Dispaying welcome message
quit = False #Initialzing variable
while not quit: #While game is running
screen.fill((50, 50, 50)) #Fill screen to grey
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
left()
if event.key == pygame.K_RIGHT:
right()
if event.key == pygame.K_UP:
up()
if event.key == pygame.K_DOWN:
down()
#TODO : Look up keyboard event in PyGame, there should be four more
# conditionals here corresponding to left, right, up and down key.
#
# Below is one such event. The quit event.
if event.type == pygame.QUIT:#If user quits game
quit = True
if draw_state == 0:#If game has just begun
welcome_message()#Display welcome message
if draw_state > 0:#If game has already begun
draw()#Draw game matrix
if draw_state < 0:
loser()
#If user achieves 2048
if continue_after_win == 0:
for i in range(0,4):
for j in range(0,4):
if board[i][j]==2048: #Checking if any element in game matrix is equal to 2048
win_message()#Displaying victory message by calling win_message() function
pygame.display.update() #Update portions of the screen for software displays
pygame.quit() #Uninitialize all pygame modules