-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris.py
509 lines (431 loc) · 14.4 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# import pygame
# import random
# pygame.init()
# screen_width = 300 # Adjust as needed
# screen_height = 600
# screen = pygame.display.set_mode((screen_width, screen_height))
# pygame.display.set_caption('Tetris')
# grid_width = 10
# grid_height = 20
# block_size = 30 # Size of each block in pixels
# grid = [[(0, 0, 0) for _ in range(grid_width)] for _ in range(grid_height)]
# # Define the dimensions of the playing area
# play_width = grid_width * block_size # Width of the playing area
# play_height = grid_height * block_size # Height of the playing area
# top_left_x = (screen_width - play_width) // 2 # Center the grid horizontally
# top_left_y = screen_height - play_height # Position it at the bottom of the screen
# #s tetromino
# S = [['.....',
# '.....',
# '..OO.',
# '.OO..',
# '.....'],
# ['.....',
# '..O..',
# '..OO.',
# '...O.',
# '.....']]
# #z tetromino
# Z = [['.....',
# '.....',
# '.OO..',
# '..OO.',
# '.....'],
# ['.....',
# '..O..',
# '.OO..',
# '.O...',
# '.....']]
# #i tetromino
# I = [['.....',
# '.....',
# 'OOOO.',
# '.....',
# '.....'],
# ['..O..',
# '..O..',
# '..O..',
# '..O..',
# '.....']]
# #o tetromino
# O = [['.....',
# '.....',
# '.OO..',
# '.OO..',
# '.....']]
# #j tetromino
# J = [['.....',
# '.O...',
# '.OOO.',
# '.....',
# '.....'],
# ['.....',
# '..OO.',
# '..O..',
# '..O..',
# '.....'],
# ['.....',
# '.....',
# '.OOO.',
# '...O.',
# '.....'],
# ['.....',
# '..O..',
# '..O..',
# '.OO..',
# '.....']]
# #l tetromino
# L = [['.....',
# '...O.',
# '.OOO.',
# '.....',
# '.....'],
# ['.....',
# '..O..',
# '..O..',
# '..OO.',
# '.....'],
# ['.....',
# '.....',
# '.OOO.',
# '.O...',
# '.....'],
# ['.....',
# '.OO..',
# '..O..',
# '..O..',
# '.....']]
# #t tetromino
# T = [['.....',
# '..O..',
# '.OOO.',
# '.....',
# '.....'],
# ['.....',
# '..O..',
# '..OO.',
# '..O..',
# '.....'],
# ['.....',
# '.....',
# '.OOO.',
# '..O..',
# '.....'],
# ['.....',
# '..O..',
# '.OO..',
# '..O..',
# '.....']]
# shapes = [S, Z, I, O, J, L, T]
# colors = [(0, 255, 0), # Green
# (255, 0, 0), # Red
# (0, 255, 255), # Cyan
# (255, 255, 0), # Yellow
# (0, 0, 255), # Blue
# (255, 165, 0), # Orange
# (128, 0, 128)] # Purple
# class Piece(object):
# def __init__(self, x, y, shape):
# self.x = x
# self.y = y
# self.shape = shape
# self.color = random.choice(colors) # Assign random color
# self.rotation = 0
# def move_left(piece):
# piece.x -= 1
# def move_right(piece):
# piece.x += 1
# def move_down(piece):
# piece.y += 1
# def rotate(piece):
# piece.rotation = (piece.rotation + 1) % len(piece.shape)
# def valid_space(piece, grid):
# accepted_positions = [...]
# formatted = convert_shape_format(piece)
# for pos in formatted:
# if pos not in accepted_positions:
# if pos[1] > -1:
# return False
# return True
# def convert_shape_format(piece):
# positions = []
# format = piece.shape[piece.rotation % len(piece.shape)]
# for i, line in enumerate(format):
# row = list(line)
# for j, column in enumerate(row):
# if column == 'O':
# positions.append((piece.x + j, piece.y + i))
# return positions
# def clear_rows(grid, locked_positions):
# inc = 0
# for i in range(len(grid)-1, -1, -1):
# row = grid[i]
# if (0, 0, 0) not in row:
# inc += 1
# ind = i
# for j in range(len(row)):
# try:
# del locked_positions[(j, i)]
# except:
# continue
# if inc > 0:
# for key in sorted(list(locked_positions), key=lambda x: x[1])[::-1]:
# x, y = key
# if y < ind:
# newKey = (x, y + inc)
# locked_positions[newKey] = locked_positions.pop(key)
# def check_lost(positions):
# for pos in positions:
# x, y = pos
# if y < 1:
# return True
# return False
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# run = False
# if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_LEFT:
# move_left(current_piece)
# elif event.key == pygame.K_RIGHT:
# move_right(current_piece)
# elif event.key == pygame.K_DOWN:
# move_down(current_piece)
# elif event.key == pygame.K_UP:
# rotate(current_piece)
# def main():
# locked_positions = {}
# grid = create_grid(locked_positions)
# change_piece = False
# run = True
# current_piece = get_shape()
# next_piece = get_shape()
# clock = pygame.time.Clock()
# fall_time = 0
# while run:
# fall_speed = 0.27
# grid = create_grid(locked_positions)
# fall_time += clock.get_rawtime()
# clock.tick()
# if fall_time/1000 > fall_speed:
# fall_time = 0
# move_down(current_piece)
# if not valid_space(current_piece, grid) and current_piece.y > 0:
# current_piece.y -= 1
# change_piece = True
# # User input handling code here
# shape_pos = convert_shape_format(current_piece)
# for i in range(len(shape_pos)):
# x, y = shape_pos[i]
# if y > -1:
# grid[y][x] = current_piece.color
# if change_piece:
# for pos in shape_pos:
# p = (pos[0], pos[1])
# locked_positions[p] = current_piece.color
# current_piece = next_piece
# next_piece = get_shape()
# change_piece = False
# clear_rows(grid, locked_positions)
# draw_window(screen, grid)
# pygame.display.update()
# if check_lost(locked_positions):
# run = False
# pygame.display.quit()
# def draw_window(surface, grid):
# surface.fill((0, 0, 0))
# for i in range(len(grid)):
# for j in range(len(grid[i])):
# pygame.draw.rect(surface, grid[i][j],
# (top_left_x + j*block_size, top_left_y + i*block_size, block_size, block_size), 0)
# pygame.draw.rect(surface, (255, 0, 0), (top_left_x, top_left_y, play_width, play_height), 5)
# def create_grid(locked_positions={}):
# grid = [[(0, 0, 0) for _ in range(grid_width)] for _ in range(grid_height)]
# 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():
# return Piece(5, 0, random.choice(shapes))
# font = pygame.font.SysFont('comicsans', 60)
# label = font.render('Tetris', 1, (255,255,255))
# if __name__ == '__main__':
# main()
import pygame
import random
pygame.init()
# Set up the display
screen_width = 300
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Tetris')
# Define the grid size
grid_width = 10
grid_height = 20
block_size = 30 # Size of each block in pixels
# Define grid
grid = [[(0, 0, 0) for _ in range(grid_width)] for _ in range(grid_height)]
# Define dimensions of the playing area
play_width = grid_width * block_size
play_height = grid_height * block_size
top_left_x = (screen_width - play_width) // 2
top_left_y = screen_height - play_height
# Tetromino shapes
S = [['.....', '.....', '..OO.', '.OO..', '.....'], ['.....', '..O..', '..OO.', '...O.', '.....']]
Z = [['.....', '.....', '.OO..', '..OO.', '.....'], ['.....', '..O..', '.OO..', '.O...', '.....']]
I = [['.....', '.....', 'OOOO.', '.....', '.....'], ['..O..', '..O..', '..O..', '..O..', '.....']]
O = [['.....', '.....', '.OO..', '.OO..', '.....']]
J = [['.....', '.O...', '.OOO.', '.....', '.....'], ['.....', '..OO.', '..O..', '..O..', '.....'],
['.....', '.....', '.OOO.', '...O.', '.....'], ['.....', '..O..', '..O..', '.OO..', '.....']]
L = [['.....', '...O.', '.OOO.', '.....', '.....'], ['.....', '..O..', '..O..', '..OO.', '.....'],
['.....', '.....', '.OOO.', '.O...', '.....'], ['.....', '.OO..', '..O..', '..O..', '.....']]
T = [['.....', '..O..', '.OOO.', '.....', '.....'], ['.....', '..O..', '..OO.', '..O..', '.....'],
['.....', '.....', '.OOO.', '..O..', '.....'], ['.....', '..O..', '.OO..', '..O..', '.....']]
shapes = [S, Z, I, O, J, L, T]
colors = [(0, 255, 0), (255, 0, 0), (0, 255, 255), (255, 255, 0),
(0, 0, 255), (255, 165, 0), (128, 0, 128)] # Tetromino colors
class Piece(object):
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
self.color = random.choice(colors) # Assign random color
self.rotation = 0
def move_left(piece):
piece.x -= 1
def move_right(piece):
piece.x += 1
def move_down(piece):
piece.y += 1
def rotate(piece):
piece.rotation = (piece.rotation + 1) % len(piece.shape)
def valid_space(piece, grid):
accepted_positions = [(x, y) for y in range(grid_height) for x in range(grid_width) if grid[y][x] == (0, 0, 0)]
formatted = convert_shape_format(piece)
for pos in formatted:
if pos not in accepted_positions:
if pos[1] > -1: # Prevent piece from going above the grid
return False
return True
def convert_shape_format(piece):
positions = []
format = piece.shape[piece.rotation % len(piece.shape)]
for i, line in enumerate(format):
row = list(line)
for j, column in enumerate(row):
if column == 'O':
positions.append((piece.x + j, piece.y + i))
return positions
def clear_rows(grid, locked_positions):
inc = 0
for i in range(len(grid)-1, -1, -1):
row = grid[i]
if (0, 0, 0) not in row: # Check if the row is full
inc += 1
ind = i
for j in range(len(row)):
try:
del locked_positions[(j, i)]
except:
continue
if inc > 0: # Move the rows down
for key in sorted(list(locked_positions), key=lambda x: x[1])[::-1]:
x, y = key
if y < ind:
newKey = (x, y + inc)
locked_positions[newKey] = locked_positions.pop(key)
def check_lost(positions):
for pos in positions:
x, y = pos
if y < 1: # If any piece is above the grid
return True
return False
def draw_window(surface, grid, score=0):
surface.fill((0, 0, 0))
# Draw grid
for i in range(len(grid)):
for j in range(len(grid[i])):
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 border
pygame.draw.rect(surface, (255, 0, 0), (top_left_x, top_left_y, play_width, play_height), 5)
# Draw Score
font = pygame.font.SysFont('comicsans', 30)
score_label = font.render(f'Score: {score}', 1, (255, 255, 255))
surface.blit(score_label, (top_left_x + 10, top_left_y + 10))
def create_grid(locked_positions={}):
grid = [[(0, 0, 0) for _ in range(grid_width)] for _ in range(grid_height)]
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():
return Piece(5, 0, random.choice(shapes))
def main():
locked_positions = {} # Store (x,y):(r,g,b)
grid = create_grid(locked_positions)
change_piece = False
run = True
current_piece = get_shape()
next_piece = get_shape()
clock = pygame.time.Clock()
fall_time = 0
score = 0
while run:
fall_speed = 0.27
grid = create_grid(locked_positions)
fall_time += clock.get_time() # Updated to get_time()
clock.tick(60) # Set frame rate to 60 FPS
if fall_time / 1000 > fall_speed:
fall_time = 0
move_down(current_piece)
if not valid_space(current_piece, grid) and current_piece.y > 0:
current_piece.y -= 1
change_piece = True
# User input handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
move_left(current_piece)
if not valid_space(current_piece, grid):
current_piece.x += 1 # Undo movement
elif event.key == pygame.K_RIGHT:
move_right(current_piece)
if not valid_space(current_piece, grid):
current_piece.x -= 1 # Undo movement
elif event.key == pygame.K_DOWN:
move_down(current_piece)
if not valid_space(current_piece, grid):
current_piece.y -= 1 # Undo movement
elif event.key == pygame.K_UP:
rotate(current_piece)
if not valid_space(current_piece, grid):
current_piece.rotation -= 1 # Undo rotation
shape_pos = convert_shape_format(current_piece)
for i in range(len(shape_pos)):
x, y = shape_pos[i]
if y > -1: # Only draw pieces that are on the screen
grid[y][x] = current_piece.color
if change_piece:
for pos in shape_pos:
p = (pos[0], pos[1])
locked_positions[p] = current_piece.color
current_piece = next_piece
next_piece = get_shape()
change_piece = False
clear_rows(grid, locked_positions)
draw_window(screen, grid, score)
pygame.display.update()
if check_lost(locked_positions):
run = False
pygame.quit()
if __name__ == "__main__":
main()
#Things to look at:
#no borders on tiles
#no score
#no level speed increase
#no end condition