0

I'm working on a prototype for a shmup game in C++ using SDL...Right now I'm just trying to get the basics working without using classes. Now, I have it so it shoots multiple bullets, but it behaves strangely which I believe is because of the way the counter is reset...The bullets will appear and disappear, and some will keep blinking in the original spot they were shot at, and there will sometimes be a delay before any can be shot again even if the limit isn't on the screen...And sometimes the player character will suddenly jump way to the right and only be able to move up and down. How do I fix this so it smoothly shoots? I've included all relevent code...

[edit] Please note that I intend on cleaning it up and moving it all to a class once I get this figured out...This is just a simple prototype so I can have the basics of the game programmed in.

[edit2] ePos is the enemy position, and pPos is the player position.

//global
SDL_Surface *bullet[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
bool shot[10];
int shotCount = 0;
SDL_Rect bPos[10];

//event handler
case SDLK_z: printf("SHOT!"); shot[shotCount] = true; ++shotCount; break; 

//main function
for(int i = 0; i <= 9; i++)
{
    shot[i] = false;  
    bullet[i] = IMG_Load("bullet.png");
}

//game loop
for(int i = 0; i <= shotCount; i++)
{    
    if(shot[i] == false)
    {
        bPos[i].x = pPos.x;
        bPos[i].y = pPos.y; 
    }
    if(shot[i] == true)
    { 
        bPos[i].y -= 8;
        SDL_BlitSurface(bullet[i], NULL, screen, &bPos[i]);

        if( (bPos[i].y + 16 == ePos.y + 16) || (bPos[i].x + 16 == ePos.x + 16) )
        {
            shot[i] = false;
        }
        else if(bPos[i].y == 0)
        {
            shot[i] = false;
        }
    }
}

if(shotCount >= 9) { shotCount = 0; }
mad_manny
  • 1,081
  • 16
  • 28
user1122136
  • 98
  • 2
  • 8
  • 1
    You're not showing enough code. What is ePos? I believe that, in your quest for simplicity, you've made things more complicated. IMO you should create a simple bullet class and then just Bullet::update() in your game loop, etc. –  Jan 02 '12 at 02:55
  • Okay...I'm working on moving everything to a class right now. Should I still utilize the for loops and arrays, or should I look into using the new and delete operators? – user1122136 Jan 02 '12 at 04:00

1 Answers1

0

Here is something like I was suggesting in the comment. It was just written off the top of my head but it gives you a general idea of what I'm talking about..

class GameObject
{
    public:
        int x;
        int y;
        int width;
        int height;
        int direction;
        int speed;

    GameObject()
    {
        x = 0;
        y = 0;
        width = 0;
        height = 0;
        direction = 0;
        speed = 0;
    }

    void update()
    {
        // Change the location of the object.
    }

    bool collidesWidth(GameObject *o)
    {
        // Test if the bullet collides with Enemy.
        // If it does, make it invisible and return true
    }
}

GameObject bullet[10];
GameObject enemy[5];

while(true)
{
    for(int x=0; x<=10;x++)
    {
        bullet[x].update();
        for(int y=0;y<=5;y++)
        {
            if(bullet[x].collidesWith(&enemy[y])
            {
                // Make explosion, etc, etc.
            }
        }
    }
}
  • Ah...Thank you. It seems to work so far...I had already written the class by the time I saw this, but wasn't sure how to actually handle all of the bullets. – user1122136 Jan 02 '12 at 06:14
  • Yeah, the idea is to make the bullet kind-of autonomous which cleans up your game loop and lets you worry about other things. –  Jan 02 '12 at 21:42