简体   繁体   中英

C++ Space Invaders Movement

I'm really kind of new to c++ and quite bad at it but I'm making a space invaders game for my university project. I'm having trouble with the alien movement system and how to get the aliens to spawn in a line (once they get to a side of the screen they move down and then across).

Firstly here is the code I'm using to move the alien movement

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 });
}

I can get the aliens to go left and move down but for some reason they aren't moving right, I commented the line out "AlienVelocity.x*+1", I thought that would have just made them go right since "-1" made them go left...

To get the aliens to spawn in a line I've made an array and set so only 2 aliens to spawn just for testing purposes.

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());
}

On the 2nd line in side the for statement I put "+200+ because I thought that would just put the next alien +200 points but obviously not. I'm not sure on how to fix this as well...

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

This code says that whenever the aliens are between 0 and 975 to reverse the velocity. I believe this is the exact opposite of what you want. This means they should just go back and forth and never move across the screen. Swap the compare signs.

For moving down, take the current y position and add the row length to it, This should be done when the velocity is reversed - in the same if statement.

Start with those changes and see if you can figure out the rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM