0

I am looking to add an object in c# using console application, then once its is on screen i would like to have it move within the bounds of my window to different locations once it has been hit. This is the code i have so far to print the apple on screen.

 static void Apple()
        {
            do
            {
                Console.ForegroundColor = ConsoleColor.White;
                NewApple = RandApple.Next(200);
                Console.Write("A");
                Console.ReadLine();
            } while (!Gameover);
        }
BamBam
  • 23
  • 7
  • Console is probably not the best technology choice for accomplishing this. I'm not saying its impossible, but it will likely be ugly. Have you considered using a different tech, like WPF or XNA that is better for canvas style drawing? – BradleyDotNET Apr 28 '14 at 21:33
  • @BradleyDotNET: No! Give us the old-skool console games! – Patrick Hofman Apr 28 '14 at 21:34
  • I am practicing with c# console commands for a course i am on. This has been racking my brain for a while now. i can do random positions but not move the character. Its a pretty basic game. – BamBam Apr 28 '14 at 21:35

1 Answers1

0

You have to use the Console.SetCursorPosition method.

In that way you can move the cursor to any place on the screen:

Random random = new Random();
Console.SetCursorPosition(random.Next(10), random.Next(10));
Console.Write("X");

Where 10 is the maximum size of your array [10,10].

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • I have that set up to put it in location but i would not like to write 40- 100 lines of code just to move it around the screen, any other advice? – BamBam Apr 28 '14 at 21:36
  • @BamBam: I see only two lines of code. Can you explain your problem a little more. Not sure if I understand you correctly. – Patrick Hofman Apr 28 '14 at 21:37
  • I am looking to move it to a random location once the apple has been eaten but keep within the bounds of my grid array. – BamBam Apr 28 '14 at 21:39
  • Then calculate X and Y according to your grid. Is that the problem? – Patrick Hofman Apr 28 '14 at 21:39
  • Exactly that, i think i can get the Y values but never the X. – BamBam Apr 28 '14 at 21:40
  • So you have to calculate a 100% random place on the screen? Or are there more rules? – Patrick Hofman Apr 28 '14 at 21:42
  • other then it changing locations once eaten nope, pretty sure that is all. – BamBam Apr 28 '14 at 21:43
  • Are you looking for something like [`Random.Next`](http://msdn.microsoft.com/en-us/library/zd1bc8e5(v=vs.110).aspx)? – Patrick Hofman Apr 28 '14 at 21:45
  • i have tried something like that but it did not work when i placed it. – BamBam Apr 28 '14 at 21:52
  • okay so i tried the code but i cant seem to get it to register while my game is active. i also do not know how to check its position with the snakes position so that i can execute the random function. know anything that would help ? – BamBam Apr 29 '14 at 08:19
  • @BamBam: I think your ReadLine is blocking. See [this answer](http://stackoverflow.com/a/2041489/993547) how to overcome this (it's not that straight forward). At timeout you can do the actual move. – Patrick Hofman Apr 29 '14 at 08:30
  • No that wouldn't work i would need to repeat the code every time i wanted to use the instance. – BamBam Apr 29 '14 at 09:57