-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetris.py
356 lines (293 loc) · 10.5 KB
/
Tetris.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
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
import pygame
import time
import random
import sys
from pygame.locals import *
pygame.init()
class Declaration:
global Display_Width
Display_Width=1000
global Display_Height
Display_Height=800
global White
White=(255,255,255)
global Black
Black=(0,0,0)
global Green
Green=(0,155,0)
pygame.display.set_caption('Tetris')
global Font
Font=pygame.font.SysFont(None,30)
global SmallFont
SmallFont=pygame.font.SysFont(None,20)
global Board_Width
Board_Width=30
global Board_Height
Board_Height=32
global Display
Display=pygame.display.set_mode((Display_Width,Display_Height))
S_Shape = [[
'.....',
'..OO.',
'.OO..',
'.....'],
[
'..O..',
'..OO.',
'...O.',
'.....']]
Z_Shape = [[
'.....',
'.OO..',
'..OO.',
'.....'],
[
'..O..',
'.OO..',
'.O...',
'.....']]
I_Shape = [[
'..O..',
'..O..',
'..O..',
'..O..'],
[
'.....',
'OOOO.',
'.....',
'.....']]
O_Shape= [[
'.....',
'.OO..',
'.OO..',
'.....']]
L_Shape = [[
'...O.',
'.OOO.',
'.....',
'.....'],
[
'..O..',
'..O..',
'..OO.',
'.....'],
[
'.....',
'.OOO.',
'.O...',
'.....'],
[
'.OO..',
'..O..',
'..O..',
'.....']]
global Pieces
Pieces = {'S': S_Shape,
'Z': Z_Shape,
'L': L_Shape,
'I': I_Shape,
'O': O_Shape
}
global Block_Size
Block_Size=20
global TopXPos
TopXPos = (Display_Width-Block_Size*Board_Width)/2
global TopYPos
TopYPos = Display_Height-Block_Size*Board_Width-160
global Clock
Clock = pygame.time.Clock()
global Fps
Fps=8
class Board(Declaration):
def DrawBoard(self,Board):
pygame.draw.rect(Display, Green, (TopXPos-3,TopYPos-7, Board_Width*Block_Size+8,Board_Height*Block_Size+8), 5)
pygame.draw.rect(Display, Black,(TopXPos,TopYPos,Board_Width*Block_Size,Board_Height*Block_Size))
for x in range(Board_Width):
for y in range(Board_Height):
self.DrawBox(x, y, Board[x][y],None,None,1)
def CheckRowEmpty(self,Board,y):
for x in range(Board_Width):
if Board[x][y]=='.':
count+=1
if count==Board_Width:
return True
else:
return False
def CheckIsValid(self,Board,Piece,X_Shift,Y_Shift):
for x in range(4):
for y in range(4):
CheckInside=y+Piece['y']+Y_Shift
if CheckInside<0 :
continue
if Pieces[Piece['shape']][Piece['rotation']][y][x] == '.':
continue
X= x + Piece['x'] + X_Shift
Y=y + Piece['y'] + Y_Shift
if X<0 or X>=Board_Width or Y>=Board_Height :
return False
if Board[X][Y] != '.':
return False
return True
def CheckRowFull(self,Board,y):
count=0
for x in range(Board_Width):
if Board[x][y] != '.':
count+=1
if count==Board_Width:
return True
else:
return False
def FillPiecePos(self,Board, Piece):
for x in range(4):
for y in range(4):
if Pieces[Piece['shape']][Piece['rotation']][y][x] != '.':
Board[x + Piece['x']][y + Piece['y']]='0'
return
def RemoveLines(self,Board):
count = 0
y = Board_Height-1
while y >= 0:
val=self.CheckRowFull(Board,y)
if val:
for y in range(Board_Height):
for x in range(Board_Width):
Board[x][y] = Board[x][y-1]
for x in range(Board_Width):
Board[x][0] = '.'
count += 1
else:
y=y-1
return count
class Block(Declaration):
def DrawBox(self,Box_X_Pos, Box_Y_Pos,color,x,y,Status):
if color=='.':
return
if Status==1:
x= TopXPos + (Box_X_Pos) * Block_Size
y= TopYPos + (Box_Y_Pos) * Block_Size
pygame.draw.rect(Display, White, (x + 1, y + 1, Block_Size - 1, Block_Size - 1))
pygame.draw.rect(Display, White, (x + 1, y + 1, Block_Size- 4, Block_Size - 4))
def Rotate(self,Board,FallingPiece):
FallingPiece['rotation'] = (FallingPiece['rotation'] + 1) % len(Pieces[FallingPiece['shape']])
if not self.CheckIsValid(Board,FallingPiece,0,0):
self.BackRotate(Board,FallingPiece)
return
def BackRotate(self,Board,FallingPiece):
FallingPiece['rotation'] = (FallingPiece['rotation'] - 1) % len(Pieces[FallingPiece['shape']])
return
def MoveLeft(self,FallingPiece):
FallingPiece['x']=FallingPiece['x']-1
return
def MoveRight(self,FallingPiece):
FallingPiece['x']=FallingPiece['x']+1
return
def DrawPiece(self,Piece):
DrawPiece = Pieces[Piece['shape']][Piece['rotation']]
Pixelx, Pixely = (TopXPos+ (Piece['x'] * Block_Size)), (TopYPos+ (Piece['y']* Block_Size))
for x in range(4):
for y in range(4):
if DrawPiece[y][x]!='.':
self.DrawBox(None,None,White,Pixelx +(x * Block_Size), Pixely + (y * Block_Size),0)
class Gamplay(Block,Board,Declaration):
def SelectPiece(self):
Shape = random.choice(list(Pieces.keys()))
Piece = {'shape': Shape,
'rotation': random.randint(0, len(Pieces[Shape]) - 1),
'x' : 13,
'y' : -1,
}
return Piece
def text_objects(self,text,color):
textSurface=Font.render(text,True,color)
return textSurface,textSurface.get_rect()
def message_to_screen(self,check,msg,color):
textSurf,textRect=self.text_objects(msg,color)
if check==1:
textRect.center=(100,100)
elif check==2:
textRect.center=(100,140)
Display.blit(textSurf,textRect)
def DisplayScore_Level(self,Score,Level):
pygame.draw.rect(Display,Black,[42,80,120,70])
pygame.draw.rect(Display,Black,[100,140,70,70])
self.message_to_screen(1,'Score : '+str(Score),White)
self.message_to_screen(2,'Level : '+str(Level),White)
def UpdateScore(self,Score,value):
Score+=int(value)
return Score
def CheckForKeyUp(self,event,MoveLeft,MoveRight):
if event.type==KEYUP and event.key==K_ESCAPE:
pygame.quit()
sys.exit()
if event.type==KEYUP:
if event.key==K_d :
MoveRight=False
elif event.key==K_a:
MoveLeft=False
return MoveLeft,MoveRight
def CheckForKeyDown(self,Board,FallingPiece,event,MoveLeft,MoveRight):
if event.type==KEYDOWN :
if event.key==K_d and self.CheckIsValid(Board, FallingPiece,1,0):
FallingPiece['x']=FallingPiece['x']+1
MoveRight=True
MoveLeft=False
elif event.key==K_a and self.CheckIsValid(Board,FallingPiece,-1,0):
FallingPiece['x']=FallingPiece['x']-1
MoveLeft=True
MoveRight=False
elif event.key==K_w:
self.Rotate(Board,FallingPiece)
elif event.key==K_SPACE:
MoveRight=False
MoveLeft=False
for i in range(1, 20):
if not self.CheckIsValid(Board, FallingPiece,0,i):
break
FallingPiece['y'] = FallingPiece['y']+i-1
return MoveLeft,MoveRight
def StartGame(self):
global Fps
Board=[['.' for x in range(Board_Height)]for y in range(Board_Width)]
MoveRight = False
MoveLeft = False
Score = 0
Level=1
GameRun=True
FallingPiece = self.SelectPiece()
while GameRun:
if FallingPiece== None :
FallingPiece=self.SelectPiece()
if not self.CheckIsValid(Board,FallingPiece,0,0):
GameRun=False
return
for event in pygame.event.get(QUIT):
pygame.quit()
sys.exit()
for event in pygame.event.get():
MoveLeft,MoveRight = self.CheckForKeyUp(event,MoveLeft,MoveRight)
MoveLeft,MoveRight = self.CheckForKeyDown(Board,FallingPiece,event,MoveLeft,MoveRight)
if MoveRight and self.CheckIsValid(Board,FallingPiece,1,0):
self.MoveRight(FallingPiece)
elif MoveLeft and self.CheckIsValid(Board,FallingPiece,-1,0):
self.MoveLeft(FallingPiece)
if self.CheckIsValid(Board, FallingPiece,0,1):
FallingPiece['y']=FallingPiece['y']+1
else:
self.FillPiecePos(Board, FallingPiece)
no_of_rows_del=self.RemoveLines(Board)
self.Updatescore=(Score,no_of_rows_del)
if no_of_rows_del > 0:
Score=self.UpdateScore(Score,100*no_of_rows_del)
else:
Score=self.UpdateScore(Score,10)
Level=int(Score/100)+1
Fps=Fps+0.5*(Level-1)
FallingPiece = None
self.DrawBoard(Board)
self.DisplayScore_Level(Score, Level)
if FallingPiece != None:
self.DrawPiece(FallingPiece)
pygame.display.update()
Clock.tick(Fps)
ob=Gamplay()
ob.StartGame()