游戏编程结构

jma*_*erx 2 c++

我现在用c ++编程了一下,我非常熟悉语法.我正在尝试使用Allegro制作纸牌游戏.我理解我需要为游戏逻辑做的一切,而不是.令我困惑的是如何驾驶游戏.我是基于循环的应用程序的新手.我习惯了VB .Net中基于事件的编程.我只是不确定正确的方式,例如转换球员和提高"事件"而没有很多ifs和bools.现在我还有一系列bool来检查哪张牌在玩.而且我的游戏每次遍历整个bool阵列,对我来说似乎很麻烦.另外,如果我想从我的菜单循环转到我的设置循环,那么如果没有大的bool怎么办呢?谢谢

Mar*_*rth 5

大多数游戏框架提供了两种您需要实现的方法(它们都在循环中调用):

  • 更新

Update是你应该把所有东西放在哪里,它应该检查用户输入,状态变化,间隔动作等.例子是Physics,ButtonPressed等.没有什么能阻止你在这里处理事件(看看BoostLibrary信号).

void Game::update() {
   if(pushedButton) {
       this->cardsOnTable->add(this->hand->remove(0));
       this->activePlayer = nextPlayer();
   }
}
Run Code Online (Sandbox Code Playgroud)

Draw应该只呈现当前,潜在的状态到屏幕上.因此,您必须确保您的基础状态/模型易于访问.

void Game::render() {
  this->table->render();
  this->cardsOnTable->render();
  this->hand->render();
  // ...
  flipBuffers();
}
Run Code Online (Sandbox Code Playgroud)

您可以使用ScenesSceneManager(可以是堆栈)解决您的Menu/SettingsMenu问题.因此,不是Game直接将逻辑放入,而是将其放入Scenes.您可以向/从管理器推送/弹出场景.

void Game::update() {
  this->sceneManager->activeScene->update();
}

void MenuScene::update() {
  if(settingsMenuItemSelected) {
    game->sceneManager->push(new SettingsMenuScene));
    // now the next time Game::update() is called
    // the active scene of the SceneManager will be
    // the settings menu. And once it is closed/cancelled
    // it will pop itself from the manager and we are back
    // in the regular menu
  }
}
Run Code Online (Sandbox Code Playgroud)

如果你想从更高级的东西开始,你可以尝试将"事件"存储到一个巨大的列表中并在你输入Game::update方法时触发所有事件- 这就是VB确保你不能修改来自另一个线程的控件而不是UI线程 - 但我不认为这是你用C++做的事情.