Alien Attack is a fixed shooter I built with C++ and SFML. Each alien moves independently, so I had to handle its direction, edge checks, and movement alongside the usual projectiles and collisions.
- Built with
- C++, SFML, CMake
- Focus
- Classes, movement, collisions, debugging
The movement bug
A few aliens dropped down as soon as the game started, before they had reached an edge. I wanted to keep them moving independently, so I looked at which aliens dropped and compared the gameplay with the movement code.
The fix
The starting direction was set for only one alien, and the edge checks didn't account for the direction each alien was moving. I initialized every alien's direction and made each edge check depend on that direction.
// Only the last alien's direction was initialized.isAlienRight[ALIEN_COUNT - 1] = { false };// The edge checks ignored which way the alien was moving.if (alien->getMyPixie()->getX() > WINDOW_WIDTH - 10) { isAlienRight[i] = true; alien->getMyPixie()->setPosition(WINDOW_WIDTH - 30, alien->getMyPixie()->getY() + 35);}else if (alien->getMyPixie()->getX() <= 0) { isAlienRight[i] = false; alien->getMyPixie()->setPosition(0, alien->getMyPixie()->getY() + 35);}// Give every alien a starting direction.std::fill_n(isAlienRight, ALIEN_COUNT, false);// Check the edge in the direction this alien is moving.if (!isAlienRight[i] && alien->getMyPixie()->getX() > WINDOW_WIDTH - 10) { isAlienRight[i] = true; alien->getMyPixie()->setPosition(WINDOW_WIDTH - 30, alien->getMyPixie()->getY() + 35);}else if (isAlienRight[i] && alien->getMyPixie()->getX() <= 0) { isAlienRight[i] = false; alien->getMyPixie()->setPosition(0, alien->getMyPixie()->getY() + 35);}That stopped the early drops while keeping the independent movement. I also made movement depend on elapsed time so its speed wasn't tied to the frame rate.