Keyboard State Keyboard State new State new State
得到按鍵的狀態 () // 宣告一個Keyboard. State 結構的變數 Keyboard. State new. State; //得到目前鍵盤全部按鍵的狀況 new. State = Keyboard. Get. State(); //判斷Esc鍵是否已經被按下 if (new. State. Is. Key. Down(Keys. Escape)) this. Exit(); 4
得到按鍵的狀態 () // 宣告一個Keyboard. State 結構的變數 Keyboard. State new. State; //得到目前鍵盤全部按鍵的狀況 new. State = Keyboard. Get. State(); //判斷Esc鍵是否已經被按下 if (new. Stat[Keys. Escape] == Key. State. Down) this. Exit(); 5
得到按鍵的狀態 // 宣告一個Keyboard. State 結構的變數 Keyboard. State new. State; Keys[] all. Pkeys = new. State. Get. Pressed. Keys(); foreach (Keys k in all. Pkeys) { if (k == Keys. ? ? ? ). . . } 6
範例一:可以四處移動的球 宣告模型物件 n public class Game 1 : Microsoft. Xna. Framework. Game n { n Graphics. Device. Manager graphics; n Sprite. Batch sprite. Batch; n n n Game. Component_Grid; Model my. Model; // 宣告一個 模型物件 int X=0, Z = 0; 8
範例一:可以四處移動的球 n n n Load 模型 protected override void Load. Content() { // Create a new Sprite. Batch, which can be used to draw textures. sprite. Batch = new Sprite. Batch(Graphics. Device); // TODO: use this. Content to load your game content here my. Model = Content. Load<Model>("ball"); // 加入模型物件 n n n } 9
範例一:可以四處移動的球 設定網格呈現效果 n protected override void Draw(Game. Time game. Time) n { …… n // TODO: Add your drawing code here n foreach (Model. Mesh mesh in my. Model. Meshes) n { // 設定網格的呈現效果 (世界、觀測、投影矩陣) n foreach (Basic. Effect effect in mesh. Effects) n { effect. Enable. Default. Lighting(); n effect. World = Matrix. Create. Translation(X, 0, Z); effect. View = Matrix. Create. Look. At(new Vector 3(0. 0 f, 20. 0 f), n Vector 3. Zero, n Vector 3. Up); n effect. Projection = ……. . } n base. Draw(game. Time); n } 10
範例一:可以四處移動的球 和Key. Board互動,改變變數值。 n protected override void Update(Game. Time game. Time) n { // Allows the game to exit n if (Game. Pad. Get. State(Player. Index. One). Buttons. Back == Button. State. Pressed) this. Exit(); n // TODO: Add your update logic here n Keyboard. State new. State; // 宣告一個Keyboard. State 結構的變數 n new. State = Keyboard. Get. State(); //得到目前鍵盤每一個按鍵的狀況 n if (new. State. Is. Key. Down(Keys. Right)) X++; //判斷Right鍵是否已經被按下 n if (new. State. Is. Key. Down(Keys. Left)) X--; //判斷Left鍵是否已經被按下 n if (new. State. Is. Key. Down(Keys. Up)) Z--; //判斷Up鍵是否已經被按下 n if (new. State. Is. Key. Down(Keys. Down)) Z++; //判斷Down鍵是否已經被按 下 n base. Update(game. Time); n } 11
範例二:避免連續的鍵盤輸入 Keyboard. State old. State; // 宣告一個Keyboard. State 結構的變數 old. State = Keyboard. Get. State(); //得到目前鍵盤全部按鍵的狀況 …. Keyboard. State new. State; // 宣告一個Keyboard. State 結構的變數 new. State = Keyboard. Get. State(); //得到目前鍵盤每一個按鍵的狀況 //判斷Right鍵是否已經被按下 而且上一次是沒有被按下的 if (old. State. Is. Key. Up(Keys. Right) && new. State. Is. Key. Down(Keys. Right)) X++; 12
範例三:製作鍵盤輸入的類別 將鍵盤輸入用類別包裝,方便使用。 加入一個類別 n using Microsoft. Xna. Framework. Input; n namespace Windows. Game 1 n { class Keyboard. Use n { Keyboard. State new. State; // 宣告一個Keyboard. State結構的變數 n Keyboard. State old. State; // 宣告一個Keyboard. State結構的變數 n public void Update() n { new. State = Keyboard. Get. State(); // 得到目前鍵盤每一個按鍵的狀況 n old. State = new. State; } n …… n } 13
範例三:製作鍵盤輸入的類別 將鍵盤輸入用類別包裝,方便使用。 加入一個類別 n …… n // 當Key按下時 n public bool Is. Key. Down(Keys key) n { return new. State. Is. Key. Down(key); } n // 當Key無按下時 n public bool Is. Key. Up(Keys key) n { return new. State. Is. Key. Up(key); } n // 當Key按一次時(按下. 放開) n public bool Is. Key. Hit(Keys key) n { new. State = Keyboard. Get. State(); // 得到目前鍵盤每一個按鍵的狀況 n return (old. State. Is. Key. Up(key) && new. State. Is. Key. Down(key)); n } 14
範例三:製作鍵盤輸入的類別 將鍵盤輸入用類別包裝,方便使用。 宣告全域變數 kbuse 使用類別定義之函數 n // Keyboard. Use類別使用 n if (kbuse. Is. Key. Down(Keys. Left. Shift) && buse. Is. Key. Down(Keys. Up)) Z--; n if (kbuse. Is. Key. Down(Keys. Left. Shift) && kbuse. Is. Key. Down(Keys. Down)) Z++; n if (kbuse. Is. Key. Down(Keys. Left. Shift) && kbuse. Is. Key. Down(Keys. Left)) X--; n if (kbuse. Is. Key. Down(Keys. Left. Shift) && kbuse. Is. Key. Down(Keys. Right)) X++; n if (kbuse. Is. Key. Hit(Keys. Up)) Z--; if (kbuse. Is. Key. Hit(Keys. Down)) Z++; if (kbuse. Is. Key. Hit(Keys. Left)) X--; if (kbuse. Is. Key. Hit(Keys. Right)) X++; n if (kbuse. Is. Key. Down(Keys. R)) X = Z = 0; // 還原座標 n n n 15
Mouse. State結構 public struct Mouse. State { public Button. State Left. Button { get; } // 得到滑鼠的左鍵狀態 public Button. State Middle. Button { get; } // 得到滑鼠的中鍵狀態 public Button. State Right. Button { get; } // 得到滑鼠的右鍵狀態 public int Scroll. Wheel. Value { get; } // 得到滑鼠的滾輪的累積值 public int X { get; } // 得到滑鼠游標的X 軸位置 public int Y { get; } // 得到滑鼠游標的Y 軸位置 } 17
範例四:滑鼠輸入 Mouse. State old. State; // 宣告一個 Mouse. State 結構的變數 old. State = Mouse. Get. State(); //得到目前滑鼠的狀態 …. Mouse. State new. State = Mouse. Get. State(); //得到目前滑鼠的狀態 if (old. State. Right. Button == Button. State. Released && //滑鼠的右鍵上次是沒被按下 new. State. Right. Button == Button. State. Pressed) X++; //滑鼠的右鍵被按下 if (old. State. Left. Button == Button. State. Released &&//滑鼠的左鍵上次是沒被按下 new. State. Left. Button == Button. State. Pressed) X--; //滑鼠的左鍵被按下 // //滑鼠的滾輪被往前推 if (new. State. Scroll. Wheel. Value > old. State. Scroll. Wheel. Value) Z--; // //滑鼠的滾輪被往後推 if (new. State. Scroll. Wheel. Value < old. State. Scroll. Wheel. Value) Z++; old. State = new. State; // 這一次就是 下一次的上一次滑鼠狀態 18
The End 22
- Slides: 22