-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris.py
389 lines (301 loc) · 9.75 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
import random, pygame
pygame.init()
pygame.font.init()
screen_width = 800
screen_height = 700
play_width = 300
play_height = 600
block_size = 30 # each little square
background_colour = (0, 0, 0)
# coordinates of top left point
top_x = (screen_width - play_width)/2
top_y = (screen_height - play_height)
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..',
'.....']]
shapes = [S, Z, I, O, J, L, T]
shape_colours = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0), (255, 165, 0), (0, 0, 255), (128, 0, 128)]
class Piece:
''' Shape object with position coordinates, colour and rotation '''
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
self.colour = shape_colours[shapes.index(shape)]
self.rotation = 0
def create_grid(locked_positions={}):
''' Creates grid object, colors squares depending if taken or empty '''
grid = [[(0, 0, 0) for x in range(10)] for y in range(20)]
for i in range(len(grid)):
for j in range(len(grid[i])):
if (j, i) in locked_positions:
grid[i][j] = locked_positions[(j, i)]
return grid
def get_shape(shapes):
''' Returns random shape at center of grid '''
return Piece(5, 0, random.choice(shapes))
def draw_text(surface, message, colour, size):
''' Draws given message at center of screen '''
f = pygame.font.SysFont('arial', size, bold=True)
label = f.render(message, 1, colour)
surface.blit(label, (top_x + play_width/2 - label.get_width()/2, top_y + play_height/2 - label.get_height()/2 - 50))
def draw_grid(surface, grid):
''' Draws the grey gridlines '''
for i in range(len(grid)):
pygame.draw.line(surface, (128, 128, 128), (top_x, top_y + i * block_size), (top_x + play_width, top_y + i * block_size))
for j in range(len(grid[i])):
pygame.draw.line(surface, (128, 128, 128), (top_x + j * block_size, top_y), (top_x + j * block_size, top_y + play_height))
def clear_rows(grid, locked_positions={}):
''' Clears complete rows and shits above rows downwards '''
increment = 0
ind = 0
for i in range(len(grid) -1, -1, -1):
if (0, 0, 0) not in grid[i]:
ind = i
increment += 1
for j in range(len(grid[i])):
try:
del locked_positions[(j, i)]
except:
continue
if increment > 0:
for key in sorted(list(locked_positions), key = lambda x: x[1])[::-1]:
x, y = key
if y < ind:
new_key = (x, y + increment)
locked_positions[new_key] = locked_positions.pop(key)
return increment
def draw_next(surface, piece):
''' Shows user the next shape '''
f = pygame.font.SysFont('arial', 40)
label = f.render('Next shape', 1, (255, 255, 255))
xx = top_x + play_width + 50
yy = top_y + play_height/2 - 100
form = piece.shape[piece.rotation % len(piece.shape)] # gives you needed sublist
for i, line in enumerate(form):
row = list(line)
for j, column in enumerate(row):
if column == '0':
pygame.draw.rect(surface, piece.colour, (xx + j * block_size, yy + i * block_size, block_size, block_size), 0)
surface.blit(label, (xx - 10, yy - 30))
def draw_window(surface, grid, score=0):
''' Displays the window, grid, surrounding red rectangle, game title and score label '''
surface.fill((0, 0, 0))
f = pygame.font.SysFont('arial', 70)
label = f.render('Tetris', 1, (255, 255, 255))
surface.blit(label, (top_x + play_width/2 - label.get_width()/2, top_y - 70))
f2 = pygame.font.SysFont('arial', 20)
label_score = f2.render('Score: %d' %(score), 1, (255, 255, 255))
surface.blit(label_score, (top_x - 150, screen_height - 100))
for i in range(len(grid)):
for j in range(len(grid[i])):
pygame.draw.rect(surface, grid[i][j], (top_x + j * block_size, top_y + i * block_size, block_size, block_size), 0)
draw_grid(surface, grid)
pygame.draw.rect(surface, (255, 0, 0), (top_x, top_y, play_width, play_height), 1)
def convert_shape_format(piece):
''' Converts shape list into positions '''
positions = []
form = piece.shape[piece.rotation % len(piece.shape)]
for i, line in enumerate(form):
row = list(line)
for j, column in enumerate(row):
if column == '0':
positions.append((piece.x + j, piece.y + i))
for i, position in enumerate(positions):
positions[i] = (position[0] - 2, position[1] - 4)
return positions
def valid_space(piece, grid):
''' Checks if piece is going to a valid space in grid '''
valid = [[(j, i) for j in range(10) if grid[i][j] == (0, 0, 0)] for i in range(20)]
valid = [j for sublist in valid for j in sublist]
formatted = convert_shape_format(piece)
for position in formatted:
if position not in valid:
if position[1] >= 0:
return False
return True
def check_lost(positions):
''' Checks if the piece is above the screen '''
for position in positions:
x, y = position
if y <= 0:
return True
return False
def main():
''' Main game structure '''
locked_positions = {}
create_grid(locked_positions)
current_piece = get_shape(shapes)
next_piece = get_shape(shapes)
clock = pygame.time.Clock()
fall_time = 0
fall_speed = 0.27
level_time = 0
score = 0
change_piece = False
running = True
while running:
grid = create_grid(locked_positions)
fall_time += clock.get_rawtime()
level_time += clock.get_rawtime()
clock.tick()
if level_time/1000 > 5:
level_time = 0
if fall_speed > 0.12:
fall_speed -= 0.001
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
change_piece = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_piece.x -= 1
if not valid_space(current_piece, grid):
current_piece.x += 1
elif event.key == pygame.K_RIGHT:
current_piece.x += 1
if not valid_space(current_piece, grid):
current_piece.x -= 1
elif event.key == pygame.K_DOWN:
current_piece.y += 1
if not valid_space(current_piece, grid):
current_piece.y -= 1
elif event.key == pygame.K_UP:
current_piece.rotation += 1
shape_position = convert_shape_format(current_piece)
for i in range(len(shape_position)):
x, y = shape_position[i]
if y >= 0:
grid[y][x] = current_piece.colour
if change_piece:
for position in shape_position:
p = (position[0], position[1])
locked_positions[p] = current_piece.colour
current_piece = next_piece
next_piece = get_shape(shapes)
increment = clear_rows(grid, locked_positions)
score += 10 * increment
change_piece = False
draw_window(screen, grid, score)
draw_next(screen, next_piece)
pygame.display.update()
if check_lost(locked_positions):
running = False
break
draw_text(screen, 'You Lost!', (255, 255, 255), 40)
pygame.display.update()
pygame.time.delay(2000)
def main_menu():
''' Start of program '''
run = True
while run:
screen.fill(background_colour)
draw_text(screen, 'Press any key to play', (255, 255, 255), 60)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.quit()
if event.type == pygame.KEYDOWN:
main()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Tetris')
main_menu()