1、游戏构成

      一个游戏基本上是一个连续的循环,它完成逻辑动作并以一定的刷新率(书上以30帧/秒为例)在屏幕上绘制图像。

      针对书上所作出了一个简化的游戏循环结构

捕获

第一步:初始化

      游戏程序执行标准初始化操作,如内存分配、资源采集、从磁盘载入数据等等。

第二步:进入游戏循环

      代码运行到游戏主循环体内部。

第三步:获得玩家的输入信息

      游戏玩家的输入信息被处理或缓存,以备下一步人工智能和游戏逻辑使用。

第四步:执行人工智能和游戏逻辑

      游戏代码的主体,诸如执行人工智能、物理系统和一般的游戏逻辑,其结果用于渲染下一帧图像。

第五步:渲染下一帧图像

      游戏的输入和第四步中游戏人工智能和游戏逻辑执行的结果,被用来产生游戏的下一帧动火。这个图像通常放在不可见的缓存区内,因此玩家不会看到它逐渐被渲染的过程。随后该图像被迅速拷贝到显示存储器中并显示出来。

第六步:同步显示

      通常由于游戏复杂程序不同,游戏在计算机上运行的速度会不一。因此必须把游戏按照某个最大帧速率进行同步,并使用定时功能或等待函数来维持同步。

第七步:循环

      返回到游戏的入口并重新执行上述全部步骤。

第八步:关闭

      表示将退出主程序或游戏循环,并回到操作系统。然而,在用户进行结束之前,用户必须释放所有的资源并清理系统。

 

      其实在大多数情况下,游戏循环是一个含有大量状态的FSM(有限状态自动机)下面贴出书上的源码。

   1: // defines for game loop states
   2: #define GAME_INIT       // the game is initializing
   3: #define GAME_MENU       // the game is in the menu mode
   4: #define GAME_STARTING   // the game is about to run
   5: #define GAME_RUN        // the game is now running
   6: #define GAME_RESTART    // the game is going to restart
   7: #define GAME_EXIT       // the game is exiting
   8:  
   9: // game globals
  10: int game_state = GAME_INIT; // start off in this state
  11: int error      = 0;     // used to send errors back to OS
  12:  
  13:  
  14: // main begins here
  15:  
  16: void main()
  17: {
  18: // implementation of main game loop
  19:  
  20: while (game_state!=GAME_EXIT)
  21:     {
  22:     // what state is game loop in
  23:         switch(game_state)
  24:     {
  25:         case GAME_INIT: // the game is initializing
  26:              {
  27:              // allocate all memory and resources
  28:             Init();
  29:  
  30:             // move to menu state
  31:             game_state = GAME_MENU;
  32:             }  break;
  33:  
  34:          case GAME_MENU:  // the game is in the menu mode
  35:               {
  36:               // call the main menu function and let it switch states
  37:               game_state = Menu();
  38:  
  39:               // note: we could force a RUN state here
  40:               }  break;
  41:  
  42:          case GAME_STARTING:   // the game is about to run
  43:               {
  44:               // this state is optional, but usually used to
  45:               // set things up right before the game is run
  46:               // you might do a little more housekeeping here
  47:               Setup_For_Run();
  48:  
  49:               // switch to run state
  50:               game_state = GAME_RUN;
  51:               }  break;
  52:  
  53:           case GAME_RUN:    // the game is now running
  54:                {
  55:                // this section contains the entire game logic loop
  56:                // clear the display
  57:                Clear();
  58:  
  59:                // get the input
  60:                Get_Input();
  61:  
  62:                // perform logic and ai
  63:                Do_Logic();
  64:                // display the next frame of animation
  65:                Render_Frame();
  66:  
  67:                // synchronize the display
  68:                Wait();
  69:  
  70:                // the only way that state can be changed is
  71:                // thru user interaction in the
  72:                // input section or by maybe losing the game.
  73:                }  break;
  74:  
  75:                case GAME_RESTART:  // the game is restarting
  76:                     {
  77:                     // this section is a cleanup state used to
  78:                     // fix up any loose ends before
  79:                     // running again
  80:                     Fixup();
  81:                     // switch states back to the menu
  82:                     game_state = GAME_MENU;
  83:                     }  break;
  84:  
  85:               case GAME_EXIT:   // the game is exiting
  86:                    {
  87:                    // if the game is in this state then
  88:                    // it's time to bail, kill everything
  89:                    // and cross your fingers
  90:                    Release_And_Cleanup();
  91:  
  92:                    // set the error word to whatever
  93:                    error = 0;
  94:  
  95:                    // note: we don't have to switch states
  96:                    // since we are already in this state
  97:                    // on the next loop iteration the code
  98:                    // will fall out of the main while and
  99:                    // exit back to the OS
 100:                    }  break;
 101:  
 102:         default: break;
 103:         }  // end switch
 104:  
 105:     } // end while
 106: // return error code to operating system
 107: return(error);
 108:  
 109: } // end main
 110:  

       上面的代码,基本上所有游戏的循环或多或少的都是这个结构设计的。下面最后给猪游戏循环逻辑的状态转换图。

11

posted @ 2011-06-23 21:13  青衫湮痕  阅读(1880)  评论(0编辑  收藏  举报