Hello, I am trying to make it where you can shoot bullets, however, it only shoots one bullet. Also, you keep having to press down the space key in order to make
the bullet move. How do I make it so that I only need to press the spacebar once in order to make the bullet move on its own?
#include <iostream>
#include <vector>
#include <SFML\System.hpp>
#include <SFML\Graphics.hpp>
using namespace std;
sf::RenderWindow GameWin;
sf::Event event;
sf::RectangleShape bullet;
sf::RectangleShape rect;
bool GameRunning = true;
bool Fired = false;
typedef struct BULLET
{
float x, y, speed;
};
BULLET gBullet;
vector <BULLET> bullets;
int main(int argc, char* argv[])
{
GameWin.create(sf::VideoMode(800, 600), "Shooter");
rect.setSize(sf::Vector2f(50, 50));
rect.setPosition(sf::Vector2f(800 / 2, 600 / 2));
rect.setFillColor(sf::Color(255, 0, 0));
gBullet.x = 10.0f, gBullet.y = 10.0f;
gBullet.speed = 3.0f;
while (GameRunning)
{
while (GameWin.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
GameRunning = false;
GameWin.close();
}
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Escape)
{
GameRunning = false;
GameWin.close();
}
if (event.key.code == sf::Keyboard::Space)
{
Fired = true;
}
}
if (Fired == true)
{
bullet.setSize(sf::Vector2f(10, 10));
bullet.setFillColor(sf::Color(0, 255, 0));
//bullet.setPosition(sf::Vector2f(gBullet.x, gBullet.y));
bullet.move(gBullet.x * gBullet.speed, 0);
bullets.push_back(gBullet);
}
}
GameWin.clear();
GameWin.draw(rect);
if (Fired == true)
{
GameWin.draw(bullet);
}
GameWin.display();
}
return 0;
}