You are on page 1of 3

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

void shoot_bullet() { for(int j=0;j<=510;j++) { ball_move++; Sleep(1); } } I want to shoot like in asteroids. Selective Quote Ad: Always have your stuff when you need it with Dropbox. 2GB account is free! #2 Trienco Members - Reputation: 641 Like 0Likes 1Likes Like Posted 15 May 2012 - 10:38 And how would you ever see me loop while you do that? lock the whole application PM the bullet do anything if you completely halt your ga How would you ever have more than one bullet if you b like that?

Shooting a bullet isn't a matter of calling a single function. You need to creat e a bullet and on every iteration through your main loop you update, check for c ollision, handle collisions if necessary and render your bullet. f@dzhttp://festini.device-zero.de Selective Quote #3 BeerNutts Members Like 1Likes 2Likes Like - Reputation: 228

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 ju st 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 n eed 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 remove d }; 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 l ist 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 a dd 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 bu llet 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; } }

You might also like