• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Need some help with bullets...

## Recommended Posts

Well I''ve tried other forums and they''re no help. I''m trying to create a galaxian/space invader clone, and everything should be pretty easy EXCEPT the bullets! I can''t think of how to implement the shooting... Are there any tutorials on the net (sure they''re are) and could you link me to them? THanks.

##### Share on other sites
Try looking up projectiles.

Why don''t you write a bullet class and spawn/delete bullets as nessesary. The class would include position, direction , velocity as well as sounds/graphics (or pointers to) and damage information/type (explosion radius etc).

,Jay

##### Share on other sites
Thanks!

That''s what I have so far, but I think I am thinking too technically . It has to be easier than I am thinking, because right now, I have about 3 different classes and structs just to manage the bullets...

And they still don''t work!

~Dwarf

##### Share on other sites
hiya dwarf, do you already have a timer control in your game? well, everytime the player presses the spacebar, activate the timer for the bullet, ie,

when the user presses spacebar:
fire = true;

if(fire = true)
{
}

well, that''s all what i can think off, hey if you''re still having a problem, ICQ me, 141451718, i''ll happily help you out,

##### Share on other sites
Yes you can increment the Y by a static velocity like 5. (y-=5) but most of the time you want to have a velocity which can slow down & go faster depending on certian conditions. So it''s easier to do something like this

struct bullet
{
int x;
int y;
int vel;
};

bullet mybullet;
int InitialVelocity = 5;

// Do this in your startup initialization.
mybullet.vel = InitialVelocity;

// when the fired button is pressed.
mybullet.x = ( (ships.x1 + ships.x2)/2); // this puts the bullet at the middle location of your ship.

mybullet.y = ships.y1;

// do this in the frame update
mybullet.y += mybullet.vel;

now if you want the bullets to move faster say you pickup a "rapid fire" power up, you can just do...
mubullet.vel = 10; and your bullets will move alot faster. Or should you need to slow things down you can just do mybullet.vel = 2;

##### Share on other sites
I forgot some stuff so let me do this again..

// is the bullet alive or dead?
#define ALIVE = 0;

struct bullet
{
int x;
int y;
int vel;
int state;
};

bullet mybullet;
int InitialVelocity = 5;

// Do this in your startup initialization.
mybullet.vel = InitialVelocity;

// when the fired button is pressed.
mybullet.x = ( (ships.x1 + ships.x2)/2); // this puts the bullet at the middle location of your ship.

mybullet.y = ships.y1;
mybullet.state = ALIVE;

// do this in the frame update
if(mybullet.state == ALIVE)
{
mybullet.y += mybullet.vel;
}

// check to see if the bullet is dead or not..
if( CollideWithAlienOROffScreen(mybullet) != FALSE )
{
// turn it off.
}

now if you want the bullets to move faster say you pickup a "rapid fire" power up, you can just do...
mubullet.vel = 10; and your bullets will move alot faster. Or should you need to slow things down you can just do mybullet.vel = 2;

now keep in mind this is just one bullet, if you want more than one bullet you''ll need to use an array or linked list of bullet structures and modify the code accordingly.

##### Share on other sites
You could implement a linked list... I don''t know if you have already...
Example:
class Bullet{public:   float x, y;   float xvel, yvel;   Bullet *next_bullet;   Bullet *last_bullet;   void AddBullet(Bullet *pParent);   void DeleteBullet(void);};void Bullet::AddBullet(Bullet *pParent){   Bullet *pNew;   pNew = new Bullet;   pNew->last_bullet = pParent;   pNew->next_bullet = NULL;}void Bullet::DeleteBullet{   Bullet *next;   Bullet *last;   next = this->next_bullet;   last = this->last_bullet;   // skip current bullet, then delete   next->last_bullet = last;   last->next_bullet = next;   delete [] this;}

This probably won''t work (so don''t compile it! ), but it might get the idea across... And then during the gameloop you''d have to create some kind of loop that walks through each bullet and does its ai. Look up a linked list tutorial in the Articles section if you don''t understand them...
Goodluck!
-Jesse

| The Hitchhiker''''s Guide to Programming |
"That is not dead which can eternal lie,
And with strange aeons even death may die."
-H.P. Lovecraft

##### Share on other sites
If you have a copy of UnrealEd look at the weapon classes there, they are all subclassed from a standard weapon class with different Touch (bullet projectile hit) etc. methods.

It makes it very easy to add new weapons etc (Exploding Pigeon Crossbow).

,Jay

##### Share on other sites
quote:
Original post by Aphelion
You could implement a linked list... I don''t know if you have already...
Example:
class Bullet{public:   float x, y;   float xvel, yvel;   Bullet *next_bullet;   Bullet *last_bullet;   void AddBullet(Bullet *pParent);   void DeleteBullet(void);};void Bullet::AddBullet(Bullet *pParent){   Bullet *pNew;   pNew = new Bullet;   pNew->last_bullet = pParent;   pNew->next_bullet = NULL;}void Bullet::DeleteBullet{   Bullet *next;   Bullet *last;   next = this->next_bullet;   last = this->last_bullet;   // skip current bullet, then delete   next->last_bullet = last;   last->next_bullet = next;   delete [] this;}

This probably won''t work (so don''t compile it! ), but it might get the idea across... And then during the gameloop you''d have to create some kind of loop that walks through each bullet and does its ai. Look up a linked list tutorial in the Articles section if you don''t understand them...
Goodluck!
-Jesse

| <a href="http://www.angelfire.com/games4/parsec/index.html">The Hitchhiker''s Guide to Programming</a> |
"That is not dead which can eternal lie,
And with strange aeons even death may die."
-H.P. Lovecraft

The linked list is a good idea, just remember DO NOT call the ADDBULLET and DELETEBULLET functions in your main game loop. New and Delete as well as malloc and free are expensive time wise, and should never be done durring game play.

• ### Forum Statistics

• Total Topics
627737
• Total Posts
2978873

• 10
• 10
• 21
• 14
• 12