c++ - 同时运行两个循环?使用循环移动和旅行

标签 c++ loops winapi

我是第一年的计算机工程师,所以我对 C++ 编码有点陌生,无论如何我正在创建一个游戏,其中有两艘宇宙飞船相互射击。我目前通过使用 while 循环和 GetASyncKeyState 函数成功地移动了第一艘宇宙飞船。但现在我正在让子弹旅行。我使用 for 循环,我确实成功地让子弹向上移动了。但是有一个问题,我不能移动宇宙飞船,直到 for 循环停止或子弹到达控制台窗口的顶部(顺便说一句,我正在使用控制台)。有没有办法同时运行 for 循环和 while ?或者同时运行两个不同的函数,因为 GameMovement() 用于飞船的移动而 BulletTravel() 用于子弹。我正在从 GameMovement() 调用 BulletTravel()。

void GameMovements()
{
bool FirstInitialization = true;
int Player1XCoordinate = 55, Player2XCoordinate = 55;
int Player1YCoordinateU = 28, Player1YCoordinateD = 29;


while (true)
{
    BulletPattern(Player1XCoordinate);

    if (FirstInitialization == true)
    {
        GameBorderAndStatus();
        SetCoordinate(Player1XCoordinate, Player1YCoordinateU);
        cout << "   ^  \n";
        SetCoordinate(Player1XCoordinate, Player1YCoordinateD);
        cout << "^==|==^ \n";
        FirstInitialization = false;
    }

    //MOVEMENTS FOR PLAYER 1
    else if (GetAsyncKeyState(VK_LEFT) && Player1XCoordinate != 16)
    {
        system("cls");
        GameBorderAndStatus();

        Sleep(10);
        Player1XCoordinate -= 3;
        SetCoordinate(Player1XCoordinate, Player1YCoordinateU);
        cout << "   ^  \n";
        SetCoordinate(Player1XCoordinate, Player1YCoordinateD);
        cout << "^==|==^ \n";
    }
    else if (GetAsyncKeyState(VK_RIGHT) && Player1XCoordinate != 94)
    {
        system("cls");
        GameBorderAndStatus();
        Player1XCoordinate += 3;

        Sleep(10);
        SetCoordinate(Player1XCoordinate, Player1YCoordinateU);
        cout << "   ^  \n";
        SetCoordinate(Player1XCoordinate, Player1YCoordinateD);
        cout << "^==|==^ \n";
    }
    else if (GetAsyncKeyState(VK_UP) && Player1YCoordinateU != 24)
    {
        system("cls");
        GameBorderAndStatus();
        Player1YCoordinateU -= 2;
        Player1YCoordinateD -= 2;

        Sleep(10);
        SetCoordinate(Player1XCoordinate, Player1YCoordinateU);
        cout << "   ^  \n";
        SetCoordinate(Player1XCoordinate, Player1YCoordinateD);
        cout << "^==|==^ \n";
    }
    else if (GetAsyncKeyState(VK_DOWN) && Player1YCoordinateU != 28)
    {
        system("cls");
        GameBorderAndStatus();
        Player1YCoordinateU += 2;
        Player1YCoordinateD += 2;

        Sleep(10);
        SetCoordinate(Player1XCoordinate, Player1YCoordinateU);
        cout << "   ^  \n";
        SetCoordinate(Player1XCoordinate, Player1YCoordinateD);
        cout << "^==|==^ \n";
    }

 }
}

void GameBorderAndStatus()
{
       //Draw game border
      for (int i = 0; i < 31; i++)
      {
         SetCoordinate(15, i);
         cout << "|";
         SetCoordinate(104, i);
         cout << "|";
      }

 }

void BulletPattern(int Player1MuzzleLocation)
{
    for (int i = 25; i != 3; i--)
    { 
       Sleep(100);
       SetCoordinate(Player1MuzzleLocation + 3, i);

    }
}

 void SetCoordinate(int CoordinateX, int CoordinateY)
 {
     COORD Coordinate;
     Coordinate.X = CoordinateX;
     Coordinate.Y = CoordinateY;
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Coordinate);
 }

最佳答案

