Here is some example code:
Simple shot class:
class Shot
{
public:
Vect2d pos; //position in pixels of the shot
Vect2d motion; //direction and speed of the shot
float distance_travelled; //how far the shot has travelled over it's lifetime so far
Shot(void);
};
to collide shots with things, do something like:
bool Ship::CheckShotCollision(Asteroid* theAsteroid)
{
for(int i = shots.size() - 1;i >= 0; --i)
{
//did we collide?
if( (shots[i]->pos - theAsteroid->pos).LengthSqr() <= theAsteroid->radius_squared)
{
//we collided with this shot
delete shots[i];
shots.erase(shots.begin() + i);
return true;
}
}
return false; //if we got here, no collision
}
Make a list of shots somewhere (like in a Ship class)
float time_since_last_shot;
std::vector<Shot *> shots;
To fire a new shot, do something like:
bool Ship::Shoot(void)
{
if(shots.size() >= MAX_SHOTS) return false;
if(time_since_last_shot < TIME_BETWEEN_SHOTS) return false;
//add a new shot
Shot *nshot = new Shot();
//create its motion vector from the ship's facing angle
nshot->motion.x = -sin(angle);
nshot->motion.y = -cos(angle);
nshot->pos = pos + nshot->motion * 16.f; //move the shot to the front of the ship
nshot->motion *= SHOT_SPEED; //scale the motion up to speed
shots.push_back(nshot);
time_since_last_shot = 0.f;
return true;
}
To draw them, do:
for(int i = 0; i < theShip.shots.size(); ++i)
{
theShotsSprite->SetPosition(theShip.shots[i]->pos.x, theShip.shots[i]->pos.y);
App->Draw(*theShotsSprite);
}