我是第一年計算機工程師,所以我有點在編碼C++ amatuer,無論如何,我正在創造一個遊戲,其中有兩個飛船互相射擊。我現在成功地通過使用while循環和GetASyncKeyState函數使第一個太空船移動。但現在我正在讓子彈旅行。我用於循環,我確實成功地讓子彈向上行進。但有一個問題,我不能移動太空船,直到for循環停止或子彈到達控制檯窗口的頂部(我正在使用控制檯btw)。有沒有辦法同時運行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);
}
我不確定你是否知道這一點,但是你使用的任何「遊戲代碼」('GetAsyncKeyState','SetCoordinate'等)都不是標準的C++。這意味着我們不知道它們是什麼,或者如果您使用它們的方式有任何問題。我誠實地建議您與您的教師聯繫以獲得指導,因爲我們可以做的很少,可以幫助您調試具有許多神祕功能的代碼。 –
@ChrisBritt今年夏天,班結束了。我只是這樣做才能殺了一些時間。無論如何,我以爲你們都知道GetASyncKetState()對此很抱歉。我會用SetCoordinate()在其中更新代碼。 – Neo
@Neo由於您的代碼嚴重依賴於Windows API,因此我已經提前發佈並重新提出了您的問題,希望能夠讓熟悉相關功能的人員獲得更好的曝光。如果您覺得沒有幫助,請隨時恢復編輯。 – computerfreaker