I am trying to make a Space Invaders game. When you press the Space key, it is supposed to make a laser spawn in front of the ship and go up, but it only works the first time, then not again. It is not finished yet. If someone could please tell me what is wrong, I will appreciate it.
#include <SFML/Graphics.hpp>
int main()
{
using namespace sf;
Sprite enemyShips [12];
int isEnemyLiving [12];
Sprite enemyLasers [12];
int isEnemyLaserActive [12];
Sprite playerLasers [12];
int isPlayerLaserActive [12];
int playerPosition = 370;
int laserInactive = 0;
for (int firstSet=0; firstSet<12; firstSet++)
{
isEnemyLaserActive [firstSet] = 0;
isEnemyLiving [firstSet] = 0;
isPlayerLaserActive [firstSet] = 0;
}
// Create the main window
RenderWindow spaceInvaders(VideoMode(800, 660), "Space Invaders");
Texture removeEntity;
if (!removeEntity.loadFromFile("empty.png"))
{
return EXIT_FAILURE;
}
Texture enemyShip;
if (!enemyShip.loadFromFile("enemyShip.png"))
{
return EXIT_FAILURE;
}
Texture boom;
if (!boom.loadFromFile("boom.png"))
{
return EXIT_FAILURE;
}
Texture laser;
if (!laser.loadFromFile("laser.png"))
{
return EXIT_FAILURE;
}
Texture spaceShip;
if (!spaceShip.loadFromFile("spaceShip.png"))
{
return EXIT_FAILURE;
}
Sprite player;
player.setTexture(spaceShip);
player.setPosition(370, 600);
// Start the game loop
while (spaceInvaders.isOpen())
{
// Process events
Event isWindowClosed;
while (spaceInvaders.pollEvent(isWindowClosed))
{
// Close window : exit
if (isWindowClosed.type == Event::Closed)
spaceInvaders.close();
}
if (playerPosition>=0)
{
if (Keyboard::isKeyPressed(Keyboard::Left))
{
player.move(-1,0);
playerPosition--;
}
}
if (playerPosition<=760)
{
if (Keyboard::isKeyPressed(Keyboard::Right))
{
player.move(1,0);
playerPosition++;
}
}
if (Keyboard::isKeyPressed(Keyboard::Space))
{
for (int playerLaserChecker=0; playerLaserChecker<12; playerLaserChecker++)
{
if (laserInactive==0)
{
if (isPlayerLaserActive [playerLaserChecker]==0)
{
playerLasers [playerLaserChecker].setTexture(laser);
isPlayerLaserActive [playerLaserChecker] = 1;
playerLasers [playerLaserChecker].setPosition(playerPosition+20, 540);
laserInactive = 1;
}
}
}
}
laserInactive = 0;
for (int laserMover=0; laserMover<12; laserMover++)
{
if (isPlayerLaserActive [laserMover]==true)
{
playerLasers [laserMover].move(0,-1);
}
}
// Clear screen
spaceInvaders.clear();
// Draw the sprite
spaceInvaders.draw(player);
for (int laserDraw=0; laserDraw<12; laserDraw++)
{
spaceInvaders.draw(playerLasers[laserDraw]);
}
// Update the window
spaceInvaders.display();
}
return EXIT_SUCCESS;
}