0
\$\begingroup\$

I am making a console based snake game. I am able to move the snake using grids that show up successively on the console. I want to animate the snake using always the same grid.

    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++;
        }
    }

snake

\$\endgroup\$
2
\$\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.

| improve this answer | |
\$\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 '19 at 23:00
  • \$\begingroup\$ @gamer67 Can you add a few pictures to show what you mean? \$\endgroup\$ – Charanor Feb 10 '19 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 '19 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 '19 at 22:46
0
\$\begingroup\$

I have made some progress. I have got the snake to move to the right and to move down.

        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();
        }
    }
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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