Chapter 10 game loop design
In the previous chapters, we have mentioned the game loop many times. This chapter allows us to learn about the concept of the game loop and how to use the game loop.
We know that the subject of a game is usually in a loop. At first, we use a variable to indicate the status of the game, such
Gamestate =State_startmenu;
Each cycle determines the current status and calls different functions for processing. This method is simple and effective. All the logic code is in a class, so you don't have to consider hiding and dividing. You just need to define some functions. However, its advantage is also its disadvantage. This structured method usually makes the program appear large and messy. Therefore, we use another method to manage the game loop and introduce the concept of scene into the program.
We can think that a game is composed of several independent scenarios, such as the Start menu, the picture in the game, and the pass picture. The whole game is constantly switching between these scenarios, until you exit. The term scenario is very relevant here. All game elements, whether in the option menu or player role, are like actors on the stage ), they enter or leave the stage based on different scenarios. With the support of scenarios, actors are no longer crowded together, but divided into groups and isolated from each other. This will make the program clear and orderly, the game loop becomes a scenario loop.
First, we create a scenebase class as the base class for all scene:
Public Abstract ClassScenebase {
Abstract Public VoidTick ();
Abstract Public VoidUpdate (canvas C );
}
Here, tick is an animated frame for all actors to make actions. Update displays actors on the stage.
Then, we create a scenebase type variable in gameview to represent the current scenario. Only the current scenario will be drawn.
StaticScenebaseCurrentscene;
......
If(C! =Null){
Currentscene. Tick ();
Currentscene. Update (C );
}
Finally, a function is used to change the current scenario, which achieves the most basic scenario conversion.
Public Static VoidSetcurrentscene (scenebase scene ){
Currentscene= Scene;
}
To test this scheme, we create two scenario classes: scenestartmenu and scenemain, respectively, to indicate the Start menu and the main loop of the game, and switch between the two scenarios by clicking the screen.
First, we define these two scenarios in gameview.
StaticScenestartmenuScenestartmenu=Null;
StaticScenemainScenemain=Null;
Initialize scenestartmenu in the gameview constructor and use it as the initial scenario.
If(Scenestartmenu=Null)
Scenestartmenu=NewScenestartmenu ();
Setcurrentscene(Scenestartmenu);
Then we reload ontouchevent in gameview (we also need to add a response function for scenebase)
@ Override
Public BooleanOntouchevent (motionevent event ){
//TodoAuto-generated method stub
Return Currentscene. Ontouchevent (event );
}
In scenestartmenu
Public BooleanOntouchevent (motionevent event ){
//TodoAuto-generated method stub
Switch(Event. getaction ()){
CaseMotionevent.Action_down:
If(Gameview.Scenemain=Null)
Gameview.Scenemain=NewScenemain ();
Gameview.Setcurrentscene(Gameview.Scenemain);
Break;
}
Return True;
}
The opposite is scenemain.
Then we modify the update functions of two scene instances.
Public VoidUpdate (canvas c ){
//TodoAuto-generated method stub
C. drawargb (255, 0, 0, 0 );
C. drawtext ("scenestartmenu", gameview.Width/2, gameview.Height/2, paint );
}
In this way, you can know which scene is.
Now let's run this program and click on the screen to see the switching effect between the two scenarios. So far, the content of this chapter has been completed. It is very short, but it is very useful. With this structure, we can add the lifecycle control in the previous chapter, your program will become more neat and orderly. Of course, in a specific application, you must expand the content of scene, such as the interaction between scenes. You may need one or several setter or map structures (IMITATING activity ). Ensure that values are passed between scenarios, not instances. Instead, they are set to the actor's instructions rather than the actors themselves.
This chapter sample program http://u.115.com/file/f1cf493609