与其使用两个不同的函数并在每个函数中移动一个对象,您可能会发现跟踪每个对象应位于的位置并使用一个函数绘制两个对象的更好结果。

由于缺少一些函数和变量声明,很难判断您的代码中发生了什么(例如,我没有看到您真正画过子弹 - 可能是您的 BulletPattern?), 但像这样的东西可能会成功:

void GameMovements()
{
    while (true)
    {        
        //MOVEMENTS FOR PLAYER 1
        if (GetAsyncKeyState(VK_LEFT) && Player1XCoordinate != GAMEBOARD_LEFT)
        {
            Player1XCoordinate -= 3;
        }
        else if (GetAsyncKeyState(VK_RIGHT) && Player1XCoordinate != GAMEBOARD_RIGHT)
        {
            Player1XCoordinate += 3;
        }
        else if (GetAsyncKeyState(VK_UP) && Player1YCoordinate != SPACESHIP_TOP)
        {
            Player1YCoordinate -= 2;
        }
        else if (GetAsyncKeyState(VK_DOWN) && Player1YCoordinate != SPACESHIP_BOTTOM)
        {
            Player1YCoordinate += 2;
        }

        Sleep(10);

        UpdateBulletPosition();
        DrawObjects();
    }
}

void UpdateBulletPosition()
{
  //if the bullet hits the top of the screen, remove it
  if (bulletYCoordinate == GAMEBOARD_TOP)
  {
      bulletXCoordinate = 0;
      bulletYCoordinate = 0;
  }

  //I assume you're automatically generating bullets whenever possible; you'll have to adjust this conditional if that's not the case

  //no bullet? generate one
  if (bulletXCoordinate == 0)
  {
      bulletXCoordinate = Player1XCoordinate + 3;
      bulletYCoordinate = 25;
  }
  else
  {
      bulletYCoordinate--;

      Sleep(100);
  }
}

void DrawObjects()
{
        //wipe the screen and show status first
        system("cls");
        GameBorderAndStatus();

        SetCoordinate(Player1XCoordinate, Player1YCoordinate);
        cout << "   ^  \n";
        SetCoordinate(Player1XCoordinate, Player1YCoordinate + 1);
        cout << "^==|==^ \n";

        //only draw the bullet if there's a bullet there to draw
        if (bulletXCoordinate != 0)
        {
            SetCoordinate(bulletXCoordinate, bulletYCoordinate);
            cout << ".\n";
        }
}

const int GAMEBOARD_LEFT = 16;
const int GAMEBOARD_RIGHT = 94;
const int GAMEBOARD_TOP = 3;
const int SPACESHIP_TOP = 24;
const int SPACESHIP_BOTTOM = 28;

int Player1XCoordinate = 55, Player2XCoordinate = 55;
int Player1YCoordinate = 28;
int bulletXCoordinate = 0, bulletYCoordinate = 0;

我还对您的其余代码进行了一些调整。 if-else block 中的每个案例都使用相同的基本绘图代码,因此我将其完全从 if-else 中删除。这也让我放弃了整个初始化 if-else

我还删除了您的宇宙飞船垂直坐标之一。你真的不需要两个;只需跟踪上部坐标并在 Player1YCoordinate + 1 处绘制船的下半部分。

最后,我用常量替换了硬编码的板边缘。魔术数字通常不受欢迎。使用命名常量可以更轻松地确定为什么要在给定位置使用给定值,并使将来更新代码更容易(也许您需要针对不同的控制台尺寸进行更新)。

关于c++ - 同时运行两个循环?使用循环移动和旅行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36660161/

相关文章:

c++ - 具有自动存储持续时间的虚拟功能似乎不起作用

c++ - 排列的递归解决方案

python - 通过迭代创建列表的最快方法

css - 如何设置 CSS 类名来自 ngFor 中的 json 对象数组 | Angular 6

c++ - 结合 blits 和 gdi 绘图(不工作,闪烁)

c++ - SystemParametersInfo 将背景设置为纯色而不是实际设置图片

c++ - XCode 不会破坏 #include 的 CPP 文件

c++ - 查看 PC 是否连接到指定网络的编程方式

python 和 NumPy : Iterating over specific axes with multi_index?

windows - 如何以编程方式防止 Windows 硬盘驱动器降速?