Game programming structure

I've done a bit of programming in C ++ and I'm very familiar with the syntax. I am trying to use Allegro to create a card game. I understand everything that is needed for the logic of the game and what is not. It puzzles me how to operate the game. I'm kind of new to loop based applications. I am used to event based programming in VB.Net. I'm just not sure about the right way to, for example, switch players and raise "events" without a lot of ifs and bools. Also right now I have a bool array to check which card is in play. And my game is iterating through the whole bool array every time, and it seems like a mess to me. Also, if I want to go from the menu loop to my settings loop, how is it done without the big bool? Thanks to

+2


source to share


4 answers


Most game frameworks provide two methods that you need to implement (they both call in a loop):

  • Update
  • Draw

Update

This is where you should put all these things that should check for user input, state changes, action interval, etc. Examples would be physics, ButtonPressed, etc. Nothing prevents you from working with events here (have a look at BoostLibrary Signals ).

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

      

Draw

should just display the current baseline on the screen. So you have to make sure your baseline / model is easy to access.



void Game::render() {
  this->table->render();
  this->cardsOnTable->render();
  this->hand->render();
  // ...
  flipBuffers();
}

      

You can solve your Menu / SettingsMenu problem with Scenes

and SceneManager

(which could be a stack). So instead of directly injecting logic into Game

, you put it into Scenes

. And you can push / pop scenes to / from Manager.

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
  }
}

      

If you want to start with more complex stuff, you can try to store the "events" in a huge list and fire all the events when the method Game::update

is entered - this is how VB makes sure you can't modify controls from a thread other than the UI thread, but I can't think this is what you would do with C ++.

+5


source


In general, you may want to look into a book on programming idioms and design patterns, which are already discussed elsewhere here. Even if they are not directly related to games, they will help you.

Game-Engines are a good example of state machines (in-game state, state in settings and sub-states) - you can do all this with conditional forking, etc., but don't lose your head if it gets more distracted. There are various approaches to implementing state machines in C ++, based on polymorphic classes (refer to your favorite C ++ templates book), based on templates ( boost.statechart ), ... The common thing is that when using a central loop you forward events to some kind of handler (representing states or substations), which will do what is appropriate in the current situation. Need to change behavior (for example, from play to options)? Turn off handlers.



You will also want to wrap and distract the current state of the game - no need for explicit handling, for example. cards anywhere. Wrap things for example. a card or manager-player that processes it internally.
Also use containers and algorithms that C ++ gives you for free, you can manipulate cards or players in std::map

or std::set

s for example . When you fall within the standard library checkout,

+2


source


These are different questions. My first suggestions are to search online for source code for similar games. If you need help with the code, please submit it along with the question.

0


source


You will most likely need a bunch of ifs and loops that look something like this:

while(1){
  initYourLevel();
  while(levelNotOver){
    //do things
  }
}

      

Then it all depends on what you are trying to do and your question is a little vague to me. Start looking at tutorials to wrap yourself around how people do things. There are many code examples!

Hope it helps

0


source







All Articles