-
Notifications
You must be signed in to change notification settings - Fork 27
/
Tetris.py
493 lines (392 loc) · 14 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
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
import random
import pygame
"""
10 x 20 grid
play_height = 2 * play_width
tetriminos:
0 - S - green
1 - Z - red
2 - I - cyan
3 - O - yellow
4 - J - blue
5 - L - orange
6 - T - purple
"""
pygame.font.init()
# global variables
col = 10 # 10 columns
row = 20 # 20 rows
s_width = 800 # window width
s_height = 750 # window height
play_width = 300 # play window width; 300/10 = 30 width per block
play_height = 600 # play window height; 600/20 = 20 height per block
block_size = 30 # size of block
top_left_x = (s_width - play_width) // 2
top_left_y = s_height - play_height - 50
filepath = './highscore.txt'
fontpath = './arcade.ttf'
fontpath_mario = './mario.ttf'
# shapes formats
S = [['.....',
'.....',
'..00.',
'.00..',
'.....'],
['.....',
'..0..',
'..00.',
'...0.',
'.....']]
Z = [['.....',
'.....',
'.00..',
'..00.',
'.....'],
['.....',
'..0..',
'.00..',
'.0...',
'.....']]
I = [['.....',
'..0..',
'..0..',
'..0..',
'..0..'],
['.....',
'0000.',
'.....',
'.....',
'.....']]
O = [['.....',
'.....',
'.00..',
'.00..',
'.....']]
J = [['.....',
'.0...',
'.000.',
'.....',
'.....'],
['.....',
'..00.',
'..0..',
'..0..',
'.....'],
['.....',
'.....',
'.000.',
'...0.',
'.....'],
['.....',
'..0..',
'..0..',
'.00..',
'.....']]
L = [['.....',
'...0.',
'.000.',
'.....',
'.....'],
['.....',
'..0..',
'..0..',
'..00.',
'.....'],
['.....',
'.....',
'.000.',
'.0...',
'.....'],
['.....',
'.00..',
'..0..',
'..0..',
'.....']]
T = [['.....',
'..0..',
'.000.',
'.....',
'.....'],
['.....',
'..0..',
'..00.',
'..0..',
'.....'],
['.....',
'.....',
'.000.',
'..0..',
'.....'],
['.....',
'..0..',
'.00..',
'..0..',
'.....']]
# index represents the shape
shapes = [S, Z, I, O, J, L, T]
shape_colors = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0), (255, 165, 0), (0, 0, 255), (128, 0, 128)]
# class to represent each of the pieces
class Piece(object):
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
self.color = shape_colors[shapes.index(shape)] # choose color from the shape_color list
self.rotation = 0 # chooses the rotation according to index
# initialise the grid
def create_grid(locked_pos={}):
grid = [[(0, 0, 0) for x in range(col)] for y in range(row)] # grid represented rgb tuples
# locked_positions dictionary
# (x,y):(r,g,b)
for y in range(row):
for x in range(col):
if (x, y) in locked_pos:
color = locked_pos[
(x, y)] # get the value color (r,g,b) from the locked_positions dictionary using key (x,y)
grid[y][x] = color # set grid position to color
return grid
def convert_shape_format(piece):
positions = []
shape_format = piece.shape[piece.rotation % len(piece.shape)] # get the desired rotated shape from piece
'''
e.g.
['.....',
'.....',
'..00.',
'.00..',
'.....']
'''
for i, line in enumerate(shape_format): # i gives index; line gives string
row = list(line) # makes a list of char from string
for j, column in enumerate(row): # j gives index of char; column gives char
if column == '0':
positions.append((piece.x + j, piece.y + i))
for i, pos in enumerate(positions):
positions[i] = (pos[0] - 2, pos[1] - 4) # offset according to the input given with dot and zero
return positions
# checks if current position of piece in grid is valid
def valid_space(piece, grid):
# makes a 2D list of all the possible (x,y)
accepted_pos = [[(x, y) for x in range(col) if grid[y][x] == (0, 0, 0)] for y in range(row)]
# removes sub lists and puts (x,y) in one list; easier to search
accepted_pos = [x for item in accepted_pos for x in item]
formatted_shape = convert_shape_format(piece)
for pos in formatted_shape:
if pos not in accepted_pos:
if pos[1] >= 0:
return False
return True
# check if piece is out of board
def check_lost(positions):
for pos in positions:
x, y = pos
if y < 1:
return True
return False
# chooses a shape randomly from shapes list
def get_shape():
return Piece(5, 0, random.choice(shapes))
# draws text in the middle
def draw_text_middle(text, size, color, surface):
font = pygame.font.Font(fontpath, size, bold=False, italic=True)
label = font.render(text, 1, color)
surface.blit(label, (top_left_x + play_width/2 - (label.get_width()/2), top_left_y + play_height/2 - (label.get_height()/2)))
# draws the lines of the grid for the game
def draw_grid(surface):
r = g = b = 0
grid_color = (r, g, b)
for i in range(row):
# draw grey horizontal lines
pygame.draw.line(surface, grid_color, (top_left_x, top_left_y + i * block_size),
(top_left_x + play_width, top_left_y + i * block_size))
for j in range(col):
# draw grey vertical lines
pygame.draw.line(surface, grid_color, (top_left_x + j * block_size, top_left_y),
(top_left_x + j * block_size, top_left_y + play_height))
# clear a row when it is filled
def clear_rows(grid, locked):
# need to check if row is clear then shift every other row above down one
increment = 0
for i in range(len(grid) - 1, -1, -1): # start checking the grid backwards
grid_row = grid[i] # get the last row
if (0, 0, 0) not in grid_row: # if there are no empty spaces (i.e. black blocks)
increment += 1
# add positions to remove from locked
index = i # row index will be constant
for j in range(len(grid_row)):
try:
del locked[(j, i)] # delete every locked element in the bottom row
except ValueError:
continue
# shift every row one step down
# delete filled bottom row
# add another empty row on the top
# move down one step
if increment > 0:
# sort the locked list according to y value in (x,y) and then reverse
# reversed because otherwise the ones on the top will overwrite the lower ones
for key in sorted(list(locked), key=lambda a: a[1])[::-1]:
x, y = key
if y < index: # if the y value is above the removed index
new_key = (x, y + increment) # shift position to down
locked[new_key] = locked.pop(key)
return increment
# draws the upcoming piece
def draw_next_shape(piece, surface):
font = pygame.font.Font(fontpath, 30)
label = font.render('Next shape', 1, (255, 255, 255))
start_x = top_left_x + play_width + 50
start_y = top_left_y + (play_height / 2 - 100)
shape_format = piece.shape[piece.rotation % len(piece.shape)]
for i, line in enumerate(shape_format):
row = list(line)
for j, column in enumerate(row):
if column == '0':
pygame.draw.rect(surface, piece.color, (start_x + j*block_size, start_y + i*block_size, block_size, block_size), 0)
surface.blit(label, (start_x, start_y - 30))
# pygame.display.update()
# draws the content of the window
def draw_window(surface, grid, score=0, last_score=0):
surface.fill((0, 0, 0)) # fill the surface with black
pygame.font.init() # initialise font
font = pygame.font.Font(fontpath_mario, 65, bold=True)
label = font.render('TETRIS', 1, (255, 255, 255)) # initialise 'Tetris' text with white
surface.blit(label, ((top_left_x + play_width / 2) - (label.get_width() / 2), 30)) # put surface on the center of the window
# current score
font = pygame.font.Font(fontpath, 30)
label = font.render('SCORE ' + str(score) , 1, (255, 255, 255))
start_x = top_left_x + play_width + 50
start_y = top_left_y + (play_height / 2 - 100)
surface.blit(label, (start_x, start_y + 200))
# last score
label_hi = font.render('HIGHSCORE ' + str(last_score), 1, (255, 255, 255))
start_x_hi = top_left_x - 240
start_y_hi = top_left_y + 200
surface.blit(label_hi, (start_x_hi + 20, start_y_hi + 200))
# draw content of the grid
for i in range(row):
for j in range(col):
# pygame.draw.rect()
# draw a rectangle shape
# rect(Surface, color, Rect, width=0) -> Rect
pygame.draw.rect(surface, grid[i][j],
(top_left_x + j * block_size, top_left_y + i * block_size, block_size, block_size), 0)
# draw vertical and horizontal grid lines
draw_grid(surface)
# draw rectangular border around play area
border_color = (255, 255, 255)
pygame.draw.rect(surface, border_color, (top_left_x, top_left_y, play_width, play_height), 4)
# pygame.display.update()
# update the score txt file with high score
def update_score(new_score):
score = get_max_score()
with open(filepath, 'w') as file:
if new_score > score:
file.write(str(new_score))
else:
file.write(str(score))
# get the high score from the file
def get_max_score():
with open(filepath, 'r') as file:
lines = file.readlines() # reads all the lines and puts in a list
score = int(lines[0].strip()) # remove \n
return score
def main(window):
locked_positions = {}
create_grid(locked_positions)
change_piece = False
run = True
current_piece = get_shape()
next_piece = get_shape()
clock = pygame.time.Clock()
fall_time = 0
fall_speed = 0.35
level_time = 0
score = 0
last_score = get_max_score()
while run:
# need to constantly make new grid as locked positions always change
grid = create_grid(locked_positions)
# helps run the same on every computer
# add time since last tick() to fall_time
fall_time += clock.get_rawtime() # returns in milliseconds
level_time += clock.get_rawtime()
clock.tick() # updates clock
if level_time/1000 > 5: # make the difficulty harder every 10 seconds
level_time = 0
if fall_speed > 0.15: # until fall speed is 0.15
fall_speed -= 0.005
if fall_time / 1000 > fall_speed:
fall_time = 0
current_piece.y += 1
if not valid_space(current_piece, grid) and current_piece.y > 0:
current_piece.y -= 1
# since only checking for down - either reached bottom or hit another piece
# need to lock the piece position
# need to generate new piece
change_piece = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_piece.x -= 1 # move x position left
if not valid_space(current_piece, grid):
current_piece.x += 1
elif event.key == pygame.K_RIGHT:
current_piece.x += 1 # move x position right
if not valid_space(current_piece, grid):
current_piece.x -= 1
elif event.key == pygame.K_DOWN:
# move shape down
current_piece.y += 1
if not valid_space(current_piece, grid):
current_piece.y -= 1
elif event.key == pygame.K_UP:
# rotate shape
current_piece.rotation = current_piece.rotation + 1 % len(current_piece.shape)
if not valid_space(current_piece, grid):
current_piece.rotation = current_piece.rotation - 1 % len(current_piece.shape)
piece_pos = convert_shape_format(current_piece)
# draw the piece on the grid by giving color in the piece locations
for i in range(len(piece_pos)):
x, y = piece_pos[i]
if y >= 0:
grid[y][x] = current_piece.color
if change_piece: # if the piece is locked
for pos in piece_pos:
p = (pos[0], pos[1])
locked_positions[p] = current_piece.color # add the key and value in the dictionary
current_piece = next_piece
next_piece = get_shape()
change_piece = False
score += clear_rows(grid, locked_positions) * 10 # increment score by 10 for every row cleared
update_score(score)
if last_score < score:
last_score = score
draw_window(window, grid, score, last_score)
draw_next_shape(next_piece, window)
pygame.display.update()
if check_lost(locked_positions):
run = False
draw_text_middle('You Lost', 40, (255, 255, 255), window)
pygame.display.update()
pygame.time.delay(2000) # wait for 2 seconds
pygame.quit()
def main_menu(window):
run = True
while run:
draw_text_middle('Press any key to begin', 50, (255, 255, 255), window)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
main(window)
pygame.quit()
if __name__ == '__main__':
win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win) # start game