import pygame
import random
import sys

# Параметры игры
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 600
BLOCK_SIZE = 30
BOARD_WIDTH = SCREEN_WIDTH // BLOCK_SIZE
BOARD_HEIGHT = SCREEN_HEIGHT // BLOCK_SIZE

# Цвета
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# Цвета фигур
COLORS = [
    (255, 0, 0),   # Красный
    (0, 255, 0),   # Зелёный
    (0, 0, 255),   # Синий
    (255, 255, 0), # Жёлтый
    (0, 255, 255), # Бирюзовый
    (255, 0, 255), # Магента
    (255, 165, 0), # Оранжевый
]

# Фигуры Тетриса
SHAPES = [
    [[1, 1, 1], [0, 1, 0]],  # T
    [[1, 1], [1, 1]],        # O
    [[1, 0, 0], [1, 1, 1]],  # L
    [[0, 0, 1], [1, 1, 1]],  # J
    [[0, 1, 1], [1, 1, 0]],  # S
    [[1, 1, 0], [0, 1, 1]],  # Z
    [[1], [1], [1], [1]]     # I
]

# Языковые переводы
LANGUAGES = {
    "en": "Score",
    "ru": "Счёт",
    "zh": "分数",
    "es": "Puntuación",
    "de": "Punktzahl",
    "fr": "Score",
}

LANGUAGE_OPTIONS = list(LANGUAGES.keys())

class Tetris:
    def __init__(self):
        self.board = [[0 for _ in range(BOARD_WIDTH)] for _ in range(BOARD_HEIGHT)]
        self.colors_board = [[BLACK for _ in range(BOARD_WIDTH)] for _ in range(BOARD_HEIGHT)]
        self.current_piece = self.new_piece()
        self.next_piece = self.new_piece()
        self.piece_x = BOARD_WIDTH // 2 - 1
        self.piece_y = 0
        self.score = 0
        self.language_index = 0  # Индекс текущего языка
        self.game_over = False

    def new_piece(self):
        shape = random.choice(SHAPES)
        color = COLORS[SHAPES.index(shape)]
        return (shape, color)

    def rotate_piece(self):
        rotated_shape = [list(row) for row in zip(*reversed(self.current_piece[0]))]
        if self.valid_position(piece=(rotated_shape, self.current_piece[1])):
            self.current_piece = (rotated_shape, self.current_piece[1])

    def valid_position(self, offset_x=0, offset_y=0, piece=None):
        if piece is None:
            piece = self.current_piece
        for y, row in enumerate(piece[0]):
            for x, value in enumerate(row):
                if value:  # Если это часть фигуры
                    board_x = x + self.piece_x + offset_x
                    board_y = y + self.piece_y + offset_y
                    if (board_x < 0 or board_x >= BOARD_WIDTH or 
                            board_y >= BOARD_HEIGHT or 
                            (board_y >= 0 and self.board[board_y][board_x])):
                        return False
        return True

    def lock_piece(self):
        shape, color = self.current_piece
        for y, row in enumerate(shape):
            for x, value in enumerate(row):
                if value and self.piece_y + y >= 0:
                    self.board[self.piece_y + y][self.piece_x + x] = 1
                    self.colors_board[self.piece_y + y][self.piece_x + x] = color
        self.score += self.clear_lines()  # Суммируем очки за очищенные линии
        self.current_piece, self.next_piece = self.next_piece, self.new_piece()
        self.piece_x = BOARD_WIDTH // 2 - 1
        self.piece_y = 0
        if not self.valid_position():
            self.game_over = True

    def clear_lines(self):
        full_lines = [i for i, row in enumerate(self.board) if all(row)]
        for line in full_lines:
            del self.board[line]
            del self.colors_board[line]
            self.board.insert(0, [0 for _ in range(BOARD_WIDTH)])
            self.colors_board.insert(0, [BLACK for _ in range(BOARD_WIDTH)])
        return len(full_lines) * 100  # 100 очков за каждую очищенную линию

    def drop_piece(self):
        while self.valid_position(offset_y=1):
            self.piece_y += 1
        self.lock_piece()

    def move_piece(self, dx):
        if self.valid_position(offset_x=dx):
            self.piece_x += dx

    def update(self):
        if self.valid_position(offset_y=1):
            self.piece_y += 1
        else:
            self.lock_piece()

