2007年11月2日 星期五

XNA Game Studio Express 控制模組的移動

http://richielin-programer.blogspot.com/2007/11/xna-game-studio-express.html

繼前幾篇文章 XNA Game Studio Express 初試 中示範 2D 遊戲畫面顯示
XNA Game Studio Express 顯示 3D 模組 中對示範 3D 遊戲畫面顯示
都還只是顯像輸出的範圍,遊戲中很重要的一項因素 - 互動
當然得包含輸出及輸入部份
本範例將介紹輸入的部份,還是以 XNA MSDN 上範例做示範
但由於該範例是以 XBOX360 控制器做為輸入界面,無法在 PC 上實際操控
所以天秤將稍微修改一下範例,以鍵盤當成輸入界面,使其在 PC 上可以動作

##CONTINUE##

準備工作

本範例是延續上一篇文篇的範例,直接跳至此篇的朋友
可以下載上一篇文章範例的程式碼來學習

讀取輸入裝置

前幾篇文章陸陸續續有提到,GraphicsDeviceManager 類別兩個重要的函式
draw 函式負責不斷的將畫面輸出至螢幕
而 update 函式即是負責不斷的計算、處理遊戲中的參數,如處理使用者的輸入
此範例即是在 update 函式加入判斷輸入的功能
其中有關 GamePad 物件為 XBOX360 特有的物件,用來取得 XBOX360 搖捍的輸入狀態
Keyboard 物件即為取得 PC 上鍵盤輸入的物件
而判斷某鍵是否有輸入,可以 Keyboard.GetState() 函式取得鍵盤狀態中該鍵是否有按下
再依是否按下狀態來做模組的移動

// Set the velocity of the model, applied each frame to the model's position.
Vector3 modelVelocity = Vector3.Zero;

protected override void Update(GameTime gameTime)
{
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();
    if (Keyboard.GetState().IsKeyDown(Keys.Escape) == true)
        this.Exit(); 

    // Get some input.
    UpdateInput(); 

    // Add velocity to the current position.
    modelPosition += modelVelocity; 

    // Bleed off velocity over time.
    modelVelocity *= 0.95f; 

    base.Update(gameTime);
}

protected void UpdateInput()
{
    // Get the game pad state.
    KeyboardState keyboard_currentState = Keyboard.GetState();
    if (keyboard_currentState != null)
    {
        // Rotate the model using the left thumbstick, and scale it down.
        if (keyboard_currentState.IsKeyDown(Keys.Left) == true)
            modelRotation += 1 * 0.10f;
        if (keyboard_currentState.IsKeyDown(Keys.Right) == true)
            modelRotation -= 1 * 0.10f; 

        // Create some velocity if the right trigger is down.
        Vector3 modelVelocityAdd = Vector3.Zero; 

        // Find out what direction we should be thrusting, using rotation.
        modelVelocityAdd.X = -(float)Math.Sin(modelRotation);
        modelVelocityAdd.Z = -(float)Math.Cos(modelRotation); 

        // Now scale our direction by how hard the trigger is down.
        if (keyboard_currentState.IsKeyDown(Keys.Up) == true)
            modelVelocityAdd *= 2;
        if (keyboard_currentState.IsKeyDown(Keys.Down) == true)
            modelVelocityAdd *= 0.5F; 

        // Finally, add this vector to our velocity.
        modelVelocity += modelVelocityAdd; 

        // In case you get lost, press A to warp back to the center.
        if (keyboard_currentState.IsKeyDown(Keys.Space) == true)
        {
            modelPosition = Vector3.Zero;
            modelVelocity = Vector3.Zero;
            modelRotation = 0.0f;
        }
    } 

    // Get the game pad state.
    GamePadState currentState = GamePad.GetState(PlayerIndex.One);
    if (currentState.IsConnected)
    {
        // Rotate the model using the left thumbstick, and scale it down.
        modelRotation -= currentState.ThumbSticks.Left.X * 0.10f; 

        // Create some velocity if the right trigger is down.
        Vector3 modelVelocityAdd = Vector3.Zero; 

        // Find out what direction we should be thrusting, using rotation.
        modelVelocityAdd.X = -(float)Math.Sin(modelRotation);
        modelVelocityAdd.Z = -(float)Math.Cos(modelRotation); 

        // Now scale our direction by how hard the trigger is down.
        modelVelocityAdd *= currentState.Triggers.Right; 

        // Finally, add this vector to our velocity.
        modelVelocity += modelVelocityAdd; 

        GamePad.SetVibration(PlayerIndex.One, currentState.Triggers.Right,
            currentState.Triggers.Right); 

        // In case you get lost, press A to warp back to the center.
        if (currentState.Buttons.A == ButtonState.Pressed)
        {
            modelPosition = Vector3.Zero;
            modelVelocity = Vector3.Zero;
            modelRotation = 0.0f;
        }
    }
}

本範例可以輸入鍵盤左、右鍵來控制飛行器的左右旋轉
上、下鍵來控制飛行器速度,如果飛行器飛出螢幕範圍了
按下 Space 鍵可以回復預設值,讓飛行器再次回到螢幕中央
最後按下 Escape 鍵結束

下載: 本範例程式碼及執行檔下載

0 意見: