Skip to content

Commit 422651e

Browse files
authored
Commited new progress
Added some music, and some more minor features.
1 parent 3c99c82 commit 422651e

File tree

4 files changed

+78
-44
lines changed

4 files changed

+78
-44
lines changed

controls_final.png

73.3 KB
Loading

drop.wav

31 KB
Binary file not shown.

main_music.wav

344 KB
Binary file not shown.

tetris.py

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
score=0
1212

13+
# Sounds which can be accessesd by other functions also
14+
dropSound=None
15+
mainMusic=None
16+
1317
def printStylish(sentence):
1418
for i in range(len(sentence)):
1519
print(sentence[i], end="")
@@ -43,14 +47,18 @@ def displayBoardPG(display_surface, bg_img, board):
4347
display_surface.fill("#000000")
4448
global startTime
4549
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))
4654
color = colorsys.hsv_to_rgb(360*((time.time()-startTime) % 10)/1000, 1, 1)
4755
color = tuple([255*x for x in color])
4856

4957
pygame.draw.line(display_surface, color, (8, 8), (8, 808), 2)
5058
pygame.draw.line(display_surface, color, (508, 8), (8, 8), 2)
5159
pygame.draw.line(display_surface, color, (508, 8), (508, 808), 2)
5260
pygame.draw.line(display_surface, color, (8, 808), (508, 808), 2)
53-
61+
5462
colorList = [(110, 255, 214), (255, 110, 176), (244, 255, 110),
5563
(0, 112, 255), (116, 255, 110), (110, 0, 255), (255,255,255)]
5664
# Drawing the board
@@ -63,12 +71,13 @@ def displayBoardPG(display_surface, bg_img, board):
6371
font = pygame.font.SysFont("Lucida Sans TypeWriter Regular", 36)
6472
text = font.render(scoreStr, True, color)
6573
textRect=text.get_rect()
66-
textRect.center=(605,100)
74+
k=50
75+
textRect.center=(605+k,100)
6776
#pygame.draw.rect
6877
font2 = pygame.font.SysFont("Lucida Sans TypeWriter Regular", 48)
6978
text2 = font.render("Score", True, (255,255,255))
7079
textRect2=text2.get_rect()
71-
textRect2.center=(605,30)
80+
textRect2.center=(605+k,30)
7281
display_surface.blit(text, textRect)
7382
display_surface.blit(text2, textRect2)
7483

@@ -84,15 +93,16 @@ def displayBoardBorderPG(display_surface):
8493
pygame.draw.line(display_surface, color, (508, 8), (508, 808), 2)
8594
pygame.draw.line(display_surface, color, (8, 808), (508, 808), 2)
8695
font = pygame.font.SysFont("Lucida Sans TypeWriter Regular", 36)
87-
96+
k=50
8897
scoreStr=f"{score:08d}"
8998
text = font.render(scoreStr, True, color)
9099
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)
96106
display_surface.blit(text, textRect)
97107

98108
pygame.display.flip()
@@ -111,9 +121,11 @@ def rotateRight(blockMatrix, blockMatrixPosition, game_board):
111121
newBlockMatrix[2][2]=blockMatrix[0][2]
112122
for i in range(len(newBlockMatrix)):
113123
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):
115125
return blockMatrix
116126
return newBlockMatrix
127+
else:
128+
return blockMatrix
117129

118130
def generateBlock():
119131
x = [1, 2, 3, 4, 5, 6, 7]
@@ -215,15 +227,16 @@ def handleLineClears(game_board):
215227
return True
216228

217229
def gameOver(display_surface):
230+
pygame.mixer.stop()
218231
print("Game got over")
219232
global score
220233
font = pygame.font.SysFont("Times new Roman", 36)
221234
text = font.render("GAME OVER", True, (255,255,255))
222235
text2 = font.render("YOUR SCORE WAS " + str(score), True, (255,255,255))
223236
textRect = text.get_rect()
224-
textRect.center= (300,382)
237+
textRect.center= (400,382)
225238
textRect2 = text2.get_rect()
226-
textRect2.center= (300,418)
239+
textRect2.center= (400,418)
227240
display_surface.fill("#000000")
228241
display_surface.blit(text, textRect)
229242
display_surface.blit(text2, textRect2)
@@ -236,13 +249,18 @@ def gameOver(display_surface):
236249

237250
def mainGame(board_dimensions):
238251
pygame.init()
239-
global startTime
252+
global startTime, dropSound
240253
startTime = time.time()
241-
display_surface = pygame.display.set_mode((700, 816))
254+
display_surface = pygame.display.set_mode((800, 816))
242255
image = pygame.image.load('bg_final.jpg')
243256
image = pygame.transform.scale(image, (600, 816))
244257

245258
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")
246264

247265
game_board = createBoard(board_dimensions) # Creates the board in an array
248266
blockMatrix = generateBlock() # 3x3 block, which acts as the present
@@ -255,48 +273,64 @@ def mainGame(board_dimensions):
255273
needGeneration = True
256274
positionUpdated = False
257275
scoreUpdated=False
276+
277+
mainMusic.play(-1)
258278
while not done:
279+
if(paused==True):
280+
pygame.mixer.pause()
281+
else:
282+
pygame.mixer.unpause()
259283
for event in pygame.event.get():
260284
if event.type == pygame.QUIT:
261285
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")
267289

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:
286295
if(checkDownwardCollision(blockMatrix, blockMatrixPosition, game_board) == False):
287296
blockMatrixPosition[0] += 1
288297
positionUpdated = True
289298
else:
299+
290300
game_board = flattenBoard(
291301
blockMatrix, blockMatrixPosition, game_board)
292302
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
300334

301335

302336
if(needGeneration == True):

0 commit comments

Comments
 (0)