Thread: Shooting

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    14

    Shooting

    I'm currently working on a game for my c++ class, it involves a space ship moving and shooting. I have a dilemma, I don't know how to allow multiple bullets to be shot without using a variable for each one, any ideas?
    yo

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    why don't you just use a variable to store each bullet's data? isn't this what variables are for?

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    If you actually read my post, you wouldn't have said that, please quit life.
    yo

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Well, I've written a few classes similar to what you want and in my program an object is created for each bullet that's shot.

    I don't think you can have multiple bullets without having a variable for each bullet.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    An array of bullets perhaps
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    somebody told me to use "linked lists" is this true? and if so, how do i use them?
    yo

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You can use linked lists although there is really no reason to in this case.
    Code:
    #define PI 3.14159
    #define DEGTORAD(radangle) ((double)radangle*PI/180.0)
    
    #define MAXBULLETS 255
    struct point2D
    {
       double x;
       double y;
    };
    
    struct Bullet
    {
       point2D ScreenPos;
       point2D VectorComponent;
       double Speed;
       int Active;
    };
    
    
    Bullet Bullets[MAXBULLETS];
    
    void ShootBullet(point2D tOrigin,double tSpeed,int angle)
    {
      if (NumBullets<MAXBULLETS)
      {
         for (int i=0;i<NumBullets;i++)
         {
            if (Bullets[i].Active==0)
            { 
               Bullets[i].ScreenPos.x=tOrigin.x;
               Bullets[i].ScreenPos.y=tOrigin.y;
               Bullets[i].VectorComponent.x=cos(DEGTORAD(angle));
               Bullets[i].VectorComponent.y=sin(DEGTORAD(angle));
               Bullets[i].Speed=tSpeed;
               Bullets[i].Active=TRUE;
            }
        }
    }
    
    void UpdateBullets(void)
    {
      for (int i=0;i<MaxBullets;i++)
      {
        if (Bullets[i].Active)
        {
           Bullets[i].ScreenPos.x+=(Bullets[i].VectorComponent.x*Bullets[i].Speed);
           Bullets[i].ScreenPos.y+=(Bullets[i].VectorComponent.y*Bullets[i].Speed);
         }
       }
    }
    
    void DisplayBullets(void)
    {
      for (int i=0;i<MaxBullets;i++)
      {
        if (Bullets[i].Active)
        {
           //Display bullet at Bullets[i].ScreenPos.x,Bullets[i].ScreenPos.y
        }
      }
    }

    The inherent problem with this is that you must scan the entire array to see which bullets are active. There are ways to fix this but I did not show them here. With a linked list, you don't have to scan the entire list....because if the bullet is in the list it is active.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > somebody told me to use "linked lists" is this true?
    Only if you want
    a) a more difficult programming challenge
    b) your game slowing down as the screen fills up with slow-moving bullets.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    A linked list is generally (though not necessarily) based on dynamic memory allocation, and is advantageous over a statically allocated array if [1] you don't know how many bullets you need in advance [2] there could be a large number of bullets. As you're using C++, you've no need to implement your own linked list unless you particularly want to as you can use the standard library containers instead.

    If you've not come across linked lists before then I would strongly advise you to avoid them, as you'll get into some low level pointer manipulation and probably spend more time than you'd wish to debugging.

    If this was a commercial game you were writing then you'd already be considering various techniques in your implementation to give you the best speed possible, but for what you're currently doing I would suggest you go for the simplest implementation that you're comfortable with, e.g. an array of bullet variables or a standard library container, and only consider something else if you find that when you run it the programme speed is unacceptable.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    My code will do what you want.

    It uses a struct and nicely defines a bullet. If you want more, then create a class for it or something.

    If you want to use a linked list either use the STL or if that's too confusing, create your own.

    It doesn't matter if your code uses a doubly linked list with a binary space partition tree - we don't care - it just matters if it works. So use what works for you.

    Besides, if you have more than 255 bullets on the screen at any one time....that's close to organized chaos. No one would be able to play that anyways.

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    Thank you a lot Bubba.
    yo

  12. #12
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    looks like someone deleted some posts here, why didn't they remove the "quit life" part from zowen's post?

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    because you really do need to
    yo

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you two are going to keep this up, I'm closing this thread
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL shooting game
    By sl4nted in forum Game Programming
    Replies: 6
    Last Post: 12-01-2006, 10:37 PM
  2. want to play a useless shooting game i made?
    By Xterria in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-01-2002, 05:34 PM
  3. Need help trouble shooting menu PLZ!
    By Bazza in forum C Programming
    Replies: 7
    Last Post: 07-08-2002, 08:34 AM
  4. School Shooting in Germany
    By Golden Bunny in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 04-27-2002, 01:47 PM
  5. Shooting yourself
    By Theologian in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-24-2001, 05:09 AM