문제

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; }
도움이 되었습니까?

해결책

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.
            }
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top