import pygame
import random
import copy
pygame.init()
WIDTH, HEIGHT = 300, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Tetris')
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
PURPLE = (255, 0, 255)
ORANGE = (255, 128, 0)
COLORS = [GREEN, RED, BLUE, YELLOW, CYAN, PURPLE, ORANGE]
BLOCKS = [
[[1, 1],
[1, 1]],
[[1, 1, 0],
[0, 1, 1]],
[[0, 1, 1],
[1, 1, 0]],
[[1, 1, 1],
[0, 1, 0]],
[[1, 1, 1, 1]],
[[1, 1, 1],
[0, 0, 1]],
[[1, 1, 1],
[1, 0, 0]]
]
class Block():
def __init__(self, x, y, block_type):
self.x = x
self.y = y
self.color = random.choice(COLORS)
self.block_type = block_type
self.shape = BLOCKS[block_type]
self.rotation = 0
def get_rotated_shape(self):
return [[self.shape[y][x] for y in range(len(self.shape))] for x in range(len(self.shape[0])-1, -1, -1)]
def get_coordinate_list(self):
coordinate_list = []
r = self.get_rotated_shape()
for x in range(len(r)):
for y in range(len(r[0])):
if r[x][y] == 1:
coordinate_list.append((x + self.x, y + self.y))
return coordinate_list
def can_move(self, direction, shape_matrix):
test_block = copy.deepcopy(self)
if direction == 'down':
test_block.y += 1
elif direction == 'left':
test_block.x -= 1
elif direction == 'right':
test_block.x += 1
for x,y in test_block.get_coordinate_list():
if x < 0 or x > len(shape_matrix) - 1 or y < 0 or y > len(shape_matrix[0]) - 1 or shape_matrix[x][y] == 1:
return False
return True
def move(self, direction):
if direction == 'down':
self.y += 1
elif direction == 'left':
self.x -= 1
elif direction == 'right':
self.x += 1
def rotate(self, shape_matrix):
self.rotation += 1
self.shape = self.get_rotated_shape()
while self.x < 0 or any([True for x,y in self.get_coordinate_list() if y < 0]):
self.x += 1
while self.x > len(shape_matrix) - 1 or any([True for x,y in self.get_coordinate_list() if y > len(shape_matrix[0]) - 1]):
self.x -= 1
while any([True for x,y in self.get_coordinate_list() if shape_matrix[x][y] == 1]):
if self.can_move('down', shape_matrix):
self.move('down')
else:
self.rotation -= 1
self.shape = self.get_rotated_shape()
break
def draw(self, window):
for x, y in self.get_coordinate_list():
pygame.draw.rect(window, self.color, ((x) * 20, (y) * 20, 20, 20))
pygame.draw.rect(window, (0, 0, 0), ((x) * 20, (y) * 20, 20, 20), 1)
def get_empty_matrix(w, h):
return [[0 for x in range(w)] for y in range(h)]
def draw_field(win, shape_matrix):
for x in range(len(shape_matrix)):
for y in range(len(shape_matrix[0])):
if shape_matrix[x][y] == 1:
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
pygame.draw.rect(win, color, ((x) * 20, (y) * 20, 20, 20))
def draw_score(win, score):
font = pygame.font.SysFont(None, 30)
score_text = font.render("Score: " + str(score), True, (255, 255, 255))
win.blit(score_text, (30, 30))
def remove_full_rows(shape_matrix):
rows_to_remove = []
for y in range(len(shape_matrix[0])):
if all([True if x[y] == 1 else False for x in shape_matrix]):
rows_to_remove.append(y)
for row in rows_to_remove:
for x in range(len(shape_matrix)):
for y in range(row, 0, -1):
shape_matrix[x][y] = shape_matrix[x][y-1]
shape_matrix[x][0] = 0
return len(rows_to_remove)
def play_game():
shape_matrix = get_empty_matrix(WIDTH//20, HEIGHT//20)
clock = pygame.time.Clock()
fps = 60
game_over = False
score = 0
active_block = Block(WIDTH//40, -2, random.randint(0, len(BLOCKS)-1))
fall_speed = 30
ground_timer = fall_speed
move_timer = 0
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and active_block.can_move('left', shape_matrix):
active_block.move('left')
move_timer = 0
elif event.key == pygame.K_RIGHT and active_block.can_move('right', shape_matrix):
active_block.move('right')
move_timer = 0
elif event.key == pygame.K_DOWN:
fall_speed = 3
elif event.key == pygame.K_UP:
active_block.rotate(shape_matrix)
move_timer += 1
if move_timer % 4 == 0:
fall_speed = 30
ground_timer += 1
if ground_timer > fall_speed and active_block.can_move('down', shape_matrix):
active_block.move('down')
ground_timer = 0
elif not active_block.can_move('down', shape_matrix):
for x, y in active_block.get_coordinate_list():
shape_matrix[x][y] = 1
full_rows = remove_full_rows(shape_matrix)
score += full_rows
active_block = Block(WIDTH//40, -2, random.randint(0, len(BLOCKS)-1))
if not active_block.can_move('down', shape_matrix):
game_over = True
WIN.fill((255, 255, 255))
active_block.draw(WIN)
draw_field(WIN, shape_matrix)
draw_score(WIN, score)
pygame.display.update()
clock.tick(fps)
play_game()
pygame.quit()