Thread: bullets

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    bullets

    This code is suppose to be for managing bullets in my space invaders clone. The part that I have trouble on is firing the bullets and tracking which ones are active and whichs are not. Any help on this?
    Code:
    struct bullet{
       int x;
       int y;
       int alive;
    };
    
    struct bullet bullets[256];
    int bulletsUsed = 0; // tracks how many bullets are active
    void createBullet(struct bullet bullets[])
    {
        bulletsUsed++;
        bullets[bulletsUsed].x = ship.x
        bullets[bulletsUsed].y = ship.y
        bullets[bulletsUsed].alive =  true
    }
    void fireBullets(struct bullet bullets[])
    {
       int i;
       for(i = 0; i < bulletsUsed; i++)
       { 
          bullets[i].y--;
          if(bullets[i].y < 0)
             bullets[i].alive = false;
        }
    
    }
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I'd suggest using a linked list with all the active bullets in it, and when a bullet becomes non-active, remove it from the list.
    Away.

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Re: bullets

    Code:
    struct bullet{
       int x;
       int y;
       bool alive;
    };
    I highlighted what alive should be (it was an int, but an int can't return true or false, only booleans (bool) can).

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Yes, I know alive should be a boolean, but I could never get them to compile. Bool is typecasted as an int anyways, isn't it?

    I'll use a linked list later, but for now an array should be good enough.
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by lambs4
    Yes, I know alive should be a boolean, but I could never get them to compile. Bool is typecasted as an int anyways, isn't it?

    I'll use a linked list later, but for now an array should be good enough.
    I dunno, you could just keep it as an int, and replace the false's w/ 0's (zero) and true's with 1's (one). Since false == 0, and true != 0 (ala 1)

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I think the reason why it wouldn't compile with 'bool' is because the bool data type is C++, while your code is C.

    But substituting 0 for false and 1 as true is just as easy.

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Nutshell
    I think the reason why it wouldn't compile with 'bool' is because the bool data type is C++, while your code is C.

    But substituting 0 for false and 1 as true is just as easy.
    I always thought that bool is c/++, all well.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Location
    Melbourne, Australia
    Posts
    92
    As a general rule, "BOOL" goes with "TRUE" and "FALSE", "bool" goes with "true" and "false".
    psychobrat at gmail

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    if you have a problem with bools in your C complier, define them yourself.

    Code:
    #ifndef                 BOOL                //add only once
    
    #define                 BOOL                int
    #define                 FALSE                0
    #define                 TRUE                  1
    
    #endif
    put this in a header.

    PS
    your array index should be incremented (increased) after you have used an array element not before.


    Code:
    int bulletsUsed = 0; // tracks how many bullets are active
    void createBullet(struct bullet bullets[])
    {
        bulletsUsed++;//this will mean the 0 element is never used
    I would also init the array to zero with a loop

    or

    ZeroMemory(&bullets,sizeof(bullet)*256);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Thanks for pointing that out. For init the array to zero, I could have also used
    Code:
    bullets[256] = {{0}}
    ?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  11. #11
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    You cannot simply increment or decrement the array size because it is not safe to assume that it was the last bullet in your array that got deactivated. It could be any bullet , i.e. the third out of 20 active bullets. If you decrese the bulletcount now, you would exclude bullet 20 while still checking bullet 3. Three sollutions: 1) always loop through the entire array as described below, 2) move all array entries above the deleted one, one index up (-1) once you delete a bullet or 3) use linked lists.

    If you just want to take the easy way, do this: Init the entire array with false so that there is no bullet. Once you add a bullet with createBullet, you loop through the array until you find an empty spot (enabled=false) and set x and y and activate the bullet.

    In firebullets, you loop through the entire 256 bullets and move those that are active. You don't need "BulletCount" in this implementation.


    Of course thats just the simplest sollution. Linked lists would be a lot better and faster here since you can really remove bullets from the list. You could do the same with your array if you move all bullets with an index greater than the one you want to delete one index up in your array, but you can imagine how slow that is.
    Last edited by darksaidin; 08-13-2003 at 06:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bullets [2D]
    By St0rmTroop3er in forum Game Programming
    Replies: 3
    Last Post: 10-04-2004, 11:26 PM
  2. bouncing bullets
    By dydoko in forum Game Programming
    Replies: 8
    Last Post: 09-18-2003, 08:31 PM
  3. bullets again
    By lambs4 in forum Game Programming
    Replies: 1
    Last Post: 09-12-2003, 02:39 PM
  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