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


import pygame
import random

pygame.init()

WIDTH = 300
HEIGHT = 600
BLOCK_SIZE = 30

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

BLACK = (15, 15, 25)
GRAY = (60, 70, 100)

CYAN = (0, 200, 255)

WHITE = (255, 255, 255)

PURPLE = (140, 70, 255)

BLUE = (80, 180, 255)

DARK_BLUE = (25, 35, 70)

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("FORTNITE TETRIS")

clock = pygame.time.Clock()

font = pygame.font.SysFont("Arial Black", 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:

                    x = (self.x + col_index) * BLOCK_SIZE
                    y = (self.y + row_index) * BLOCK_SIZE

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

                    pygame.draw.rect(
                        screen,
                        WHITE,
                        (
                            x,
                            y,
                            BLOCK_SIZE,
                            BLOCK_SIZE
                        ),
                        2
                    )

    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(DARK_BLUE)

    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,
                BLUE,
                (
                    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
                    )
                )

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

    if not game_over:
        current_piece.draw()

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

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

    title = font.render("FORTNITE TETRIS", True, PURPLE)

    screen.blit(title, (20, 60))

    if game_over:

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

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

        button_rect = pygame.Rect(
            WIDTH // 2 - 100,
            HEIGHT // 2,
            200,
            60
        )

        pygame.draw.rect(
            screen,
            PURPLE,
            button_rect,
            border_radius=15
        )

        pygame.draw.rect(
            screen,
            WHITE,
            button_rect,
            3,
            border_radius=15
        )

        button_text = font.render("PLAY AGAIN", True, BLACK)

        screen.blit(
            button_text,
            (
                button_rect.x + 10,
                button_rect.y + 10
            )
        )

        mouse_pos = pygame.mouse.get_pos()

        mouse_click = pygame.mouse.get_pressed()

        if button_rect.collidepoint(mouse_pos):

            if mouse_click[0]:

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

                current_piece = Piece()

                score = 0

                game_over = False

    pygame.display.update()

pygame.quit()