View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# shoot bullets

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1phil67rpg  Members

Posted 15 May 2012 - 07:17 PM

I am trying to write a very simple routine that shoots bullets.here is my function.

void shoot_bullet()
{
for(int j=0;j<=510;j++)
{
ball_move++;
Sleep(1);
}
}
I want to shoot like in asteroids.

### #2Trienco  Members

Posted 15 May 2012 - 10:38 PM

And how would you ever see the bullet do anything if you completely halt your game loop while you do that? How would you ever have more than one bullet if you block the whole application like that?

Shooting a bullet isn't a matter of calling a single function. You need to create a bullet and on every iteration through your main loop you update, check for collision, handle collisions if necessary and render your bullet.
f@dzhttp://festini.device-zero.de

### #3BeerNutts  Members

Posted 16 May 2012 - 12:50 PM

Phil,

What is a bullet? How would you define a bullet it code? Let me try and help you, with some code. I'm only going to use structures to keep it simple.
// simple enum for colors
enum etColors
{
COLOR_RED,
COLOR_BLUE,
COLOR_GREEN,
COLOR_WHITE
{;

// a simple bullet (real bullet would have images attached to them, but we're just going to assume it's a colored rectangle)
struct tBullet
{
float XPosition; // upper left of bullet
float YPosition;
float XVelocity; // should be in pixels/second, but you can make it what you need for it to work for now
float YVelocity;
int Height;
int Width;
int Damage;
int Owner; // 0 is the Player, other values are the enemies
etColors eColor; // what color is this bullet
bool IsActive; // true if this bullet is still active, false, it can be removed
};

Does all the above variable make sense?

Now, we want to be able to have many bullets on the screen, so we would want a list of bullets. Just make this a global variable in your main .cpp file:
std::list<tBullet> BulletList;

(BTW, only to help make it easier do I suggest making it a global; I would never tell anyone but a struggling beginner to do this).
Somewhere, your player or your enemy will shoot a bullet, and you will want to add it to the list. You could do this (assuming Bulletlist is global).
// just a temp define, you would really base the bullet speed on what type of bullet the player is shooting
#define PLAYERS_BULLET_SPEEDX 0.0f
#define PLAYERS_BULLET_SPEEDY -8.0f // Assumes bullet go straight up from ship

// This assumes the GetInput function has told your player to fire a bullet
void PlayerUpdate()
{
if (FireBullet) {
tBullet NewBullet;
// setup the new bullet
NewBullet.XPosition = PlayersXPosition + PlayerShipWidth()/2;
NewBullet.YPosition = PlayersYPosition;
NewBullet.XVelocity = PLAYERS_BULLET_SPEEDX;
NewBullet.YVelocity = PLAYERS_BULLET_SPEEDY;
NewBullet.Height = 10;
NewBullet.Width = 10;
NewBullet.Damage = 100;
NewBullet.Owner = 0; // main ship owns it
NewBullet.eColor = COLOR_WHITE;
NewBullet.IsActive = true;

// add it to the bullet list
BulletList.push_back(NewBullet);
}
}


Now, you'll need to loop through all the bullets in the list and update them.

void UpdateBullets()
{
std::list<tBullet>::iterator it;

// All bullets should be active at this point
for (it = BulletList.begin(); it != BulletList.end(); ++it) {
// Move the bullet
it->XPosition += it->XVelocity;
it->YPosition += it->YVelocity;

// Check if the bullet has hit anything
if (CheckBulletCollision(*it)) {
//mark inactive
it->IsActive = false;
continue;
}
// Check if the bullet is off the screen
if (CheckBoundary(it->XPosition, it->YPosition) {

//mark inactive
it->IsActive = false;
continue;
}

// Draw the bullet
GraphicsRectDraw(it->XPosition, it->YPosition, it->Width, it->Height, it->eColor);
}

// loop through and delete all the inactive bullets
for (it = BulletList.begin(); it != BulletList.end(); ) {
if (!it->IsActive) {
it = BulletList.erase(it);
continue;
}
++it;
}
}


Anyway, that's the general idea. Good Luck.

Edited by BeerNutts, 16 May 2012 - 12:52 PM.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.