游戏循环: 1. 初始化 2. 进入游戏循环 3. 查询用户输入状态 4. 执行游戏逻辑和AI判断 5. 绘制图像 6. 循环 7. 退出 CODE: // defines for game loop states #define GAME_INIT 1 // the game is initializing #define GAME_MENU 2 // the game is in the menu #define GAME_STARTING 3 // the game is about to run #define GAME_RUN 4 // the game is now running #define GAME_RESTART 5 // the game is going to restart #define GAME_EXIT 6 // the game is exiting // game globals int game_state = GAME_INIT; // start off in this state Int error = 0; // used to send errors back to OS // main begins here Void main( ) { // implementation of main game loop While (game_state != GAME_EXIT) { // implementation of main game loop switch(game_state) { case GAME_INIT: // the game is initializing { // allocate all memory and resources Init( ); game_state = GAME_MENU; } break; case GAME_MENU: // the game is in the menu { // call the main menu function and let it switch states game_state = Menu( ); // note: we could force a RUN state here } break; case GAME_STARTING: // the game is about to run { // this state is optional, but usually used to set things up right // before the game is run you might do a little more housekeeping Setup_For_Run( ); // switch to run state game_state = GAME_RUN; } break; case GAME_RUN: // the game is now running { // this section contains the entire game logic loop Clear( ); // clear the display Get_Input( ); // get the input Do_Logic( ); // perform logic and AI Render_Frame( ); // display the next frame of animation Wait( ); // synchronize the display // the only way that state can be changed is thru user interaction // in the input section or by maybe losing the game. } break; case GAME_RESTART: // the game is restarting { // this section is a cleanup state used to fix up any loose ends // before running again Fixup( ); // switch states back to the menu game_state = GAME_MENU; } break; case GAME_EXIT: // the game is exiting { // if the game is in this state then it’s time to bail, kill everything // and cross your fingers Release_And_Cleanup( ); error =0; // set the error word to whatever // note: we do not have to switch states since we are already in this state // on the next loop iteration the code will fall out of the main while and // exit back to the OS } break; default: break; } // end switch return(error); // return error code to operating system } // end main