Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a console game where character 'M' as in "Martian" and character 'S' as in "SpaceCreature" stay opposite on both ends on the X axis and move up and down across Y axis.

I use arrow keys to make the 'M' move up and down. But the 'S' should also move but by itself whenever 'M' moves. I need to make the 'S' move at a slower pace to follow the 'M'.

What I have tried:

As of now, I got 'M' moving up and down using arrow keys and 'S' is also moving at the same time.

I need to make the 'S' move slower. I have tried thread.Sleep, but that just makes the 'S' disappear and appear back like a glitch. I think I need to use something called "Console.keyAvailable" but I am finding it hard on where to place that function.

C#
//X and Y get set constructors are defined in the abstract class:-SpaceObject 

public override void Draw()  //In both classes Martian and SpaceCreature
{
   Console.SetCursorPosition(X, Y);
   Console.WriteLine("S");  
   //In Martian class:- Console.WriteLine("M");
}
static void Main(string[] args)
{
   var m = new Martian(100, 10);
   var s = new SpaceShip(100, 10);

   const int MaxY = 25;

   m.Draw();  //Abstract override void method
   s.X = m.X + 100;
   s.Y = m.Y;
   s.Draw(); //Abstract override void method

   ConsoleKeyInfo keyInfo;
   while (true)
   {
      keyInfo = Console.ReadKey(true);
      Console.Clear();
      switch (keyInfo.Key)
      {
         case ConsoleKey.UpArrow:
         if (m.Y > 0)
         {
            m.Y--;
         }
         break;
         case ConsoleKey.DownArrow:
         if (m.Y < MaxY)
         {
            m.Y++;
         }
         break;
         }
         m.Draw();
         s.X = m.X + 100;
         s.Y = m.Y;
         s.Draw();
      }
   }
} 
Posted
Updated 21-Apr-19 4:31am

You don't use a timer at all. In a very simple method of doing this, you can just use the current DateTime.Now value and schedule the next move. In your drawing loop, if the scheduled time hasn't arrived yet, you don't move the character.
C#
// Schedule the next move for a half second from now.
DateTime schedMove = DateTime.Now().AddMilliseconds(500);

and in your look where you determine if you need to move your character or not, check to see if Now is greater than the sceduled DateTime:
C#
if (DateTime.Now() >= schedMove)
{
    // Move your character coordinates.
    ...
    // Schedule the next move.
    schedMove = DateTime.Now().AddMilliseconds(500);

    // Draw your characters.
 }
 
Share this answer
 
Quote:
I need to make the 'S' move at a slower pace to follow the 'M'.


Update both at the same time; but update 'S" with a smaller factor.

If M advances X, then advance S by some percentage of X.

Or, if M uses X steps or "frames" to advance, then have S use only (X-1) steps in the same time frame (stand still one step instead of moving).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900