def draw_board(screen, board, colors_board):
    for y in range(BOARD_HEIGHT):
        for x in range(BOARD_WIDTH):
            if board[y][x]:
                pygame.draw.rect(screen, colors_board[y][x], 
                                 (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))

def draw_piece(screen, piece, x, y):
    shape, color = piece
    for py, row in enumerate(shape):
        for px, value in enumerate(row):
            if value:
                pygame.draw.rect(screen, color, 
                                 ((px + x) * BLOCK_SIZE, (py + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))

def draw_score(screen, score, lang):
    font = pygame.font.SysFont('Arial', 24)
    score_surface = font.render(f'{LANGUAGES[lang]}: {score}', True, WHITE)
    screen.blit(score_surface, (10, 10))

def draw_game_over(screen):
    font = pygame.font.SysFont('Arial', 48)
    game_over_surface = font.render('Игра окончена', True, WHITE)
    restart_surface = font.render('Нажмите R, чтобы играть снова', True, WHITE)
    screen.blit(game_over_surface, (SCREEN_WIDTH // 2 - 75, SCREEN_HEIGHT // 2 - 40))
    screen.blit(restart_surface, (SCREEN_WIDTH // 2 - 140, SCREEN_HEIGHT // 2 + 10))

def draw_settings_button(screen):
    font = pygame.font.SysFont('Arial', 20)
    settings_surface = font.render('⚙️ Настройки', True, WHITE)
    screen.blit(settings_surface, (SCREEN_WIDTH - 120, SCREEN_HEIGHT - 30))

def draw_language_selection(screen, language_index):
    font = pygame.font.SysFont('Arial', 24)
    lang_surface = font.render('Язык: ' + LANGUAGE_OPTIONS[language_index], True, WHITE)
    screen.blit(lang_surface, (10, SCREEN_HEIGHT - 30))

def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("Tetris")
    clock = pygame.time.Clock()
    tetris = Tetris()

    while True:
        while not tetris.game_over:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        tetris.move_piece(-1)
                    elif event.key == pygame.K_RIGHT:
                        tetris.move_piece(1)
                    elif event.key == pygame.K_DOWN:
                        tetris.drop_piece()
                    elif event.key == pygame.K_UP:
                        tetris.rotate_piece()
                    elif event.key == pygame.K_s:  # Нажмите 'S' для настройки
                        # Переход к выбору языка
                        tetris.language_index = (tetris.language_index + 1) % len(LANGUAGE_OPTIONS)

            tetris.update()

            screen.fill(BLACK)
            draw_board(screen, tetris.board, tetris.colors_board)
            draw_piece(screen, tetris.current_piece, tetris.piece_x, tetris.piece_y)
            draw_score(screen, tetris.score, LANGUAGE_OPTIONS[tetris.language_index])
            draw_settings_button(screen)
            draw_language_selection(screen, tetris.language_index)
            pygame.display.flip()
            
            clock.tick(5)

        while tetris.game_over:
            screen.fill(BLACK)
            draw_game_over(screen)
            draw_settings_button(screen)
            pygame.display.flip()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_r:  # Если нажата R, начинаем игру заново
                        tetris = Tetris()
                    if event.key == pygame.K_s:  # Нажмите 'S' для переключения языков
                        tetris.language_index = (tetris.language_index + 1) % len(LANGUAGE_OPTIONS)

    pygame.quit()

if __name__ == "__main__":
    main() в этом коде нельзя сменить язык счетчика исправь