如何使角色在控制台窗口上移动?

时间:2019-04-29 18:31:09

标签: c# .net

我正在尝试创建一个游戏,其中“火星人” M使用空格键和上下箭头键在Y轴上移动来射击“激光”。当“ M”和“ S”都在同一X轴上时,还有一个名为“ SpaceCreature” S的生物跟随着“ M”移动并射击激光。

当M和S在相同的X轴上时,S开始拍摄,但是当M向上或向下移动时,先前拍摄的激光束会卡在屏幕中间。即使M和S不在同一轴上,我也应该能够使激光继续在屏幕上移动。

到目前为止,除了S的Laser拍摄之外,我都能正常工作。

abstract class SpaceObject
{
        public int X;
        public int Y;
        public int MovementDelay;
        public DateTime nextMovement;

        public int Health;

        abstract public void Draw(bool Visible);

        //Martian's and SpaceShip's Movement Up and Down
        public void MoveUp()
        {
            if (this.Y > 0)
            {
                this.Draw(false);
                this.Y--;
                this.Draw(true);
            }
            this.UpdateNextMovement();
        }

        public void MoveDown()
        {
            if (this.Y < Console.WindowHeight - 4)
            {
                this.Draw(false);
                this.Y++;
                this.Draw(true);
            }
            this.UpdateNextMovement();
        }

        //SpaceShip's Laser move left
        public void MoveLeft()
        {
            if (this.X > Console.WindowLeft + 2)
            {
                this.Draw(false);
                this.X--;
                this.Draw(true);
            }

            if (this.X == Console.WindowLeft + 2)
            {
                this.Draw(false);
            }
            this.UpdateNextMovement();
        }

        //Continue SpaceShip's Movement to follow Martain
        public void MoveToward(SpaceObject so)
        {
            if (so.Y < this.Y)
            {
                this.MoveUp();
            }
            else if (so.Y > this.Y)
            {
                this.MoveDown();
            }
            else
            {
                this.UpdateNextMovement();
            }
        }

        //Continue Martian's Lasers' Movement
        public void MoveTowardRight()
        {
            if (this.X > 2)
            {
                this.MoveRight();
            }
            else
            {
                this.UpdateNextMovement();
            }
        }

        //Continue SpaceShip's Lasers' Movement
        public void MoveTowardLeft()
        {
            if (this.X > Console.WindowLeft + 2)
            {

                this.MoveLeft();
            }
            else
            {
                this.UpdateNextMovement();
            }
        }

        public void UpdateNextMovement()
        {
            this.nextMovement = DateTime.Now.AddMilliseconds(this.MovementDelay);
        }
}
////////
class SpaceShipLaser : SpaceObject
{
  public SpaceShipLaser()
  {
    this.X = Console.WindowWidth - 3;
    this.Y = Console.WindowHeight / 2;
    this.MovementDelay = 100;
    this.nextMovement = DateTime.Now.AddMilliseconds(this.MovementDelay);
  }

  public override void Draw(bool Visible)
  {
    Console.SetCursorPosition(this.X, this.Y);
    Console.Write(Visible ? "*" : " ");
  }
}
///////
static void Main(string[] args)
{
   bool spaceLaser = false;
   bool quit = false;
   ConsoleKeyInfo cki;
   Console.Clear();
   Console.CursorVisible = false;
   var m = new Martian();
   var s = new SpaceShip();

   List<MartianLaser> martianLasers = new List<MartianLaser>();
   List<SpaceShipLaser> spaceShipLasers = new List<SpaceShipLaser>();

   m.Draw(true);
   s.Draw(true);
   do
   {
      //Martian Movement
      if (Console.KeyAvailable)
      {
         cki = Console.ReadKey(true);
         switch (cki.Key)
         {
           case ConsoleKey.UpArrow:
           m.MoveUp();
           break;

           case ConsoleKey.DownArrow:
           m.MoveDown();
           break;

           case ConsoleKey.Spacebar:
           var mLaser = new MartianLaser();
           mLaser.Y = m.Y;
           mLaser.X = m.X + 1;
           mLaser.MoveRight();
           martianLasers.Add(mLaser);
           break;

           case ConsoleKey.Escape:
           quit = true;
           break;
          }
     }

     //SpaceShip follows Martain
     if (DateTime.Now >= s.nextMovement)
     {
       s.MoveToward(m);
     }
     //SpaceShip's Lasers' Movement
     if (s.Y == m.Y)
     {
        spaceLaser = true; //to set lasers continue to move
        var sL = new SpaceShipLaser();
        spaceShipLasers.Add(sL);
        for (int index = 0; index < spaceShipLasers.Count; index++)
        {
          SpaceShipLaser sLaser = spaceShipLasers[index];
          sLaser.Y = s.Y;

          if (DateTime.Now >= sLaser.nextMovement)
          {
            sLaser.MoveTowardLeft();
          }

          if (sLaser.X == m.X + 1 && sLaser.Y == m.Y)
          {
            if (m.Health > 0)
              m.Health--;
            if (m.Health == 0)
            {
              Console.Clear();
              Console.WriteLine("Martian dead!");
              return;
            }
            sLaser.Draw(false);
            spaceShipLasers.Remove(sLaser);
          }
       }
     }
     if (spaceLaser==true) //To make existing lasers move
     {
       for (int index = 0; index < spaceShipLasers.Count; index++)
       {
         SpaceShipLaser laserBeamShip = spaceShipLasers[index];
         if (DateTime.Now >= laserBeamShip.nextMovement)
         {
             laserBeamShip.MoveTowardLeft();
         }
       }
     } 
  }while (!quit);
}

0 个答案:

没有答案