Need advice with game loop

Started by
7 comments, last by Servant of the Lord 9 years, 9 months ago

Hi...

I am newbie to game programming, so excuse me if I ask silly question.

I start to implement my first game. The game is like a turn-base board game.(implemented using Java for Android)

Each player have 14 soldiers. each soldier placed/located in default position.

Before starting game, there is an 'INIT' stage, where each player can change the default location of each of his soldiers.

After each player set their soldiers, the game can start.

My game loop(pseudo code)


while(true){
	processUserEvents();	// if its player's TURN, will update the event(else will drop it)
	draw();			// redraw			
}

My question is: should the stage of 'INIT' need to be inside game loop?

or it should be in a different place, and when 'INIT' finished, only then execute game loop ?

any byte of advice will help.

Advertisement

Init should be done before game loop.

Ok Thanks..

BTW, user input(such as touch event), are stored in Queue, instead of handling them immediately.

In every iteration of game loop i 'poll' event from the queue and handle it.


Before starting game, there is an 'INIT' stage, where each player can change the default location of each of his soldiers.

This is part of the game, and needs to be done in the game loop. Performing an initialize() before the game loop starts is a different thing.

Anything that happens while the game is running goes inside the game loop.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

Agreed, this is part of the game loop. It is a particular rule state, or phase, rather than a separate step.

Before starting game, there is an 'INIT' stage, where each player can change the default location of each of his soldiers.

Ooops, didn't read it thorough. Of course in this case this should be in game loop. SORRY for inconvenience.

In this instance I'd be inclined to do something like...


initialise_game();

bool replay = true;

while( replay )
{
    get_player_start_locations(); // this function would have a mini game loop while players set their starting locations
    
    bool running = true;

    while( running )
    {
        running = process_user_events(); // return false when player hits quit button etc.
 
        if( running == true )
        {
            draw();
        }
    }

    replay = ask_if_player_wants_another_game(); // again, a mini game loop in here while replay question is displayed
}

shut_down_game();
Maybe you can create a set of states, so depending on the game state something will happen when you're in the game loop. In your case you could have a state "menu" in which the player can either change the positions you mentioned or config something else. And another state coud be "game_running"

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

In this instance I'd be inclined to do something like...


initialise_game();

bool replay = true;

while( replay )
{
    get_player_start_locations(); // this function would have a mini game loop while players set their starting locations
    
    bool running = true;

    while( running )
    {
        running = process_user_events(); // return false when player hits quit button etc.
 
        if( running == true )
        {
            draw();
        }
    }

    replay = ask_if_player_wants_another_game(); // again, a mini game loop in here while replay question is displayed
}

shut_down_game();

If you expand the functions with the "mini game loops", you get:


while( gameRunning )
{
    while( gettingPlayerStartPositions )
    {
        //'Place starting positions' logic
        //'Place starting positions' drawing
    }

    while( playingGame )
    {
        //Playing logic
        //Playing drawing
    }

    while( askIfPlayerWantsToPlayAgain)
    {
        //Play Again? logic
        //Play Again? drawing
    }
}

Then you see it has unnecessary looping.

So it can be simplified like this:



enum GameState { PlaceStartingPositions, PlayerTurn, MainMenu, GameOver,
GameState gameState = MainMenu;

while( gameRunning )
{
    if( gameState == PlaceStartingPositions )
    {
        //'Place starting positions' logic
        //'Place starting positions' drawing
    }
    else if( gameState == PlayerTurn )
    {
        //Playing logic
        //Playing drawing
    }
    else if( gameState == EnemyTurn )
    {
        //Watching enemy moves
    }
    else if( gameState == GameOver)
    {
        //Play Again? logic
        //Play Again? drawing
    }
}

Just to be clear, I'm not advocating against functions - just against the use of infinite loops within other infinite loops to control the flow of gameplay.

(By 'infinite loop' I mean loops where the game can't break out of it without player action).

This topic is closed to new replies.

Advertisement