Загрузка данных


import pygame
import random

WIDTH = 300
HEIGHT = 600
BLOCK_SIZE = 30

COLS = WIDTH // BLOCK_SIZE
ROWS = HEIGHT // BLOCK_SIZE

BLACK = (0, 0, 0)
GRAY = (50, 50, 50)
CYAN = (0, 255, 255)
WHITE = (255, 255, 255)

pygame.init()

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Тетрис")

clock = pygame.time.Clock()

font = pygame.font.SysFont("Arial", 40)

SHAPES = [
    [[1, 1, 1, 1]],

    [[1, 1],
     [1, 1]],

    [[0, 1, 0],
     [1, 1, 1]],

    [[1, 0, 0],
     [1, 1, 1]],

    [[0, 0, 1],
     [1, 1, 1]]
]

grid = [[0 for _ in range(COLS)] for _ in range(ROWS)]


class Piece:
    def __init__(self):
        self.shape = random.choice(SHAPES)
        self.x = COLS // 2 - len(self.shape[0]) // 2
        self.y = 0

    def draw(self):
        for row_index, row in enumerate(self.shape):
            for col_index, cell in enumerate(row):

                if cell:
                    pygame.draw.rect(
                        screen,
                        CYAN,
                        (
                            (self.x + col_index) * BLOCK_SIZE,
                            (self.y + row_index) * BLOCK_SIZE,
                            BLOCK_SIZE,
                            BLOCK_SIZE
                        )
                    )

    def move(self, dx, dy):
        self.x += dx
        self.y += dy

    def rotate(self):
        self.shape = [list(row) for row in zip(*self.shape[::-1])]


def valid_move(piece, dx, dy):
    for row_index, row in enumerate(piece.shape):
        for col_index, cell in enumerate(row):

            if cell:
                new_x = piece.x + col_index + dx
                new_y = piece.y + row_index + dy

                if new_x < 0 or new_x >= COLS:
                    return False

                if new_y >= ROWS:
                    return False

                if new_y >= 0 and grid[new_y][new_x]:
                    return False

    return True


def lock_piece(piece):
    for row_index, row in enumerate(piece.shape):
        for col_index, cell in enumerate(row):

            if cell:
                grid[piece.y + row_index][piece.x + col_index] = 1


def clear_lines():
    global grid
    global score

    new_grid = [row for row in grid if not all(row)]

    lines_cleared = ROWS - len(new_grid)

    if lines_cleared == 1:
        score += 100

    elif lines_cleared == 2:
        score += 300

    elif lines_cleared == 3:
        score += 700

    elif lines_cleared >= 4:
        score += 1500

    for _ in range(lines_cleared):
        new_grid.insert(0, [0 for _ in range(COLS)])

    grid = new_grid


current_piece = Piece()

fall_time = 0
fall_speed = 500

game_over = False

score = 0

running = True

while running:

    screen.fill(BLACK)

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False

        if not game_over and event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT:
                if valid_move(current_piece, -1, 0):
                    current_piece.move(-1, 0)

            if event.key == pygame.K_RIGHT:
                if valid_move(current_piece, 1, 0):
                    current_piece.move(1, 0)

            if event.key == pygame.K_DOWN:
                if valid_move(current_piece, 0, 1):
                    current_piece.move(0, 1)

            if event.key == pygame.K_UP:

                old_shape = current_piece.shape

                current_piece.rotate()

                if not valid_move(current_piece, 0, 0):
                    current_piece.shape = old_shape

    if not game_over:

        fall_time += clock.get_rawtime()
        clock.tick()

        if fall_time > fall_speed:

            if valid_move(current_piece, 0, 1):
                current_piece.move(0, 1)

            else:
                lock_piece(current_piece)
                clear_lines()

                current_piece = Piece()

                if not valid_move(current_piece, 0, 0):
                    game_over = True

            fall_time = 0

    for x in range(COLS):
        for y in range(ROWS):

            pygame.draw.rect(
                screen,
                GRAY,
                (
                    x * BLOCK_SIZE,
                    y * BLOCK_SIZE,
                    BLOCK_SIZE,
                    BLOCK_SIZE
                ),
                1
            )

    for y in range(ROWS):
        for x in range(COLS):

            if grid[y][x]:

                pygame.draw.rect(
                    screen,
                    CYAN,
                    (
                        x * BLOCK_SIZE,
                        y * BLOCK_SIZE,
                        BLOCK_SIZE,
                        BLOCK_SIZE
                    )
                )

    if not game_over:
        current_piece.draw()

    score_text = font.render(f"Score: {score}", True, WHITE)

    screen.blit(score_text, (10, 10))

    if game_over:

        text = font.render("GAME OVER", True, WHITE)

        screen.blit(
            text,
            (
                WIDTH // 2 - text.get_width() // 2,
                HEIGHT // 2 - text.get_height() // 2
            )
        )

    pygame.display.update()

pygame.quit()