-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtetris.py
More file actions
178 lines (151 loc) · 5.49 KB
/
tetris.py
File metadata and controls
178 lines (151 loc) · 5.49 KB
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
import pygame
import random
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
COLORS = [
(0, 255, 255),
(255, 255, 0),
(128, 0, 128),
(0, 255, 0),
(255, 0, 0),
(0, 0, 255),
(255, 127, 0)
]
# Define game settings
BLOCK_SIZE = 30
ROWS = 20
COLS = 10
WIDTH = COLS * BLOCK_SIZE
HEIGHT = ROWS * BLOCK_SIZE
# Define tetromino shapes
SHAPES = [
[[1, 1, 1],
[0, 1, 0]],
[[0, 2, 2],
[2, 2, 0]],
[[3, 3, 0],
[0, 3, 3]],
[[4, 0, 0],
[4, 4, 4]],
[[0, 0, 5],
[5, 5, 5]],
[[6, 6, 6, 6]],
[[7, 7],
[7, 7]]
]
class Tetromino:
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
self.color = COLORS[shape]
self.rotation = 0
def rotate(self):
self.rotation = (self.rotation + 1) % 4
def move(self, dx, dy):
self.x += dx
self.y += dy
def create_grid():
grid = [[BLACK] * COLS for _ in range(ROWS)]
return grid
def draw_grid(screen, grid):
for y in range(ROWS):
for x in range(COLS):
pygame.draw.rect(screen, grid[y][x], (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
for y in range(ROWS):
pygame.draw.line(screen, GRAY, (0, y * BLOCK_SIZE), (WIDTH, y * BLOCK_SIZE))
for x in range(COLS):
pygame.draw.line(screen, GRAY, (x * BLOCK_SIZE, 0), (x * BLOCK_SIZE, HEIGHT))
def draw_tetromino(screen, tetromino):
shape_matrix = tetromino.shape[tetromino.rotation % len(tetromino.shape)]
for y, row in enumerate(shape_matrix):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(screen, tetromino.color, (tetromino.x * BLOCK_SIZE + x * BLOCK_SIZE,
tetromino.y * BLOCK_SIZE + y * BLOCK_SIZE,
BLOCK_SIZE, BLOCK_SIZE), 0)
def check_collision(grid, tetromino):
shape_matrix = tetromino.shape[tetromino.rotation % len(tetromino.shape)]
for y, row in enumerate(shape_matrix):
for x, cell in enumerate(row):
try:
if cell and grid[tetromino.y + y][tetromino.x + x] != BLACK:
return True
except IndexError:
return True
return False
def merge_tetromino(grid, tetromino):
shape_matrix = tetromino.shape[tetromino.rotation % len(tetromino.shape)]
for y, row in enumerate(shape_matrix):
for x, cell in enumerate(row):
if cell:
grid[tetromino.y + y][tetromino.x + x] = tetromino.color
def remove_completed_lines(grid):
completed_lines = 0
for y in range(ROWS - 1, -1, -1):
if BLACK not in grid[y]:
completed_lines += 1
del grid[y]
grid.insert(0, [BLACK] * COLS)
return completed_lines
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tetris")
clock = pygame.time.Clock()
grid = create_grid()
current_tetromino = Tetromino(COLS // 2, 0, random.choice(SHAPES))
next_tetromino = Tetromino(COLS // 2, 0, random.choice(SHAPES))
fall_speed = 0.5
fall_time = 0
score = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_tetromino.move(-1, 0)
if check_collision(grid, current_tetromino):
current_tetromino.move(1, 0)
elif event.key == pygame.K_RIGHT:
current_tetromino.move(1, 0)
if check_collision(grid, current_tetromino):
current_tetromino.move(-1, 0)
elif event.key == pygame.K_DOWN:
current_tetromino.move(0, 1)
if check_collision(grid, current_tetromino):
current_tetromino.move(0, -1)
merge_tetromino(grid, current_tetromino)
completed_lines = remove_completed_lines(grid)
score += completed_lines * 10
current_tetromino = next_tetromino
next_tetromino = Tetromino(COLS // 2, 0, random.choice(SHAPES))
elif event.key == pygame.K_UP:
current_tetromino.rotate()
if check_collision(grid, current_tetromino):
current_tetromino.rotate()
current_tetromino.rotate()
current_tetromino.rotate()
fall_time += clock.get_rawtime()
clock.tick()
if fall_time / 1000 >= fall_speed:
fall_time = 0
current_tetromino.move(0, 1)
if check_collision(grid, current_tetromino):
current_tetromino.move(0, -1)
merge_tetromino(grid, current_tetromino)
completed_lines = remove_completed_lines(grid)
score += completed_lines * 10
current_tetromino = next_tetromino
next_tetromino = Tetromino(COLS // 2, 0, random.choice(SHAPES))
screen.fill(BLACK)
draw_grid(screen, grid)
draw_tetromino(screen, current_tetromino)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()