HQ220
HQ220

Reputation: 43

c# two cursors in a console application game

I am trying to make a snake game in a c# console application.

using System.Reflection;

namespace First_console_game
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rnd = new Random();
            int redo = 0;
            Byte Foods = 0;
            Byte Flops = 0;
            int x = 10, y = 20;
            int Foodx = rnd.Next(1, 200);
            int Foody = rnd.Next(1, 175);
            ConsoleKeyInfo keyInfo;
            do
            {
                keyInfo = Console.ReadKey(true);

                //Console.SetCursorPosition(Foodx, Foody); This is the position of the food cell, and this is the part where it doesn't work. 

                Flops += 1;
                if (Flops == 5)
                {
                    Console.Clear();
                    Flops = 0;
                }



                switch (keyInfo.Key)
                {
                    case ConsoleKey.RightArrow:
                        x += 1;
                        Console.SetCursorPosition(x, y);
                        Console.Write("x");
                        break;
                    case ConsoleKey.LeftArrow:
                        x--;
                        Console.SetCursorPosition(x, y);
                        Console.Write("x");
                        break;
                    case ConsoleKey.UpArrow:
                        y -= 1;
                        Console.SetCursorPosition(x, y);
                        Console.Write("^");
                        break;
                    case ConsoleKey.DownArrow:
                        y += 1;
                        Console.SetCursorPosition(x, y);
                        Console.Write("x");
                        break;


                }

            } while (redo == 0);
            Console.ReadLine();
        }
    }
}

In the game, I want to have two cursors. One for the food, and one for the player.

But obviously, I can't use Console.SetCursorPosition(Foodx, Foody) because that will drive the player all around the screen, and therefore mess up the game.

So how do I add a separate cursor? Thanks.

Upvotes: 0

Views: 494

Answers (1)

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

Re: "But obviously,..." - this is NOT obvious at all! You can set the cursor there and output some "food" character, like * or F.

Cursor doesn't do anything in your game, so you could just hide it with

Console.CursorVisible = false;

Upvotes: 2

Related Questions