-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine2.py
159 lines (146 loc) · 5.14 KB
/
engine2.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
import pygame
import os
import random
import math
import time
from classes import GameWindow, BackGroundMusic, Block, GameStats, Grid, CollapsedBlocks
gameWindow = GameWindow()
clock = pygame.time.Clock()
window = gameWindow.setMode()
gameWindow.setCaption("Tetris - Aditya Chaturvedi")
# collapsedBlocks = renderBlocksForTesting(window)
backGroundMusic = BackGroundMusic()
backGroundMusic.playRandomMusic()
gameStats = GameStats()
grid = Grid()
blocksOnScreen = []
collapsedBlocks = CollapsedBlocks()
possibleBlockShapes = [
"cube-block",
"i",
"j",
"L",
"rs",
"s",
"t"
]
def renderBlocksForTesting(window):
global collapsedBlocks
cubeBlock = Block()
jBlock = Block()
lBlock = Block()
cubeBlock.shape = cubeBlock.gameWindow.loadImage("cube-block.png")
cubeBlock.blockShape = "cube"
cubeBlock.row = 21
cubeBlock.column = 2
cubeBlock.isFalling = False
jBlock.shape = jBlock.gameWindow.loadImage("j-block.png")
jBlock.blockShape = "j"
jBlock.isFalling = False
jBlock.row = 18
jBlock.column = 5
lBlock.shape = lBlock.gameWindow.loadImage("L-block.png")
lBlock.blockShape = "L"
lBlock.isFalling = False
lBlock.row = 21
lBlock.column = 6
collapsedBlocks.addToCollapsedBlocks(cubeBlock)
collapsedBlocks.addToCollapsedBlocks(jBlock)
collapsedBlocks.addToCollapsedBlocks(lBlock)
return collapsedBlocks
def checkForCollision(currentBlock, window):
global collapsedBlocks, grid
# if currentBlock.blockShape == "cube":
if currentBlock.checkCollisionForCube(collapsedBlocks, currentBlock):
collapsedBlocks.addToCollapsedBlocks(currentBlock)
grid.updateGrid(collapsedBlocks)
newBlock = spawnBlock(window)
return newBlock
else:
return currentBlock
# elif currentBlock == ""
def renderCollapsedBlocks(window):
global collapsedBlocks
if collapsedBlocks.collapsed == []:
return
else:
for block in collapsedBlocks.collapsed:
block.renderUpdatedPosition(window)
def renderDownWardMotionForAllBlocks():
global blocksOnScreen
for block in blocksOnScreen:
block.renderBlockDownwardMotion()
def getColumnUpperLimit(blockShape):
if blockShape == "cube-block":
return 13
elif blockShape == "i":
return 11
elif blockShape == "j" or blockShape == "s" or blockShape == "L" or blockShape == "t" or blockShape == "rs":
return 12
def spawnBlock(window):
global blocksOnScreen, possibleBlockShapes
block = Block()
blockShape = random.choice(possibleBlockShapes)
columnUpperLimit = getColumnUpperLimit(blockShape)
block.returnRandomBlockShape(blockShape, columnUpperLimit)
block.generateBlock(window)
blocksOnScreen.append(block)
return block
def generateBlock(window):
global blocksOnScreen, collapsedBlocks
if blocksOnScreen == []:
block = spawnBlock(window)
return block
else:
currentBlock = blocksOnScreen[len(blocksOnScreen) - 1]
if currentBlock.blockShape == "i":
if currentBlock.row == 22:
currentBlock.isFalling = False
collapsedBlocks.addToCollapsedBlocks(currentBlock)
newBlock = spawnBlock(window)
return newBlock
else:
return currentBlock
else:
if currentBlock.row == 21:
currentBlock.isFalling = False
collapsedBlocks.addToCollapsedBlocks(currentBlock)
newBlock = spawnBlock(window)
return newBlock
else:
return currentBlock
def gameLoop():
global collapsedBlocks, gameWindow, clock, window
pygame.init()
while gameWindow.windowRunning:
gameWindow.renderGameBackground(window)
gameWindow.renderGrid(window)
gameWindow.renderScoreBoard(window)
renderCollapsedBlocks(window)
currentBlock = generateBlock(window)
currentBlock.renderBlockDownwardMotion(window)
currentBlock.renderUpdatedPosition(window)
currentBlock = checkForCollision(currentBlock, window)
clock.tick(8)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameWindow.windowRunning = False
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
gameWindow.windowRunning = False
if event.key == pygame.K_LEFT and currentBlock.column > 0:
currentBlock.moveLeft()
if event.key == pygame.K_RIGHT and currentBlock.column < 12:
currentBlock.moveRight()
if event.key == pygame.K_m:
backGroundMusic.togglePauseUnPause()
if event.key == pygame.K_p:
if not gameStats.isPaused:
gameStats.isPaused = True
gameStats.pauseGame()
else:
gameStats.isPaused = False
gameStats.unpauseGame()
pygame.display.update()
clock.tick(60)