0
\$\begingroup\$
    static void Main(string[] args)
    {
        char[,] board = new char[17, 17];
        int count = 5;
        for (int i = 0; i <= 16;             {
            for (int j = 0; j <= 16; j++) 
            {
                board[i, j] = '.';
            }
        }
        while (count < 11)
        {
            board[8, count] = '*';
            board[8, count + 1] = '*';
            board[8, count + 2] = '*';
            board[8, count + 3] = '*';
            board[8, count + 4] = '*';
            board[8, count + 5] = '*';
            board[8, count + 6] = '*';

            for (int i = 0; i <= 16; i++)
            {
                for (int j = 0; j <= 16; j++)
                {
                    Console.Write(board[i, j]);
                }
                Console.WriteLine();
            }
            count++;
        }
    }

I am making a console based snake game. I am able to move the snake using multiple grids. I want to animate the snake using only one grid.

snake

\$\endgroup\$
1
\$\begingroup\$

Assuming I understood your question correctly, simply call Console.Clear() before you draw your grid. This will clear the console of all text so when you draw your grid it will always be at the top.

\$\endgroup\$
  • \$\begingroup\$ I put the Console.Clear() function below the while loop and it only draws one grid but the last grid. But I want the snake to animate on the one grid. \$\endgroup\$ – gamer67 Feb 9 at 23:00
  • \$\begingroup\$ @gamer67 Can you add a few pictures to show what you mean? \$\endgroup\$ – Charanor Feb 10 at 2:10
  • 1
    \$\begingroup\$ Put the clear() inside the while loop, at the beginning. If the animation is too fast, add a delay, or only move when a button is pressed \$\endgroup\$ – Peethor Feb 10 at 17:04
  • \$\begingroup\$ I put the clear in the bottom of the while loop and it works. I also put in a small delay. \$\endgroup\$ – gamer67 Feb 10 at 22:46
0
\$\begingroup\$

well I have made some progress. I have got the snake to move to the right and to move down but I want the snake to move down following the snake previous position.

        static void Main(string[] args)
    {
        const int board_size = 17;
        char[,] board = new char[board_size, board_size];
        int horizontal = 5, vertical = 8, count = 0;

        for (int i = 0; i < board_size; i++) 
        {
            for (int j = 0; j < board_size; j++) 
            {
                board[i, j] = '.';
            }
        }

        void draw_board()
        {
            for (int i = 0; i < board_size; i++)
            {
                for (int j = 0; j < board_size; j++)
                {
                    Console.Write(board[i, j]);
                }
                Console.WriteLine();
            }
        }

        while (count < 12)
        {
            if (count >= 0 && count < 6)
            {
                board[vertical, horizontal] = '*';
                board[vertical, horizontal + 1] = '*';
                board[vertical, horizontal + 2] = '*';
                board[vertical, horizontal + 3] = '*';
                board[vertical, horizontal + 4] = '*';
                board[vertical, horizontal + 5] = '*';
                board[vertical, horizontal + 6] = '*';

                draw_board();

                horizontal++;
            }

            if (count > 5 && count < 12)
            {
                board[vertical, horizontal] = '*';
                board[vertical+1, horizontal] = '*';
                board[vertical+2, horizontal] = '*';
                board[vertical+3, horizontal] = '*';
                board[vertical+4, horizontal] = '*';
                board[vertical+5, horizontal] = '*';
                board[vertical+6, horizontal] = '*';

                draw_board();

                vertical++;
            }

            count++;

            System.Threading.Thread.Sleep(1000);
            Console.Clear();
        }
    }
\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.