• Advertisement
Sign in to follow this  

shoot bullets

This topic is 2076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

[font="Consolas"][size="2"][font="Consolas"][size="2"]void shoot_bullet()[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]for[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]([/font][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] j=0;j<=510;j++)[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]{[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]ball_move++;[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]Sleep(1);[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]}[/font][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"]I want to shoot like in asteroids.[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement