Well in-case anyone else has the same issue, the easiest fix is from
If you would like to fire at regular intervals, you can use a timer and realtime key states. If the timer is below the minimum interval/delay, don't fire. If the timer is above that delay, reset the timer and trigger the bullet.
I removed the vector erase for now until I find a better way to clean up excess bullets, but using a cool-down timer achieves the goal of one key press = one bullet.
Now I have two settings to tweak with power ups, either the speed of the bullet or the cool down for rate of fire.
Thanks Hapax!
See below (changes are bolded):
#include <SFML/Graphics.hpp>
#include <SFML/Poll_Event.h>
#include <SFML/Sprite_Creator.h>
#include <SFML/Key_logic.h>
#include <SFML/ast_randomizer.h>
#include <SFML/Levels.h>
#include <SFML/Move_the_background.h>
#include <SFML/Bullets.h>
int main()
{
//make all the things
sf::RenderWindow main_window(sf::VideoMode(1920, 1080), "Game", sf::Uint32());
main_window.setKeyRepeatEnabled(true);
sprite_creator create_space_ship,
create_asteroid,
create_background,
create_bullet;
levels start_level;
key_logic key_check;
sf::Sprite space_ship = create_space_ship.create_sprite("spaceship.png", true),
new_bullet = create_bullet.create_sprite("bullet.png", true),
main_background = create_background.create_sprite("main_game_background.png", false),
main_background2 = create_background.create_sprite("main_game_background.png", false);
std::vector<sf::Sprite> asteroid_sprites(100, sf::Sprite(create_asteroid.create_sprite("Asteroid.png", true)));
std::vector<sf::Sprite> bullet_sprites;
main_background2.setPosition(sf::Vector2f(3820.f, 0.f));
space_ship.setPosition(sf::Vector2f(300, 100));
space_ship.setScale(sf::Vector2f(.8f, .8f));
//clock
sf::Clock delta_clock, bullet_clock;
sf::Time bullet_cooldown; while (main_window.isOpen())
{
float delta = delta_clock.restart().asSeconds();
bullet_cooldown = bullet_clock.getElapsedTime();
//clear...draw...
main_window.clear();
move_the_background(main_background, main_background2, delta, main_window);
start_level.level_one(asteroid_sprites, main_window, delta, space_ship);
start_level.level_two(asteroid_sprites, main_window, delta, space_ship);
main_window.draw(space_ship);
//keypress
key_check.w_key(space_ship.getPosition(), space_ship);
key_check.s_key(space_ship.getPosition(), space_ship);
key_check.a_key(space_ship.getPosition(), space_ship);
key_check.d_key(space_ship.getPosition(), space_ship);
key_check.esc_key(main_window);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && bullet_cooldown.asSeconds() >= .5f)
{
bullet_clock.restart(); sf::Sprite new_bullet = create_bullet.create_sprite("bullet.png", true);
new_bullet.setPosition(space_ship.getPosition().x + 145.f, space_ship.getPosition().y + 0.f);
bullet_sprites.push_back(new_bullet);
}
for (int j = 0; j < bullet_sprites.size(); j++)
{
main_window.draw(bullet_sprites[j]);
create_bullet.move_sprite(1500.f, 0.f, delta, bullet_sprites[j], false, 0.f, false, 0.f, 0.f, "pos");
}
//display...
main_window.display();
//check to see if main window is closed
close_event_checker(main_window);
}
return 0;
}