Qt中的2D游戏:初始化怪物并将动态分配的数组作为指针参数传递

时间:2013-07-24 03:08:33

标签: c++ arrays qt 2d

我正在使用Qt框架创建2D游戏。虽然我在其他一些语言方面有一些经验,但C ++对我来说是一种新语言。

我的第一个问题是怪物。我应该如何初始化(并存储它们)?我的游戏的每个级别都有不同数量的怪物。此时我以这种方式初始化它们:

    Monster *mobs;
    mobs = new Monster[mobsQuantity];

但我不确定这是最好的方法。此外,每一秒,地图上定位的每个怪物都必须移动1个位置。这是应该移动小怪的功能。

void MainWindow::moveMobs(){

int random[mobsQuantity]; // Different random position move for each monster.

for(int a = 0; a < mobsQuantity; a++){
    random[a] = qrand() % 5; // Initialize the moves
}

for(int a = 0; a < mobsQuantity; a++){
    int x = mobs[a].x(); 
    int y = mobs[a].y(); // Take the position for each different mob

    switch(random[a]){
    case NORTH:
        // Collision detection and movement
    break;
    case SOUTH:
        // Collision detection and movement
    break;
    case WEST:
        // Collision detection and movement
    break;
    case EAST:
        // Collision detection and movement
    break;
        }
    }
}

有没有更好的方法呢?

我的第二个问题是将Monster类传递给函数。 Monster源自QGraphicsItem。我在主窗口中有一个功能,为每个暴徒设置正确的位置,然后将其添加到QGraphicsScene。我的代码如下所示:

void MainWindow::createMobs(){
for(int a = 0; a < mobsQuantity; a++){
    mobs[a].setPos(/* X pos */ , /* Y pos */);

    scene->addItem(mobs[a]);
}

}

错误来自将项添加到场景中。我应该如何正确地传递一个dinamically分配的数组元素作为指针?

 error: no matching function for call to 'QGraphicsScene::addItem(Monster&)'

我尝试像这样添加它:

   scene->addItem(&mobs[a]);

但是我的程序崩溃了。

感谢所有愿意帮助我的人。

2 个答案:

答案 0 :(得分:1)

@Austin是正确的,使用向量或列表比使用普通数组要好得多,但如果你使用Qt,我会使用QVector而不是std :: vector。两者都有效,但这是我的偏好。

此外,C ++中的游戏通常使用在更新循环中更新自身的对象体系结构。所以你有一个主游戏循环只调用怪物的更新功能。像这个骨架代码: -

class Monster : public QGraphicsObject // note QGraphicsObject instead of Item so we have signals and slots too!
{
    void Update()
    {
         // move the monster according to rules
         // check for colliding objects
    }
};

然后在你的主游戏循环中

QList<QMonster*> pMonsterList = GetMonsters(); // from CLevel shown by @Austin
QMonster* pMonster;
foreach(pMonster, monsterList)
{
    pMonster->Update();
}

现在,如果你创建一个基类,让我们称之为GameObject,它是从QGraphicsObject派生的,然后你可以让所有可移动的对象以相同的架构运行。

class GameObject : public QGraphicsObject
{
    virtual void Update() = 0; // move object
};

class Monster : public GameObject
{
    void Update();
};

所以你现在只需更新这个级别的所有对象: -

CLevel::Update()
{
    QList<GameObject*> gameObjectList = GetGameObjects();
    GameObject* pGameObj;
    foreach(pGameObj, gameObjectList)
    {
        pGameObj->Update();
    }
}

答案 1 :(得分:0)

似乎使用std::vector<Monster*>qvector<Monster*>是最好的选择。例如:

 //header example for CLevel
 class CLevel:
      CLevel();
      std::vector<Monster*> *GetMonsters() {return &mMonsterVector;}
      void CreateMonsters(int NumberOfMonsters);

 private:
      std::vector<Monster*> mMonsterVector;

 };

 //cpp file
 void CLevel::CreateMonsters(int NumberOfMonsters){
       //make this a method in the level class

      for(int i = 0; i < NumberOfMonsters; i++){
           Monster *newMob = new Monster;
           //Fill in the monster properties
           newMob->FillProperties();
           //end monster properties
           mMonsterVector.push_back(newMob);
      }
 }

对于第二个问题,很难在没有看到所有代码的情况下进行诊断,但是如果代码在传递指针(&amp; array [a]是)之后崩溃,则指针不存在或者没有'已初始化或已取消初始化。也许数组是在调用函数后已经解除分配的函数中分配的,因为编译器已将其清理干净。