# Shooting

• 12-19-2003
zowen
Shooting
I'm currently working on a game for my c++ class, it involves a space ship moving and shooting. I have a dilemma, I don't know how to allow multiple bullets to be shot without using a variable for each one, any ideas?
• 12-19-2003
glUser3f
why don't you just use a variable to store each bullet's data? isn't this what variables are for?
• 12-19-2003
zowen
If you actually read my post, you wouldn't have said that, please quit life.
• 12-19-2003
The Dog
Well, I've written a few classes similar to what you want and in my program an object is created for each bullet that's shot.

I don't think you can have multiple bullets without having a variable for each bullet.
• 12-19-2003
Salem
An array of bullets perhaps
• 12-19-2003
zowen
somebody told me to use "linked lists" is this true? and if so, how do i use them?
• 12-19-2003
VirtualAce
You can use linked lists although there is really no reason to in this case.
Code:

```#define PI 3.14159 #define DEGTORAD(radangle) ((double)radangle*PI/180.0) #define MAXBULLETS 255 struct point2D {   double x;   double y; }; struct Bullet {   point2D ScreenPos;   point2D VectorComponent;   double Speed;   int Active; }; Bullet Bullets[MAXBULLETS]; void ShootBullet(point2D tOrigin,double tSpeed,int angle) {   if (NumBullets<MAXBULLETS)   {     for (int i=0;i<NumBullets;i++)     {         if (Bullets[i].Active==0)         {           Bullets[i].ScreenPos.x=tOrigin.x;           Bullets[i].ScreenPos.y=tOrigin.y;           Bullets[i].VectorComponent.x=cos(DEGTORAD(angle));           Bullets[i].VectorComponent.y=sin(DEGTORAD(angle));           Bullets[i].Speed=tSpeed;           Bullets[i].Active=TRUE;         }     } } void UpdateBullets(void) {   for (int i=0;i<MaxBullets;i++)   {     if (Bullets[i].Active)     {       Bullets[i].ScreenPos.x+=(Bullets[i].VectorComponent.x*Bullets[i].Speed);       Bullets[i].ScreenPos.y+=(Bullets[i].VectorComponent.y*Bullets[i].Speed);     }   } } void DisplayBullets(void) {   for (int i=0;i<MaxBullets;i++)   {     if (Bullets[i].Active)     {       //Display bullet at Bullets[i].ScreenPos.x,Bullets[i].ScreenPos.y     }   } }```

The inherent problem with this is that you must scan the entire array to see which bullets are active. There are ways to fix this but I did not show them here. With a linked list, you don't have to scan the entire list....because if the bullet is in the list it is active.
• 12-20-2003
Salem
> somebody told me to use "linked lists" is this true?
Only if you want
a) a more difficult programming challenge
b) your game slowing down as the screen fills up with slow-moving bullets.
• 12-20-2003
Omnius
A linked list is generally (though not necessarily) based on dynamic memory allocation, and is advantageous over a statically allocated array if [1] you don't know how many bullets you need in advance [2] there could be a large number of bullets. As you're using C++, you've no need to implement your own linked list unless you particularly want to as you can use the standard library containers instead.

If you've not come across linked lists before then I would strongly advise you to avoid them, as you'll get into some low level pointer manipulation and probably spend more time than you'd wish to debugging.

If this was a commercial game you were writing then you'd already be considering various techniques in your implementation to give you the best speed possible, but for what you're currently doing I would suggest you go for the simplest implementation that you're comfortable with, e.g. an array of bullet variables or a standard library container, and only consider something else if you find that when you run it the programme speed is unacceptable.
• 12-20-2003
VirtualAce
My code will do what you want.

It uses a struct and nicely defines a bullet. If you want more, then create a class for it or something.

If you want to use a linked list either use the STL or if that's too confusing, create your own.

It doesn't matter if your code uses a doubly linked list with a binary space partition tree - we don't care - it just matters if it works. So use what works for you.

Besides, if you have more than 255 bullets on the screen at any one time....that's close to organized chaos. No one would be able to play that anyways.
• 12-20-2003
zowen
Thank you a lot Bubba.
• 12-20-2003
glUser3f
looks like someone deleted some posts here, why didn't they remove the "quit life" part from zowen's post? :rolleyes:
• 12-20-2003
zowen
because you really do need to
• 12-20-2003
Salem
Well if you two are going to keep this up, I'm closing this thread