[SDL]Simple tank shooting a bullet problem

Started by
3 comments, last by TheUmpteenth 16 years, 10 months ago
Hi, After reading some tutorials for SDL I decided to make a simple test program. I started writing and after some time I made a tank that is controlable with the keyboard by pressing left and right arrow buttons, moving left and right resp. Here is how I've done it: class Tank, which contains int x,y,velocity,width,height,speed and methods move (to calculate position for next frame), show(blit the tank on screen), handle_input(detects what keys are pressed and calculates velocity which is needed for move() ) then in main, ill just paste the important stuff:


    //Quit flag
    bool quit = false;

    
    //The tank
	Tank mijnTank (40,160, 50, 30, 5);
  

    //While the user hasn't quit
    while( quit == false )
    {

        
		//Fill the screen white
        SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

        //While there's events to handle
        while( SDL_PollEvent( &event ) )
        {
            //Handle events for the tank
            mijnTank.handle_input();
            
            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }
        
        //Move the tank
        mijnTank.move();
        
        //Show the tank on the screen
        mijnTank.show();
        
        //Update the screen
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;    
        }
        
    }
So basicly every frame it calls show() and move() This works well. Now I want to make the tank shoot a dot when the user presses SPACE. Therefore, for spawning the bullet, I need the tank's x,y,height and width. I tried making a Dot class with a similar show() and move(), and to Tank class I added shoot(), which makes a new Dot (bullet) and calls the bullet.move() and bullet.show(). shoot() is called in handle_input() when space is pressed. However this doesn't work. I think I need some other kind of system. Does anyone have tips how to design my code? PS: It's a bit vague how I explained the situation, if you need to see the whole code I'll upload it later.
Advertisement
Could be that you are not storing the bullet instance anywhere, so that when the method which spawns a bullet finnishes, it is lost.

I had a similar problem once, and found that linked lists are good for storing things that there could be any number of.

you create the bullet, add it to the list, and remove it from the list when its done.

hope that helps you.
Hey man thanks for the tip, works like a charm :)
Just a note: you don't specifically need a linked list for that. There are various container classes available, most prominently std::vector and std::deque. For now, you probably don't need to worry about their differences and their implementation, you can simply use them and save yourself some time and trouble. :)
Create-ivity - a game development blog Mouseover for more information.
Np glad to be of help.

BTW, Captain P is right, these containers are also good, although I'm not the biggest fan of the C++ Standard Library, it has it's uses...


using namespace std;

#include <list>//... or #include <deque> or #include <vector>

This topic is closed to new replies.

Advertisement