Thread: bullets [2D]

  1. #1
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77

    bullets [2D]

    Hello,


    Long time no post! Well, I'm creating a game uisng Allegro, and dev-c++. But I have ran into a problem in my game. I have a struct bullet. I know how to check the space bar, but what I don't know is how to make it so it creates a NEW bullet, everytime he/she pushes the space bar, then, how do I make it so it updates the bullets location so it looks like its flying through the air?? I can't find anyware about a for each loop. If you could help, thanks. Here is my source code. **NOTE I havn't put in the bullet's graphic yet.

    Code:
    #include <allegro.h> //For allegro graphics lbrary
    
    
    //Structs
    struct enemy {
       int x;
       int y;   
    };    
    
    struct player {
        int x;
    };    
    
    struct bullet {
        int x;
        int y;   
    };    
    
    int num_of_enemys;
    
    
    //Main Function
    int main(int argc, char *argv[]){
    
    //Start Allegro    
    allegro_init(); // Start allegro
     //Set color depth
    set_color_depth(32); 
     //Set Graphics mode
    set_gfx_mode(GFX_AUTODETECT,800,600,0,0);
    
    
    
    //Images
    BITMAP *strongbad = load_bitmap("./graphics/strongbad.bmp",NULL);
    BITMAP *homestar = load_bitmap("./graphics/homestarrunner.bmp",NULL);
    BITMAP *background = load_bitmap("./graphics/background.bmp",NULL);
    BITMAP *buffer = create_bitmap(800,600);
    
    
    //create objects
    player strong_bad;
    strong_bad.x = 500;
    
    
    
    install_keyboard();
    
     
                      
    while(!key[KEY_ESC]){
        
        //Check keyboard
        if(key[KEY_LEFT]){
            strong_bad.x = strong_bad.x - 5;
        }
        
        if(key[KEY_RIGHT]){
            strong_bad.x = strong_bad.x + 5;
        }
        
        if(key[KEY_SPACE]){
         bullet dot;
         dot.x = strong_bad.x;
         dot.y = 400   
        }    
        
        
        //Check players position
        if(strong_bad.x <=  -32){
            strong_bad.x = 790;
        }
        if(strong_bad.x >= 832){
            strong_bad.x = 10;
        }
            
                                        
        
       clear_bitmap(buffer);
       draw_sprite(buffer,background,0,0);
       masked_blit(strongbad,buffer,0,0,strong_bad.x,400,32,55);
       
       blit(buffer,screen,0,0,0,0,800,600);
       
    
    }    
    destroy_bitmap(buffer);
    destroy_bitmap(strongbad);
    destroy_bitmap(homestar);
    destroy_bitmap(background);
     
    return 0;    
    
    
        
    }
    END_OF_MAIN()

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I had this problem when I began making my first game. I made a bool variable in the struct to keep track of when the bullet had been fired, then if it had it would update.

    Here's some example code:
    Code:
    struct bullet{
    int x, y;
    bool active=false;
    }
    
    int main()
    {
     bullet ammo;
    
     if[SPACEBAR]
        ammo.active=true;
    
     if(ammo.active=true)
       move bullet();
    
    return 0;
    Not a complete program by any means, but it should give you the general idea. That method can also be easily converted into using arrays of bullets, just use a couple for loops.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    struct point2D
    {
    float x;
    float y;
    };

    struct bullet
    {
    point2D Position;
    point2D VelocityVector;

    float Speed;

    bool Active;
    };

    This structure will help.

    You can then use an array, vector, linked list, or any other data structure you deem appropriate to store the bullets.

    Then when one is fired you set active to true and either add it to the active list, or search through the list in the update function and update the one's that are active.

    I will show the array based method.

    Code:
    void BulletSystem::UpdateBullets(float FrameTime)
    {
      for (int i=0;i<NumBullets;i++)
      {
        if (BulletList[i].active)
       {
           BulletList[i].Position.x+=(BulletList[i].VelocityVector.x*BulletList[i].Speed)*FrameTime;
           BulletList[i].Position.y+=(BulletList[i].VelocityVector.y*BulletList[i].Speed)*FrameTime;
       }
     }
    }
    Problem with this method is you must search all bullet 'slots' per se in order to find which ones are active. A better way would be to keep an active list - if the bullet is in the list then you know it's active. This eliminates a lot of overhead in the function.

  4. #4
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169
    Quote Originally Posted by Bubba
    A better way would be to keep an active list - if the bullet is in the list then you know it's active. This eliminates a lot of overhead in the function.
    yea that's how i've always done it. just check after updating and remove the bullets from the linked list that are out of bounds.
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bouncing bullets
    By dydoko in forum Game Programming
    Replies: 8
    Last Post: 09-18-2003, 08:31 PM
  2. bullets again
    By lambs4 in forum Game Programming
    Replies: 1
    Last Post: 09-12-2003, 02:39 PM
  3. bullets
    By lambs4 in forum Game Programming
    Replies: 10
    Last Post: 08-13-2003, 06:32 AM
  4. pic movement
    By pode in forum Game Programming
    Replies: 31
    Last Post: 08-21-2002, 09:30 PM
  5. Allegro help-o
    By Vicious in forum Game Programming
    Replies: 109
    Last Post: 06-11-2002, 08:38 PM