C 太空入侵者运动

C++ Space Invaders Movement

本文关键字:运动 入侵者 太空      更新时间:2023-10-16

我真的是C 的新手,而且很糟糕,但我正在为我的大学项目制作太空入侵者游戏。我在外星人运动系统方面遇到了麻烦,以及如何使外星人在一条线上产卵(一旦他们到达屏幕的一面,它们就向下移动,然后越过)。

首先,这是我用来移动外星人运动的代码

if (currentSpritePos.x < 975 || currentSpritePos.x > 0)
{
    this->AlienVelocity.x = this->AlienVelocity.x*-1;
}
if (currentSpritePos.x == 0)
{
    cout << "Im at the wall wooo and going to move down";
    //AlievVelocity.x*+1;
    setSpritePos({ 0, 75 });
}

我可以让外星人向左移动并向下移动,但是由于某些原因,我没有向右移动,我评论了" Alienvelocity.x* 1",我认为这会让它们变得正确,因为" -1"使他们向左走...

要让外星人在一条线上产生我制作的数组并设置了两个外星人,因此仅用于测试目的。

for (int aly = 0; aly < 2; aly++) // Change the 35 to number of desired aliens
{
    theAliens.push_back(new cAliens);
    theAliens[aly]->setSpritePos({ (100, 50) + 200 });
    theAliens[aly]->setSpriteTranslation({ 0, 0 });
    theAliens[aly]->setTexture(theTextureManager->getTexture("alien"));
    theAliens[aly]->setSpriteDimensions(theTextureManager->getTexture("ship")->getTWidth(), theTextureManager->getTexture("alien")->getTHeight());
}

在侧面的第二行上,我将" 200 "放置在语句中,因为我认为这只会将下一个外星人 200点放置,但显然不是。我不确定如何解决此问题...

if (currentSpritePos.x < 975 || currentSpritePos.x > 0)
{
    this->AlienVelocity.x = this->AlienVelocity.x*-1;
}

此代码说,每当外星人在0到975之间以扭转速度。我相信这与您想要的完全相反。这意味着他们应该来回走动,永远不要在屏幕上移动。交换比较符号。

为了向下移动,占据当前的位置并将行长添加到其上,应在速度相反时进行此操作 - 在同一if语句中。

从这些更改开始,看看您是否可以找出其余的。