0

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.

HQ220
  • 43
  • 6
  • 2
    You might want to do this in something more graphical than the console. I suspect you can do this by faking out a console input line. As your program runs, use (/poll) `Console.KeyAvailable` to determine if the user has pressed a key. If he/she has, read it with `Console.ReadKey(false)` (which won't echo the key to the screen). If you want to echo the key, move to where you want it to appear and write it to the screen. Then go back to snaking around the screen – Flydog57 Sep 03 '20 at 21:40
  • 1
    _So how do I add a separate cursor?_ that's not possible. – aepot Sep 03 '20 at 22:21
  • 2
    You might want to [experiment with my snake game here](https://stackoverflow.com/a/16907575/2330053)... – Idle_Mind Sep 04 '20 at 00:24

1 Answers1

2

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;
Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
  • Right...move your cursor, draw. Move your cursor again, draw. This will happen so fast it will appear to be doing two different things at once. But to complete the illusion, hide that cursor. – Idle_Mind Sep 04 '20 at 00:22
  • Ok. Thanks, but how do I conclude with this code? Sorry, I do not have as much knowledge of c#. – HQ220 Sep 06 '20 at 21:07