An error occurred while executing the following Python script:

Error:   File "D:\Spatial\temp_scripts\temp_script_0c692fa2f7d348479fb360ab5d633407.py", line 1
    display.update()\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                run = False\n\n            if event.type == pygame.KEYDOWN:\n                main(win)\n\n    pygame.quit()\n\nif __name__ == \'__main__\':\n    win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))\n    pygame.display.set_caption(\'Tetris\')\n    main_menu(win)  # start game\n
     ^
SyntaxError: unexpected character after line continuation character


Script:
\nimport pygame\nimport random\n\n# Initialize Pygame\npygame.init()\n\n# Screen dimensions\nSCREEN_WIDTH = 300\nSCREEN_HEIGHT = 600\nBLOCK_SIZE = 30\n\n# Play area dimensions\nPLAY_WIDTH = 10 * BLOCK_SIZE  # 300\nPLAY_HEIGHT = 20 * BLOCK_SIZE  # 600\nTOP_LEFT_X = (SCREEN_WIDTH - PLAY_WIDTH) // 2\nTOP_LEFT_Y = SCREEN_HEIGHT - PLAY_HEIGHT\n\n# Define shapes\nS = [[\'.....\',\n      \'.....\',\n      \'..00.\',\n      \'.00..\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..00.\',\n      \'...0.\',\n      \'.....\']]\n\nZ = [[\'.....\',\n      \'.....\',\n      \'.00..\',\n      \'..00.\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'.00..\',\n      \'.0...\',\n      \'.....\']]\n\nI = [[\'..0..\',\n      \'..0..\',\n      \'..0..\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'0000.\',\n      \'.....\',\n      \'.....\',\n      \'.....\']]\n\nO = [[\'.....\',\n      \'.....\',\n      \'.00..\',\n      \'.00..\',\n      \'.....\']]\n\nJ = [[\'.....\',\n      \'.0...\',\n      \'.000.\',\n      \'.....\',\n      \'.....\'],\n     [\'.....\',\n      \'..00.\',\n      \'..0..\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'.....\',\n      \'.000.\',\n      \'...0.\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..0..\',\n      \'.00..\',\n      \'.....\']]\n\nL = [[\'.....\',\n      \'...0.\',\n      \'.000.\',\n      \'.....\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..0..\',\n      \'..00.\',\n      \'.....\'],\n     [\'.....\',\n      \'.....\',\n      \'.000.\',\n      \'.0...\',\n      \'.....\'],\n     [\'.....\',\n      \'.00..\',\n      \'..0..\',\n      \'..0..\',\n      \'.....\']]\n\nT = [[\'.....\',\n      \'..0..\',\n      \'.000.\',\n      \'.....\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..00.\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'.....\',\n      \'.000.\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'.00..\',\n      \'..0..\',\n      \'.....\']]\n\n# List of shapes and their colors\nSHAPES = [S, Z, I, O, J, L, T]\nSHAPE_COLORS = [(0, 255, 0),    # S - Green\n                (255, 0, 0),    # Z - Red\n                (0, 255, 255),  # I - Cyan\n                (255, 255, 0),  # O - Yellow\n                (255, 165, 0),  # J - Orange\n                (0, 0, 255),    # L - Blue\n                (128, 0, 128)]  # T - Purple\n\n# Define the Piece class\nclass Piece:\n    def __init__(self, x, y, shape):\n        self.x = x  # Horizontal position on the grid\n        self.y = y  # Vertical position on the grid\n        self.shape = shape\n        self.color = SHAPE_COLORS[SHAPES.index(shape)]\n        self.rotation = 0  # Number from 0-3\n\ndef create_grid(locked_positions={}):\n    grid = [[(0,0,0) for _ in range(10)] for _ in range(20)]\n\n    for i in range(len(grid)):\n        for j in range(len(grid[i])):\n            if (j,i) in locked_positions:\n                grid[i][j] = locked_positions[(j,i)]\n    return grid\n\ndef convert_shape_format(shape):\n    positions = []\n    format = shape.shape[shape.rotation % len(shape.shape)]\n\n    for i, line in enumerate(format):\n        row = list(line)\n        for j, column in enumerate(row):\n            if column == \'0\':\n                positions.append((shape.x + j - 2, shape.y + i - 4))\n\n    return positions\n\ndef valid_space(shape, grid):\n    accepted_positions = [[(j, i) for j in range(10) if grid[i][j] == (0,0,0)] for i in range(20)]\n    accepted_positions = [j for sub in accepted_positions for j in sub]\n\n    formatted = convert_shape_format(shape)\n\n    for pos in formatted:\n        if pos not in accepted_positions:\n            if pos[1] >= 0:\n                return False\n    return True\n\ndef check_lost(positions):\n    for pos in positions:\n        x, y = pos\n        if y < 1:\n            return True\n    return False\n\ndef get_shape():\n    return Piece(5, 0, random.choice(SHAPES))\n\ndef draw_text_middle(surface, text, size, color):\n    font = pygame.font.SysFont(\'comicsans\', size, bold=True)\n    label = font.render(text, 1, color)\n\n    surface.blit(label, (TOP_LEFT_X + PLAY_WIDTH/2 - (label.get_width()/2),\n                         TOP_LEFT_Y + PLAY_HEIGHT/2 - label.get_height()/2))\n\ndef draw_grid(surface, grid):\n    sx = TOP_LEFT_X\n    sy = TOP_LEFT_Y\n    for i in range(len(grid)):\n        pygame.draw.line(surface, (128,128,128), (sx, sy + i*BLOCK_SIZE), (sx+PLAY_WIDTH, sy + i*BLOCK_SIZE))\n        for j in range(len(grid[i])):\n            pygame.draw.line(surface, (128,128,128), (sx + j*BLOCK_SIZE, sy), (sx + j*BLOCK_SIZE, sy + PLAY_HEIGHT))\n\ndef clear_rows(grid, locked):\n    # Need to see if row is clear then shift every other row above down\n    increment = 0\n    for i in range(len(grid)-1, -1, -1):\n        row = grid[i]\n        if (0,0,0) not in row:\n            increment += 1\n            # Add positions to remove\n            ind = i\n            for j in range(len(row)):\n                try:\n                    del locked[(j, i)]\n                except:\n                    continue\n    if increment > 0:\n        # Shift every row above down\n        for key in sorted(list(locked), key=lambda x: x[1])[::-1]:\n            x, y = key\n            if y < ind:\n                newKey = (x, y + increment)\n                locked[newKey] = locked.pop(key)\n    return increment\n\ndef draw_next_shape(shape, surface):\n    font = pygame.font.SysFont(\'comicsans\', 30)\n    label = font.render(\'Next Shape\', 1, (255,255,255))\n\n    sx = TOP_LEFT_X + PLAY_WIDTH + 50\n    sy = TOP_LEFT_Y + PLAY_HEIGHT/2 - 100\n    format = shape.shape[shape.rotation % len(shape.shape)]\n\n    for i, line in enumerate(format):\n        row = list(line)\n        for j, column in enumerate(row):\n            if column == \'0\':\n                pygame.draw.rect(surface, shape.color, (sx + j*BLOCK_SIZE, sy + i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)\n\n    surface.blit(label, (sx + 10, sy - 30))\n\ndef update_score(nscore):\n    score = max_score()\n\n    with open(\'scores.txt\', \'w\') as f:\n        if int(score) > nscore:\n            f.write(str(score))\n        else:\n            f.write(str(nscore))\n\ndef max_score():\n    try:\n        with open(\'scores.txt\', \'r\') as f:\n            lines = f.readlines()\n            score = lines[0].strip()\n    except:\n        score = \'0\'\n    return score\n\ndef draw_window(surface, grid, score=0, last_score=0):\n    surface.fill((0,0,0))\n\n    # Title\n    font = pygame.font.SysFont(\'comicsans\', 60)\n    label = font.render(\'Tetris\', 1, (255,255,255))\n\n    surface.blit(label, (TOP_LEFT_X + PLAY_WIDTH / 2 - (label.get_width()/2), 30))\n\n    # Current score\n    font = pygame.font.SysFont(\'comicsans\', 30)\n    label = font.render(\'Score: \' + str(score), 1, (255,255,255))\n\n    sx = TOP_LEFT_X - 200\n    sy = TOP_LEFT_Y + 200\n\n    surface.blit(label, (sx + 20, sy + 160))\n\n    # High score\n    label = font.render(\'High Score: \' + last_score, 1, (255,255,255))\n    surface.blit(label, (sx + 20, sy + 200))\n\n    for i in range(len(grid)):\n        for j in range(len(grid[i])):\n            pygame.draw.rect(surface, grid[i][j],\n                             (TOP_LEFT_X + j*BLOCK_SIZE, TOP_LEFT_Y + i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)\n\n    # Draw grid and border\n    draw_grid(surface, grid)\n    pygame.draw.rect(surface, (255,0,0), (TOP_LEFT_X, TOP_LEFT_Y, PLAY_WIDTH, PLAY_HEIGHT), 5)\n\ndef main(win):\n    last_score = max_score()\n\n    locked_positions = {}\n    grid = create_grid(locked_positions)\n\n    change_piece = False\n    run = True\n    current_piece = get_shape()\n    next_piece = get_shape()\n    clock = pygame.time.Clock()\n    fall_time = 0\n    fall_speed = 0.5  # Seconds\n    level_time = 0\n    score = 0\n\n    while run:\n        grid = create_grid(locked_positions)\n        fall_time += clock.get_rawtime()\n        level_time += clock.get_rawtime()\n        clock.tick()\n\n        # Increase speed over time\n        if level_time/1000 > 5:\n            level_time = 0\n            if fall_speed > 0.12:\n                fall_speed -= 0.005\n\n        # Handle piece falling\n        if fall_time/1000 >= fall_speed:\n            fall_time = 0\n            current_piece.y +=1\n            if not(valid_space(current_piece, grid)) and current_piece.y >0:\n                current_piece.y -=1\n                change_piece = True\n\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                run = False\n                pygame.display.quit()\n                quit()\n\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_LEFT:\n                    current_piece.x -=1\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.x +=1\n                elif event.key == pygame.K_RIGHT:\n                    current_piece.x +=1\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.x -=1\n                elif event.key == pygame.K_DOWN:\n                    current_piece.y +=1\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.y -=1\n                elif event.key == pygame.K_UP:\n                    current_piece.rotation = (current_piece.rotation +1) % len(current_piece.shape)\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.rotation = (current_piece.rotation -1) % len(current_piece.shape)\n\n        shape_pos = convert_shape_format(current_piece)\n\n        # Add piece to the grid for drawing\n        for i in range(len(shape_pos)):\n            x, y = shape_pos[i]\n            if y > -1:\n                grid[y][x] = current_piece.color\n\n        # Check if piece hit the ground\n        if change_piece:\n            for pos in shape_pos:\n                locked_positions[(pos[0], pos[1])] = current_piece.color\n            current_piece = next_piece\n            next_piece = get_shape()\n            change_piece = False\n            # Clear rows\n            cleared = clear_rows(grid, locked_positions)\n            if cleared > 0:\n                score += cleared * 10\n\n        draw_window(win, grid, score, last_score)\n        draw_next_shape(next_piece, win)\n        pygame.display.update()\n\n        # Check if user lost\n        if check_lost(locked_positions):\n            draw_text_middle(win, "YOU LOST!", 80, (255,255,255))\n            pygame.display.update()\n            pygame.time.delay(1500)\n            run = False\n            update_score(score)\n\ndef main_menu(win):\n    run = True\n    while run:\n        win.fill((0,0,0))\n        draw_text_middle(win, \'Press Any Key To Play\', 60, (255,255,255))\n        pygame.display.update()\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                run = False\n\n            if event.type == pygame.KEYDOWN:\n                main(win)\n\n    pygame.quit()\n\nif __name__ == \'__main__\':\n    win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))\n    pygame.display.set_caption(\'Tetris\')\n    main_menu(win)  # start game\n
# Suggested Fix: \nimport pygame\nimport random\n\n# Initialize Pygame\npygame.init()\n\n# Screen dimensions\nSCREEN_WIDTH = 300\nSCREEN_HEIGHT = 600\nBLOCK_SIZE = 30\n\n# Play area dimensions\nPLAY_WIDTH = 10 * BLOCK_SIZE  # 300\nPLAY_HEIGHT = 20 * BLOCK_SIZE  # 600\nTOP_LEFT_X = (SCREEN_WIDTH - PLAY_WIDTH) // 2\nTOP_LEFT_Y = SCREEN_HEIGHT - PLAY_HEIGHT\n\n# Define shapes\nS = [[\'.....\',\n      \'.....\',\n      \'..00.\',\n      \'.00..\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..00.\',\n      \'...0.\',\n      \'.....\']]\n\nZ = [[\'.....\',\n      \'.....\',\n      \'.00..\',\n      \'..00.\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'.00..\',\n      \'.0...\',\n      \'.....\']]\n\nI = [[\'..0..\',\n      \'..0..\',\n      \'..0..\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'0000.\',\n      \'.....\',\n      \'.....\',\n      \'.....\']]\n\nO = [[\'.....\',\n      \'.....\',\n      \'.00..\',\n      \'.00..\',\n      \'.....\']]\n\nJ = [[\'.....\',\n      \'.0...\',\n      \'.000.\',\n      \'.....\',\n      \'.....\'],\n     [\'.....\',\n      \'..00.\',\n      \'..0..\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'.....\',\n      \'.000.\',\n      \'...0.\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..0..\',\n      \'.00..\',\n      \'.....\']]\n\nL = [[\'.....\',\n      \'...0.\',\n      \'.000.\',\n      \'.....\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..0..\',\n      \'..00.\',\n      \'.....\'],\n     [\'.....\',\n      \'.....\',\n      \'.000.\',\n      \'.0...\',\n      \'.....\'],\n     [\'.....\',\n      \'.00..\',\n      \'..0..\',\n      \'..0..\',\n      \'.....\']]\n\nT = [[\'.....\',\n      \'..0..\',\n      \'.000.\',\n      \'.....\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..00.\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'.....\',\n      \'.000.\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'.00..\',\n      \'..0..\',\n      \'.....\']]\n\n# List of shapes and their colors\nSHAPES = [S, Z, I, O, J, L, T]\nSHAPE_COLORS = [(0, 255, 0),    # S - Green\n                (255, 0, 0),    # Z - Red\n                (0, 255, 255),  # I - Cyan\n                (255, 255, 0),  # O - Yellow\n                (255, 165, 0),  # J - Orange\n                (0, 0, 255),    # L - Blue\n                (128, 0, 128)]  # T - Purple\n\n# Define the Piece class\nclass Piece:\n    def __init__(self, x, y, shape):\n        self.x = x  # Horizontal position on the grid\n        self.y = y  # Vertical position on the grid\n        self.shape = shape\n        self.color = SHAPE_COLORS[SHAPES.index(shape)]\n        self.rotation = 0  # Number from 0-3\n\ndef create_grid(locked_positions={}):\n    grid = [[(0,0,0) for _ in range(10)] for _ in range(20)]\n\n    for i in range(len(grid)):\n        for j in range(len(grid[i])):\n            if (j,i) in locked_positions:\n                grid[i][j] = locked_positions[(j,i)]\n    return grid\n\ndef convert_shape_format(shape):\n    positions = []\n    format = shape.shape[shape.rotation % len(shape.shape)]\n\n    for i, line in enumerate(format):\n        row = list(line)\n        for j, column in enumerate(row):\n            if column == \'0\':\n                positions.append((shape.x + j - 2, shape.y + i - 4))\n\n    return positions\n\ndef valid_space(shape, grid):\n    accepted_positions = [[(j, i) for j in range(10) if grid[i][j] == (0,0,0)] for i in range(20)]\n    accepted_positions = [j for sub in accepted_positions for j in sub]\n\n    formatted = convert_shape_format(shape)\n\n    for pos in formatted:\n        if pos not in accepted_positions:\n            if pos[1] >= 0:\n                return False\n    return True\n\ndef check_lost(positions):\n    for pos in positions:\n        x, y = pos\n        if y < 1:\n            return True\n    return False\n\ndef get_shape():\n    return Piece(5, 0, random.choice(SHAPES))\n\ndef draw_text_middle(surface, text, size, color):\n    font = pygame.font.SysFont(\'comicsans\', size, bold=True)\n    label = font.render(text, 1, color)\n\n    surface.blit(label, (TOP_LEFT_X + PLAY_WIDTH/2 - (label.get_width()/2),\n                         TOP_LEFT_Y + PLAY_HEIGHT/2 - label.get_height()/2))\n\ndef draw_grid(surface, grid):\n    sx = TOP_LEFT_X\n    sy = TOP_LEFT_Y\n    for i in range(len(grid)):\n        pygame.draw.line(surface, (128,128,128), (sx, sy + i*BLOCK_SIZE), (sx+PLAY_WIDTH, sy + i*BLOCK_SIZE))\n        for j in range(len(grid[i])):\n            pygame.draw.line(surface, (128,128,128), (sx + j*BLOCK_SIZE, sy), (sx + j*BLOCK_SIZE, sy + PLAY_HEIGHT))\n\ndef clear_rows(grid, locked):\n    # Need to see if row is clear then shift every other row above down\n    increment = 0\n    for i in range(len(grid)-1, -1, -1):\n        row = grid[i]\n        if (0,0,0) not in row:\n            increment += 1\n            # Add positions to remove\n            ind = i\n            for j in range(len(row)):\n                try:\n                    del locked[(j, i)]\n                except:\n                    continue\n    if increment > 0:\n        # Shift every row above down\n        for key in sorted(list(locked), key=lambda x: x[1])[::-1]:\n            x, y = key\n            if y < ind:\n                newKey = (x, y + increment)\n                locked[newKey] = locked.pop(key)\n    return increment\n\ndef draw_next_shape(shape, surface):\n    font = pygame.font.SysFont(\'comicsans\', 30)\n    label = font.render(\'Next Shape\', 1, (255,255,255))\n\n    sx = TOP_LEFT_X + PLAY_WIDTH + 50\n    sy = TOP_LEFT_Y + PLAY_HEIGHT/2 - 100\n    format = shape.shape[shape.rotation % len(shape.shape)]\n\n    for i, line in enumerate(format):\n        row = list(line)\n        for j, column in enumerate(row):\n            if column == \'0\':\n                pygame.draw.rect(surface, shape.color, (sx + j*BLOCK_SIZE, sy + i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)\n\n    surface.blit(label, (sx + 10, sy - 30))\n\ndef update_score(nscore):\n    score = max_score()\n\n    with open(\'scores.txt\', \'w\') as f:\n        if int(score) > nscore:\n            f.write(str(score))\n        else:\n            f.write(str(nscore))\n\ndef max_score():\n    try:\n        with open(\'scores.txt\', \'r\') as f:\n            lines = f.readlines()\n            score = lines[0].strip()\n    except:\n        score = \'0\'\n    return score\n\ndef draw_window(surface, grid, score=0, last_score=0):\n    surface.fill((0,0,0))\n\n    # Title\n    font = pygame.font.SysFont(\'comicsans\', 60)\n    label = font.render(\'Tetris\', 1, (255,255,255))\n\n    surface.blit(label, (TOP_LEFT_X + PLAY_WIDTH / 2 - (label.get_width()/2), 30))\n\n    # Current score\n    font = pygame.font.SysFont(\'comicsans\', 30)\n    label = font.render(\'Score: \' + str(score), 1, (255,255,255))\n\n    sx = TOP_LEFT_X - 200\n    sy = TOP_LEFT_Y + 200\n\n    surface.blit(label, (sx + 20, sy + 160))\n\n    # High score\n    label = font.render(\'High Score: \' + last_score, 1, (255,255,255))\n    surface.blit(label, (sx + 20, sy + 200))\n\n    for i in range(len(grid)):\n        for j in range(len(grid[i])):\n            pygame.draw.rect(surface, grid[i][j],\n                             (TOP_LEFT_X + j*BLOCK_SIZE, TOP_LEFT_Y + i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)\n\n    # Draw grid and border\n    draw_grid(surface, grid)\n    pygame.draw.rect(surface, (255,0,0), (TOP_LEFT_X, TOP_LEFT_Y, PLAY_WIDTH, PLAY_HEIGHT), 5)\n\ndef main(win):\n    last_score = max_score()\n\n    locked_positions = {}\n    grid = create_grid(locked_positions)\n\n    change_piece = False\n    run = True\n    current_piece = get_shape()\n    next_piece = get_shape()\n    clock = pygame.time.Clock()\n    fall_time = 0\n    fall_speed = 0.5  # Seconds\n    level_time = 0\n    score = 0\n\n    while run:\n        grid = create_grid(locked_positions)\n        fall_time += clock.get_rawtime()\n        level_time += clock.get_rawtime()\n        clock.tick()\n\n        # Increase speed over time\n        if level_time/1000 > 5:\n            level_time = 0\n            if fall_speed > 0.12:\n                fall_speed -= 0.005\n\n        # Handle piece falling\n        if fall_time/1000 >= fall_speed:\n            fall_time = 0\n            current_piece.y +=1\n            if not(valid_space(current_piece, grid)) and current_piece.y >0:\n                current_piece.y -=1\n                change_piece = True\n\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                run = False\n                pygame.display.quit()\n                quit()\n\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_LEFT:\n                    current_piece.x -=1\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.x +=1\n                elif event.key == pygame.K_RIGHT:\n                    current_piece.x +=1\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.x -=1\n                elif event.key == pygame.K_DOWN:\n                    current_piece.y +=1\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.y -=1\n                elif event.key == pygame.K_UP:\n                    current_piece.rotation = (current_piece.rotation +1) % len(current_piece.shape)\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.rotation = (current_piece.rotation -1) % len(current_piece.shape)\n\n        shape_pos = convert_shape_format(current_piece)\n\n        # Add piece to the grid for drawing\n        for i in range(len(shape_pos)):\n            x, y = shape_pos[i]\n            if y > -1:\n                grid[y][x] = current_piece.color\n\n        # Check if piece hit the ground\n        if change_piece:\n            for pos in shape_pos:\n                locked_positions[(pos[0], pos[1])] = current_piece.color\n            current_piece = next_piece\n            next_piece = get_shape()\n            change_piece = False\n            # Clear rows\n            cleared = clear_rows(grid, locked_positions)\n            if cleared > 0:\n                score += cleared * 10\n\n        draw_window(win, grid, score, last_score)\n        draw_next_shape(next_piece, win)\n        pygame.display.update()\n\n        # Check if user lost\n        if check_lost(locked_positions):\n            draw_text_middle(win, "YOU LOST!", 80, (255,255,255))\n            pygame.display.update()\n            pygame.time.delay(1500)\n            run = False\n            update_score(score)\n\ndef main_menu(win):\n    run = True\n    while run:\n        win.fill((0,0,0))\n        draw_text_middle(win, \'Press Any Key To Play\', 60, (255,255,255))\n        pygame.display.update()\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                run = False\n\n            if event.type == pygame.KEYDOWN:\n                main(win)\n\n    pygame.quit()\n\nif __name__ == \'__main__\':\n    win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))\n    pygame.display.set_caption(\'Tetris\')\n    main_menu(win)  # start game\n
# Suggested Fix: \nimport pygame\nimport random\n\n# Initialize Pygame\npygame.init()\n\n# Screen dimensions\nSCREEN_WIDTH = 300\nSCREEN_HEIGHT = 600\nBLOCK_SIZE = 30\n\n# Play area dimensions\nPLAY_WIDTH = 10 * BLOCK_SIZE  # 300\nPLAY_HEIGHT = 20 * BLOCK_SIZE  # 600\nTOP_LEFT_X = (SCREEN_WIDTH - PLAY_WIDTH) // 2\nTOP_LEFT_Y = SCREEN_HEIGHT - PLAY_HEIGHT\n\n# Define shapes\nS = [[\'.....\',\n      \'.....\',\n      \'..00.\',\n      \'.00..\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..00.\',\n      \'...0.\',\n      \'.....\']]\n\nZ = [[\'.....\',\n      \'.....\',\n      \'.00..\',\n      \'..00.\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'.00..\',\n      \'.0...\',\n      \'.....\']]\n\nI = [[\'..0..\',\n      \'..0..\',\n      \'..0..\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'0000.\',\n      \'.....\',\n      \'.....\',\n      \'.....\']]\n\nO = [[\'.....\',\n      \'.....\',\n      \'.00..\',\n      \'.00..\',\n      \'.....\']]\n\nJ = [[\'.....\',\n      \'.0...\',\n      \'.000.\',\n      \'.....\',\n      \'.....\'],\n     [\'.....\',\n      \'..00.\',\n      \'..0..\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'.....\',\n      \'.000.\',\n      \'...0.\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..0..\',\n      \'.00..\',\n      \'.....\']]\n\nL = [[\'.....\',\n      \'...0.\',\n      \'.000.\',\n      \'.....\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..0..\',\n      \'..00.\',\n      \'.....\'],\n     [\'.....\',\n      \'.....\',\n      \'.000.\',\n      \'.0...\',\n      \'.....\'],\n     [\'.....\',\n      \'.00..\',\n      \'..0..\',\n      \'..0..\',\n      \'.....\']]\n\nT = [[\'.....\',\n      \'..0..\',\n      \'.000.\',\n      \'.....\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'..00.\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'.....\',\n      \'.000.\',\n      \'..0..\',\n      \'.....\'],\n     [\'.....\',\n      \'..0..\',\n      \'.00..\',\n      \'..0..\',\n      \'.....\']]\n\n# List of shapes and their colors\nSHAPES = [S, Z, I, O, J, L, T]\nSHAPE_COLORS = [(0, 255, 0),    # S - Green\n                (255, 0, 0),    # Z - Red\n                (0, 255, 255),  # I - Cyan\n                (255, 255, 0),  # O - Yellow\n                (255, 165, 0),  # J - Orange\n                (0, 0, 255),    # L - Blue\n                (128, 0, 128)]  # T - Purple\n\n# Define the Piece class\nclass Piece:\n    def __init__(self, x, y, shape):\n        self.x = x  # Horizontal position on the grid\n        self.y = y  # Vertical position on the grid\n        self.shape = shape\n        self.color = SHAPE_COLORS[SHAPES.index(shape)]\n        self.rotation = 0  # Number from 0-3\n\ndef create_grid(locked_positions={}):\n    grid = [[(0,0,0) for _ in range(10)] for _ in range(20)]\n\n    for i in range(len(grid)):\n        for j in range(len(grid[i])):\n            if (j,i) in locked_positions:\n                grid[i][j] = locked_positions[(j,i)]\n    return grid\n\ndef convert_shape_format(shape):\n    positions = []\n    format = shape.shape[shape.rotation % len(shape.shape)]\n\n    for i, line in enumerate(format):\n        row = list(line)\n        for j, column in enumerate(row):\n            if column == \'0\':\n                positions.append((shape.x + j - 2, shape.y + i - 4))\n\n    return positions\n\ndef valid_space(shape, grid):\n    accepted_positions = [[(j, i) for j in range(10) if grid[i][j] == (0,0,0)] for i in range(20)]\n    accepted_positions = [j for sub in accepted_positions for j in sub]\n\n    formatted = convert_shape_format(shape)\n\n    for pos in formatted:\n        if pos not in accepted_positions:\n            if pos[1] >= 0:\n                return False\n    return True\n\ndef check_lost(positions):\n    for pos in positions:\n        x, y = pos\n        if y < 1:\n            return True\n    return False\n\ndef get_shape():\n    return Piece(5, 0, random.choice(SHAPES))\n\ndef draw_text_middle(surface, text, size, color):\n    font = pygame.font.SysFont(\'comicsans\', size, bold=True)\n    label = font.render(text, 1, color)\n\n    surface.blit(label, (TOP_LEFT_X + PLAY_WIDTH/2 - (label.get_width()/2),\n                         TOP_LEFT_Y + PLAY_HEIGHT/2 - label.get_height()/2))\n\ndef draw_grid(surface, grid):\n    sx = TOP_LEFT_X\n    sy = TOP_LEFT_Y\n    for i in range(len(grid)):\n        pygame.draw.line(surface, (128,128,128), (sx, sy + i*BLOCK_SIZE), (sx+PLAY_WIDTH, sy + i*BLOCK_SIZE))\n        for j in range(len(grid[i])):\n            pygame.draw.line(surface, (128,128,128), (sx + j*BLOCK_SIZE, sy), (sx + j*BLOCK_SIZE, sy + PLAY_HEIGHT))\n\ndef clear_rows(grid, locked):\n    # Need to see if row is clear then shift every other row above down\n    increment = 0\n    for i in range(len(grid)-1, -1, -1):\n        row = grid[i]\n        if (0,0,0) not in row:\n            increment += 1\n            # Add positions to remove\n            ind = i\n            for j in range(len(row)):\n                try:\n                    del locked[(j, i)]\n                except:\n                    continue\n    if increment > 0:\n        # Shift every row above down\n        for key in sorted(list(locked), key=lambda x: x[1])[::-1]:\n            x, y = key\n            if y < ind:\n                newKey = (x, y + increment)\n                locked[newKey] = locked.pop(key)\n    return increment\n\ndef draw_next_shape(shape, surface):\n    font = pygame.font.SysFont(\'comicsans\', 30)\n    label = font.render(\'Next Shape\', 1, (255,255,255))\n\n    sx = TOP_LEFT_X + PLAY_WIDTH + 50\n    sy = TOP_LEFT_Y + PLAY_HEIGHT/2 - 100\n    format = shape.shape[shape.rotation % len(shape.shape)]\n\n    for i, line in enumerate(format):\n        row = list(line)\n        for j, column in enumerate(row):\n            if column == \'0\':\n                pygame.draw.rect(surface, shape.color, (sx + j*BLOCK_SIZE, sy + i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)\n\n    surface.blit(label, (sx + 10, sy - 30))\n\ndef update_score(nscore):\n    score = max_score()\n\n    with open(\'scores.txt\', \'w\') as f:\n        if int(score) > nscore:\n            f.write(str(score))\n        else:\n            f.write(str(nscore))\n\ndef max_score():\n    try:\n        with open(\'scores.txt\', \'r\') as f:\n            lines = f.readlines()\n            score = lines[0].strip()\n    except:\n        score = \'0\'\n    return score\n\ndef draw_window(surface, grid, score=0, last_score=0):\n    surface.fill((0,0,0))\n\n    # Title\n    font = pygame.font.SysFont(\'comicsans\', 60)\n    label = font.render(\'Tetris\', 1, (255,255,255))\n\n    surface.blit(label, (TOP_LEFT_X + PLAY_WIDTH / 2 - (label.get_width()/2), 30))\n\n    # Current score\n    font = pygame.font.SysFont(\'comicsans\', 30)\n    label = font.render(\'Score: \' + str(score), 1, (255,255,255))\n\n    sx = TOP_LEFT_X - 200\n    sy = TOP_LEFT_Y + 200\n\n    surface.blit(label, (sx + 20, sy + 160))\n\n    # High score\n    label = font.render(\'High Score: \' + last_score, 1, (255,255,255))\n    surface.blit(label, (sx + 20, sy + 200))\n\n    for i in range(len(grid)):\n        for j in range(len(grid[i])):\n            pygame.draw.rect(surface, grid[i][j],\n                             (TOP_LEFT_X + j*BLOCK_SIZE, TOP_LEFT_Y + i*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)\n\n    # Draw grid and border\n    draw_grid(surface, grid)\n    pygame.draw.rect(surface, (255,0,0), (TOP_LEFT_X, TOP_LEFT_Y, PLAY_WIDTH, PLAY_HEIGHT), 5)\n\ndef main(win):\n    last_score = max_score()\n\n    locked_positions = {}\n    grid = create_grid(locked_positions)\n\n    change_piece = False\n    run = True\n    current_piece = get_shape()\n    next_piece = get_shape()\n    clock = pygame.time.Clock()\n    fall_time = 0\n    fall_speed = 0.5  # Seconds\n    level_time = 0\n    score = 0\n\n    while run:\n        grid = create_grid(locked_positions)\n        fall_time += clock.get_rawtime()\n        level_time += clock.get_rawtime()\n        clock.tick()\n\n        # Increase speed over time\n        if level_time/1000 > 5:\n            level_time = 0\n            if fall_speed > 0.12:\n                fall_speed -= 0.005\n\n        # Handle piece falling\n        if fall_time/1000 >= fall_speed:\n            fall_time = 0\n            current_piece.y +=1\n            if not(valid_space(current_piece, grid)) and current_piece.y >0:\n                current_piece.y -=1\n                change_piece = True\n\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                run = False\n                pygame.display.quit()\n                quit()\n\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_LEFT:\n                    current_piece.x -=1\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.x +=1\n                elif event.key == pygame.K_RIGHT:\n                    current_piece.x +=1\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.x -=1\n                elif event.key == pygame.K_DOWN:\n                    current_piece.y +=1\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.y -=1\n                elif event.key == pygame.K_UP:\n                    current_piece.rotation = (current_piece.rotation +1) % len(current_piece.shape)\n                    if not(valid_space(current_piece, grid)):\n                        current_piece.rotation = (current_piece.rotation -1) % len(current_piece.shape)\n\n        shape_pos = convert_shape_format(current_piece)\n\n        # Add piece to the grid for drawing\n        for i in range(len(shape_pos)):\n            x, y = shape_pos[i]\n            if y > -1:\n                grid[y][x] = current_piece.color\n\n        # Check if piece hit the ground\n        if change_piece:\n            for pos in shape_pos:\n                locked_positions[(pos[0], pos[1])] = current_piece.color\n            current_piece = next_piece\n            next_piece = get_shape()\n            change_piece = False\n            # Clear rows\n            cleared = clear_rows(grid, locked_positions)\n            if cleared > 0:\n                score += cleared * 10\n\n        draw_window(win, grid, score, last_score)\n        draw_next_shape(next_piece, win)\n        pygame.display.update()\n\n        # Check if user lost\n        if check_lost(locked_positions):\n            draw_text_middle(win, "YOU LOST!", 80, (255,255,255))\n            pygame.display.update()\n            pygame.time.delay(1500)\n            run = False\n            update_score(score)\n\ndef main_menu(win):\n    run = True\n    while run:\n        win.fill((0,0,0))\n        draw_text_middle(win, \'Press Any Key To Play\', 60, (255,255,255))\n        pygame.display.update()\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                run = False\n\n            if event.type == pygame.KEYDOWN:\n                main(win)\n\n    pygame.quit()\n\nif __name__ == \'__main__\':\n    win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))\n    pygame.display.set_caption(\'Tetris\')\n    main_menu(win)  # start game\n

Please suggest a fix for the error.