# How can I get multiple bullets on screen?

I've finally gotten this laser thing to work for my Space Shooter, and so far I've come across a slight problem. I've gotten the laser to position itself at the starting position correctly and move vertically perfectly. Anyway, whenever I hit the space-bar (which is the controls for shooting), it shoots the bullet/laser out, but I have to wait until the bullet clears to shoot another one.

What I want it to do is keep producing bullets until the user releases their hand on the space-bar... here's the code I have so far:

if(spacebar)
// this bool variable is set to true once the user presses the space bar
{
int frame2 = 0;

if(once2 == 0)
{
projectilex2 = x + 19;
projectiley2 = y + -35;
//Positions the bullet at the nose of the ship
}

if(frame2 == 3)
{
frame2 = 0;
}

apply_surface(projectilex2, projectiley2, ShootStuff, screen, &lazers[0]);

frame2 ++;
projectiley2--;

once2++;
if(once2 == 250)
{
once2 = 0;
spacebar = false;
}
//This will eventually get better. My shoot function is horrible lol :/
}


Does anyone have any ideas as to how I can accomplish this without having to wait for the bullet to clear before I can shoot another one :( ?

You really need to have a separate class for your bullets! It will make everything 1000x easier to manage, render and ultimately to answer your question, the ability to keep adding in new bullets when the space bar is held down.

Your class can be as simple as the following (source):

You could use vectors to create them dynamically.

Example:

#include <vector>
using namespace std;

class CBullet
{
public:
CBullet();
~CBullet();
};

vector<CBullet *> Bullets;


Then just use something like:

void Shoot()
{
Bullets.push_back(new CBullet());
}


And loop through them:

void DrawBullets()
{
for (int i=0; i<Bullets.size(); i++)
{
//do stuff here
}
}


But I guess an array of bullets would work just as fine. Personally, I prefer to use vectors.

You will need to add some form of update function to the class to manage the movement and ultimately the collision of your bullet, and if you look at the thread it also shows you a bit further down how to remove bullets from a vector array.

But as a word of advice;

1. Get rid of the magic numbers, http://en.wikipedia.org/wiki/Magic_number_(programming).
2. Your naming convention, your variable names should as clear as water to understand, "once2" & "frame2 doesn't mean anything in my eyes without a comment or knowing what it's purpose is for. In-fact i can't see personally why you need that last if statement you have. If i could hazard a guess it looks like to be some form of reloading code? As to an example of making your variables clearer, lets take the spacebar boolean you have. Because of your comment, i know that it's changed when the space bar is pressed. To make it clear as crystal however, you can simple change spacebar to something like "hasSpaceBarBeenPressed".