Can I have an overall about game main loop, it seems to be complicated :-/
Thanks

Recommended Answers

All 5 Replies

Can you provide an example of what you are looking for? Are you asking about wrapping up your game code in main in a while or do/while loop so you allow the user to repeat playing?

I'm asking for a loop that runs while the program are running beside another code
EX: in snake game, there are a function for putting food, and this function should work while the play is working
thanks :]

An approach, perhaps not the most effective and fanciest, would be to implement a "turn" based system. Make a class for your snake and make a "game" class that contains a snake object. One turn will pass once you've placed all the food, moved all the enemies one or more steps and finally move the snake based on what the user has entered. You'd instantiate the game class in main and use its method "playOneTurn()" or some such to do the 3 steps in the turn (repeat playOneTurn() until the game is over).

Let me know if you need a more concrete example. Others may have another idea or two.

Recently, I wrote a dice game that was set up with several loops.

To answer your question though. There is a loop in main() that I called the "application loop", much like jonsca suggested. This application loop was set up like this:

[B]//this is mostly pseudo code[/B]
int main() {
  define a "game" pointer
  begin "application" loop
    allocate a new "game" object
    set the game's configuration options
    begin "game play loop", continue as long as game::endOfGame() is false
      call game::play_a_round()
      report current game status at end of round
    end "game play loop"
    de-allocate the "game" object
  end "application" loop
  return 0;
}

game::play_a_round() {
  //"round" loop
  for player1 to playerX
    check player's "turn" status to determine score and if turn is done
    if needed, update current player score
  end round loop
}

//etc.....
//etc.....

I believe that this specifically is what you were asking about, but it is certainly possible to re-arrange things to put more of the loops/control within the "game" class itself.

Thank you all :]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.