10
10
11
11
score = 0
12
12
13
+ # Sounds which can be accessesd by other functions also
14
+ dropSound = None
15
+ mainMusic = None
16
+
13
17
def printStylish (sentence ):
14
18
for i in range (len (sentence )):
15
19
print (sentence [i ], end = "" )
@@ -43,14 +47,18 @@ def displayBoardPG(display_surface, bg_img, board):
43
47
display_surface .fill ("#000000" )
44
48
global startTime
45
49
display_surface .blit (bg_img , (0 , 0 ))
50
+
51
+ controlsImg = pygame .image .load ('controls_final.png' )
52
+ controlsImg = pygame .transform .scale (controlsImg , (300 , 600 ))
53
+ display_surface .blit (controlsImg , (510 , 200 ))
46
54
color = colorsys .hsv_to_rgb (360 * ((time .time ()- startTime ) % 10 )/ 1000 , 1 , 1 )
47
55
color = tuple ([255 * x for x in color ])
48
56
49
57
pygame .draw .line (display_surface , color , (8 , 8 ), (8 , 808 ), 2 )
50
58
pygame .draw .line (display_surface , color , (508 , 8 ), (8 , 8 ), 2 )
51
59
pygame .draw .line (display_surface , color , (508 , 8 ), (508 , 808 ), 2 )
52
60
pygame .draw .line (display_surface , color , (8 , 808 ), (508 , 808 ), 2 )
53
-
61
+
54
62
colorList = [(110 , 255 , 214 ), (255 , 110 , 176 ), (244 , 255 , 110 ),
55
63
(0 , 112 , 255 ), (116 , 255 , 110 ), (110 , 0 , 255 ), (255 ,255 ,255 )]
56
64
# Drawing the board
@@ -63,12 +71,13 @@ def displayBoardPG(display_surface, bg_img, board):
63
71
font = pygame .font .SysFont ("Lucida Sans TypeWriter Regular" , 36 )
64
72
text = font .render (scoreStr , True , color )
65
73
textRect = text .get_rect ()
66
- textRect .center = (605 ,100 )
74
+ k = 50
75
+ textRect .center = (605 + k ,100 )
67
76
#pygame.draw.rect
68
77
font2 = pygame .font .SysFont ("Lucida Sans TypeWriter Regular" , 48 )
69
78
text2 = font .render ("Score" , True , (255 ,255 ,255 ))
70
79
textRect2 = text2 .get_rect ()
71
- textRect2 .center = (605 ,30 )
80
+ textRect2 .center = (605 + k ,30 )
72
81
display_surface .blit (text , textRect )
73
82
display_surface .blit (text2 , textRect2 )
74
83
@@ -84,15 +93,16 @@ def displayBoardBorderPG(display_surface):
84
93
pygame .draw .line (display_surface , color , (508 , 8 ), (508 , 808 ), 2 )
85
94
pygame .draw .line (display_surface , color , (8 , 808 ), (508 , 808 ), 2 )
86
95
font = pygame .font .SysFont ("Lucida Sans TypeWriter Regular" , 36 )
87
-
96
+ k = 50
88
97
scoreStr = f"{ score :08d} "
89
98
text = font .render (scoreStr , True , color )
90
99
textRect = text .get_rect ()
91
- textRect .center = (605 ,100 )
92
- pygame .draw .line (display_surface , color , (514 , 85 ), (514 , 115 ), 2 )
93
- pygame .draw .line (display_surface , color , (514 , 85 ), (694 , 85 ), 2 )
94
- pygame .draw .line (display_surface , color , (514 , 115 ), (694 , 115 ), 2 )
95
- pygame .draw .line (display_surface , color , (694 , 85 ), (694 ,115 ), 2 )
100
+ textRect .center = (605 + k ,100 )
101
+
102
+ pygame .draw .line (display_surface , color , (514 + k , 85 ), (514 + k , 115 ), 2 )
103
+ pygame .draw .line (display_surface , color , (514 + k , 85 ), (694 + k , 85 ), 2 )
104
+ pygame .draw .line (display_surface , color , (514 + k , 115 ), (694 + k , 115 ), 2 )
105
+ pygame .draw .line (display_surface , color , (694 + k , 85 ), (694 + k ,115 ), 2 )
96
106
display_surface .blit (text , textRect )
97
107
98
108
pygame .display .flip ()
@@ -111,9 +121,11 @@ def rotateRight(blockMatrix, blockMatrixPosition, game_board):
111
121
newBlockMatrix [2 ][2 ]= blockMatrix [0 ][2 ]
112
122
for i in range (len (newBlockMatrix )):
113
123
for j in range (len (newBlockMatrix [i ])):
114
- if (newBlockMatrix [i ][j ]!= 0 and game_board [blockMatrixPosition [0 ]+ i ][blockMatrixPosition [1 ]+ j ]!= 0 ):
124
+ if (newBlockMatrix [i ][j ]!= 0 and game_board [blockMatrixPosition [0 ]+ i ][( blockMatrixPosition [1 ]+ j ) % 10 ]!= 0 ):
115
125
return blockMatrix
116
126
return newBlockMatrix
127
+ else :
128
+ return blockMatrix
117
129
118
130
def generateBlock ():
119
131
x = [1 , 2 , 3 , 4 , 5 , 6 , 7 ]
@@ -215,15 +227,16 @@ def handleLineClears(game_board):
215
227
return True
216
228
217
229
def gameOver (display_surface ):
230
+ pygame .mixer .stop ()
218
231
print ("Game got over" )
219
232
global score
220
233
font = pygame .font .SysFont ("Times new Roman" , 36 )
221
234
text = font .render ("GAME OVER" , True , (255 ,255 ,255 ))
222
235
text2 = font .render ("YOUR SCORE WAS " + str (score ), True , (255 ,255 ,255 ))
223
236
textRect = text .get_rect ()
224
- textRect .center = (300 ,382 )
237
+ textRect .center = (400 ,382 )
225
238
textRect2 = text2 .get_rect ()
226
- textRect2 .center = (300 ,418 )
239
+ textRect2 .center = (400 ,418 )
227
240
display_surface .fill ("#000000" )
228
241
display_surface .blit (text , textRect )
229
242
display_surface .blit (text2 , textRect2 )
@@ -236,13 +249,18 @@ def gameOver(display_surface):
236
249
237
250
def mainGame (board_dimensions ):
238
251
pygame .init ()
239
- global startTime
252
+ global startTime , dropSound
240
253
startTime = time .time ()
241
- display_surface = pygame .display .set_mode ((700 , 816 ))
254
+ display_surface = pygame .display .set_mode ((800 , 816 ))
242
255
image = pygame .image .load ('bg_final.jpg' )
243
256
image = pygame .transform .scale (image , (600 , 816 ))
244
257
245
258
display_surface .blit (image , (0 , 0 ))
259
+ pygame .mixer .init ()
260
+ dropSound = pygame .mixer .Sound ("drop.wav" )
261
+ paused = True
262
+
263
+ mainMusic = pygame .mixer .Sound ("main_music.wav" )
246
264
247
265
game_board = createBoard (board_dimensions ) # Creates the board in an array
248
266
blockMatrix = generateBlock () # 3x3 block, which acts as the present
@@ -255,48 +273,64 @@ def mainGame(board_dimensions):
255
273
needGeneration = True
256
274
positionUpdated = False
257
275
scoreUpdated = False
276
+
277
+ mainMusic .play (- 1 )
258
278
while not done :
279
+ if (paused == True ):
280
+ pygame .mixer .pause ()
281
+ else :
282
+ pygame .mixer .unpause ()
259
283
for event in pygame .event .get ():
260
284
if event .type == pygame .QUIT :
261
285
done = True
262
- elif event .type == fallEvent :
263
- if (checkDownwardCollision (blockMatrix , blockMatrixPosition , game_board ) == False ):
264
- blockMatrixPosition [0 ] += 1
265
- positionUpdated = True
266
- else :
286
+ if event .type == pygame .KEYDOWN :
287
+ if event .key == pygame .K_c :
288
+ #print("C key pressed")
267
289
268
- game_board = flattenBoard (
269
- blockMatrix , blockMatrixPosition , game_board )
270
- needGeneration = True
271
- scoreUpdate = handleLineClears (game_board )
272
- if (scoreUpdated ):
273
- displayBoardPG (display_surface , image , flattenBoard (
274
- blockMatrix , blockMatrixPosition , game_board ))
275
-
276
- elif event .type == pygame .KEYDOWN :
277
- if event .key == pygame .K_RIGHT :
278
- if (checkRightwardCollision (blockMatrix , blockMatrixPosition , game_board )== False ):
279
- blockMatrixPosition [1 ] += 1
280
- positionUpdated = True
281
- elif event .key == pygame .K_LEFT :
282
- if (checkLeftwardCollision (blockMatrix , blockMatrixPosition , game_board )== False ):
283
- blockMatrixPosition [1 ] -= 1
284
- positionUpdated = True
285
- elif event .key == pygame .K_DOWN :
290
+ paused = not paused
291
+
292
+ #print(paused)
293
+ if (not paused ):
294
+ if event .type == fallEvent :
286
295
if (checkDownwardCollision (blockMatrix , blockMatrixPosition , game_board ) == False ):
287
296
blockMatrixPosition [0 ] += 1
288
297
positionUpdated = True
289
298
else :
299
+
290
300
game_board = flattenBoard (
291
301
blockMatrix , blockMatrixPosition , game_board )
292
302
needGeneration = True
293
- elif event .key == pygame .K_SPACE :
294
- while (checkDownwardCollision (blockMatrix , blockMatrixPosition , game_board )== False ):
295
- blockMatrixPosition [0 ] += 1
296
- positionUpdated = True
297
- elif event .key == pygame .K_r :
298
- blockMatrix = rotateRight (blockMatrix , blockMatrixPosition , game_board )
299
- positionUpdated = True
303
+ scoreUpdate = handleLineClears (game_board )
304
+
305
+ if (scoreUpdated ):
306
+ displayBoardPG (display_surface , image , flattenBoard (
307
+ blockMatrix , blockMatrixPosition , game_board ))
308
+
309
+ elif event .type == pygame .KEYDOWN :
310
+ if event .key == pygame .K_RIGHT :
311
+ if (checkRightwardCollision (blockMatrix , blockMatrixPosition , game_board )== False ):
312
+ blockMatrixPosition [1 ] += 1
313
+ positionUpdated = True
314
+ elif event .key == pygame .K_LEFT :
315
+ if (checkLeftwardCollision (blockMatrix , blockMatrixPosition , game_board )== False ):
316
+ blockMatrixPosition [1 ] -= 1
317
+ positionUpdated = True
318
+ elif event .key == pygame .K_DOWN :
319
+ if (checkDownwardCollision (blockMatrix , blockMatrixPosition , game_board ) == False ):
320
+ blockMatrixPosition [0 ] += 1
321
+ positionUpdated = True
322
+ else :
323
+ game_board = flattenBoard (
324
+ blockMatrix , blockMatrixPosition , game_board )
325
+ needGeneration = True
326
+ elif event .key == pygame .K_SPACE :
327
+ while (checkDownwardCollision (blockMatrix , blockMatrixPosition , game_board )== False ):
328
+ blockMatrixPosition [0 ] += 1
329
+ positionUpdated = True
330
+ dropSound .play (0 )
331
+ elif event .key == pygame .K_r or event .key == pygame .K_UP :
332
+ blockMatrix = rotateRight (blockMatrix , blockMatrixPosition , game_board )
333
+ positionUpdated = True
300
334
301
335
302
336
if (needGeneration == True ):
0 commit comments