def game_loop():
    while True:
        game_over = False
        game_close = False

        x = WIDTH / 2
        y = HEIGHT / 2

        x_change = 0
        y_change = 0

        snake_blocks = []
        snake_length = 1

        food_x = round(random.randrange(0, WIDTH - BLOCK_SIZE) / 20.0) * 20.0
        food_y = round(random.randrange(0, HEIGHT - BLOCK_SIZE) / 20.0) * 20.0

        while not game_over:

            while game_close:
                screen.fill(BLACK)
                message("You Lost! Press Q-Quit or C-Play Again", RED)
                pygame.display.update()

                for event in pygame.event.get():
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_q:
                            pygame.quit()
                            sys.exit()
                        if event.key == pygame.K_c:
                            game_over = True  # Exit inner loop to restart

            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:
                        x_change = -BLOCK_SIZE
                        y_change = 0
                    elif event.key == pygame.K_RIGHT:
                        x_change = BLOCK_SIZE
                        y_change = 0
                    elif event.key == pygame.K_UP:
                        y_change = -BLOCK_SIZE
                        x_change = 0
                    elif event.key == pygame.K_DOWN:
                        y_change = BLOCK_SIZE
                        x_change = 0

            x += x_change
            y += y_change

            if x >= WIDTH or x < 0 or y >= HEIGHT or y < 0:
                game_close = True

            screen.fill(BLACK)
            pygame.draw.rect(screen, RED, [food_x, food_y, BLOCK_SIZE, BLOCK_SIZE])

            snake_head = [x, y]
            snake_blocks.append(snake_head)

            if len(snake_blocks) > snake_length:
                del snake_blocks[0]

            for block in snake_blocks[:-1]:
                if block == snake_head:
                    game_close = True

            draw_snake(snake_blocks)

            pygame.display.update()

            if x == food_x and y == food_y:
                food_x = round(random.randrange(0, WIDTH - BLOCK_SIZE) / 20.0) * 20.0
                food_y = round(random.randrange(0, HEIGHT - BLOCK_SIZE) / 20.0) * 20.0
                snake_length += 1

            clock.tick(15)
