Jump to content
  • Advertisement
Sign in to follow this  
HawkAgent

[SDL]Simple tank shooting a bullet problem

This topic is 4177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!