0

SDL を使用して C++ で shmup ゲームのプロトタイプを作成しています...現在、クラスを使用せずに基本を機能させようとしています。今、私はそれを持っているので、それは複数の弾丸を発射しますが、カウンターがリセットされる方法が原因であると私は信じています. 、制限が画面に表示されていなくても、もう一度撃つことができるようになるまでに遅延が発生することがあります...そして、プレイヤーキャラクターが突然右にジャンプして、上下にしか移動できないことがあります. これをスムーズに撮影するにはどうすればよいですか?私はすべての関連コードを含めました...

[編集] これを理解したら、それをクリーンアップしてすべてのクラスに移動するつもりであることに注意してください...これは単純なプロトタイプであるため、ゲームの基本をプログラムすることができます.

[edit2] ePos は敵の位置、pPos はプレイヤーの位置です。

//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; }
4

1 に答える 1

0

これは私がコメントで提案していたようなものです。頭のてっぺんから書いたものですが、私が話していることの一般的なアイデアを提供します..

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.
            }
        }
    }
}
于 2012-01-02T05:14:52.740 に答える