Liny_@NotePad

沉迷ACG中

使用XNA引擎做3D赛车小游戏..(4)

YOYO posted @ 2009年9月27日 22:35 in 【游戏开发】 with tags XNA , 2551 阅读

汽车可以向前进。。当然也应该有向左或向右的功能。。这即是本节的主题……

截图如下:

  1. 首先我们增加一个方法CheckInput,在Game1中,用于捕捉左右方向键的按下事件:
    1.         private void CheckInput()
    2.         {
    3.             KeyboardState newState = Keyboard.GetState();
    4.  
    5.             if (newState.IsKeyDown(Keys.Left))
    6.             {
    7.                 car.carLocation = 2.5f;
    8.             }
    9.             if (newState.IsKeyDown(Keys.Right))
    10.             {
    11.                 car.carLocation = -2.5f;
    12.             }
    13.         }
  2. 在Update方法中调用CheckInput方法:
    1. CheckInput();

运行看看,此时汽车已经能左右切换跑道了。
只不过,一下切换过来有点奇怪 - -……

因此,下面继续对其进行修改。。

 

  1. 为Car类增加以下属性:
    1. public float carSpeed = 10.0f;      // 车左右移动的速度
    2. public bool movingLeft = false;   // 车是否左移
    3. public bool movingRight = false;                // 车是否右移
  2. 在Car类中添加Update方法,用于更新移动标记与速度:
    1. public void Update(GameTime gameTime)
    2. {   
    3.         // gameTime.ElapsedGameTime为相邻两次调用Update方法的时间间隔
    4.         double elapsed = gameTime.ElapsedGameTime.TotalSeconds;   
    5.         if (movingLeft)                          // 车是否向左移动
    6.         {
    7.                 carLocation += (float)(carSpeed * elapsed);
    8.                 if (carLocation >= 2.5f)                                                                    // 移到2.5f,左移完成
    9.                 {
    10.                         movingLeft = false;                                                  // 修改左移标志为假,表示停止左移
    11.                         carLocation = 2.5f;
    12.                 }
    13.         }
    14.         if (movingRight)
    15.         {
    16.                 carLocation -= (float)(carSpeed * elapsed);                     // 同上
    17.                 if (carLocation <= -2.5f)
    18.                 {
    19.                         movingRight = false;
    20.                         carLocation = -2.5f;
    21.                 }
    22.         }
    23. }
  3. 修改Game1类中的CheckInput方法,使其支持左右上下方向键:
    1.         private void CheckInput()
    2.         {
    3.             KeyboardState newState = Keyboard.GetState();
    4.  
    5.             if (newState.IsKeyDown(Keys.Left))             // 是否方向左键被按下
    6.             {
    7.                 car.movingLeft = true;                  // 左移
    8.                 car.movingRight = false;
    9.             }
    10.             if (newState.IsKeyDown(Keys.Right))             // 是否方向右键被按下
    11.             {
    12.                 car.movingLeft = false;
    13.                 car.movingRight = true;                 // 右移
    14.             }
    15.  
    16.             if (newState.IsKeyDown(Keys.Down))      // 是否下箭头键被按下
    17.             {
    18.                 RoadSpeed--;                            // 减少汽车速度
    19.                 if (RoadSpeed <= 10)
    20.                     RoadSpeed = 10;                             // 允许汽车速度的最小值
    21.             }
    22.             if (newState.IsKeyDown(Keys.Up))            // 是否上箭头键被按下
    23.             {
    24.                 RoadSpeed++;                            // 增加汽车速度
    25.                 if (RoadSpeed >= 100)
    26.                     RoadSpeed = 100;                    // 允许汽车速度的最大值
    27.             }
    28.         }
  4. 在Game1类的Update方法中的CheckInput()后面调用car的Update方法:
    1. car.Update(gameTime);

 

于是汽车就可以调速 向左向右切换时也较为自然了 - -


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter