我是第一年的计算机工程师,所以我在编写C ++方面有点小,无论如何我正在制作一个游戏,其中有两个太空飞船相互射击。我目前通过使用while循环和GetASyncKeyState函数成功完成了第一次太空飞船移动。但现在我正在制造子弹旅行。我用了循环,我确实成功地让子弹向上移动。但是有一个问题,我不能移动飞船直到for循环停止或子弹到达控制台窗口的顶部(我使用控制台顺便说一句)。有没有办法同时运行for循环?或者同时运行两个不同的功能,因为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);
}
答案 0 :(得分:1)
不是使用两个不同的函数并在每个函数中移动一个对象,而是可以找到更好的结果,跟踪每个对象应该在哪里,并使用一个函数来绘制两者。
由于缺少某些函数和变量声明(例如,我没有看到你实际上正在绘制一个子弹 - 可能是你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
块中的每个案例都使用相同的基本绘图代码,因此我完全将其从if-else
中删除。这也让我放弃了整个初始化if-else
。
我还丢弃了宇宙飞船的一个垂直坐标。你真的不需要两个;只需跟踪上部坐标并在Player1YCoordinate + 1
处绘制船的下半部分。
最后,我用常量替换了硬编码的板边缘。魔术数字通常不受欢迎;使用命名常量可以更容易地确定您在给定位置使用给定值的原因,以及将来更容易更新代码(可能需要针对不同的控制台大小进行更新)。