Chapter 03 game input

884 views
732 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
884
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Control the objects on the screen and give your application some user interaction. Mouse input is never available on the Xbox360,andThe Zune only supports its emulated controls—you won’t be plugginga mouse, a keyboard, or any other input devices into a Zune anytime soon.
  • Using the Microsoft.XNA.Framework.Input namespace
  • XButton1, XButton2:Returns the state of additional buttons on some mice.
  • Microsoft Permissive License (Ms-PL) This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software. 1. DefinitionsThe terms “reproduce,” “reproduction,” “derivative works,” and “distribution” have the same meaning here as under U.S. copyright law.A “contribution” is the original software, or any additions or changes to the software.A “contributor” is any person that distributes its contribution under this license. “Licensed patents” are a contributor’s patent claims that read directly on its contribution. 2. Grant of Rights(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software. 3. Conditions and Limitations (A) No Trademark License- This license does not grant you rights to use any contributors’ name, logo, or trademarks.(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.(E) The software is licensed “as-is.” You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
  • If you’re developing a game for Windows, you can still program for an Xbox 360 controller.You’ll have to have a wired controller, or you can purchase an Xbox 360 Wireless Receiver for around $20, which will allow you to connect up to four wireless controllers to a PC.
  • Ý tưởng đơn giản là xác định khung chữ nhật của 2 sprite và dùng phương thức Intersects để kiểm tra có đụng độ hay không
  • Chapter 03 game input

    1. 1. Game Input<br />Tran Minh Triet – Nguyen KhacHuy<br />Faculty of Information Technology<br />University of Science, VNU-HCM<br />
    2. 2. Game Input<br />XNA Framework supports three input sources<br />Xbox 360 controller<br />Wired controller under Windows<br />Wireless or wired for Xbox 360<br />Up to 4 at a time<br />Keyboard<br />Good default for Windows games<br />Xbox 360 also supports USB keyboards<br />Mouse<br />Windows only (no Xbox 360 support)<br />Poll for input<br />Every clock tick, check the state of your input devices<br />Generally works OK for 1/60th second ticks<br />
    3. 3. Digital vs Analog Controls<br />Input devices have two types of controls<br />Digital<br />Reports only two states: on or off<br />Keyboard: keys<br />Controller A, B, X, Y, Back, Start, D-Pad<br />Analog<br />Report a range of values<br />XBox 360 controller: -1.0f to 1.0f<br />Mouse: mouse cursor values (in pixels)<br />
    4. 4. Input Type Overview<br />
    5. 5. Keyboard Input<br />Handled via the Keyboard class<br />KeyBoard.GetState() retrieves KeyboardState<br />KeyboardState key methods<br />
    6. 6. Keyboard Input<br />Example: Checking the “A” key was pressed<br />Moving a rings<br />if (Keyboard.GetState().IsKeyDown(Keys.A))<br /> // BAM!!! A is pressed!<br />KeyboardState keyboardState = Keyboard.GetState();<br />if (keyboardState.IsKeyDown(Keys.Left))<br /> ringsPosition.X -= ringsSpeed;<br />if (keyboardState.IsKeyDown(Keys.Right))<br /> ringsPosition.X += ringsSpeed;<br />
    7. 7. Mouse Input<br />Providing a Mouse class to interact<br />Mouse.GetState() retrieves MouseState<br />MouseState important properties<br />
    8. 8. Mouse Input<br />
    9. 9. Mouse Input<br />Example: Moving the ring<br />On Update() method<br />MouseState prevMouseState;<br />MouseState mouseState = Mouse.GetState();<br />if(mouseState.X != prevMouseState.X ||<br /> mouseState.Y != prevMouseState.Y)<br /> ringsPosition = new Vector2(mouseState.X, mouseState.Y);<br />prevMouseState = mouseState;<br />
    10. 10. Gamepad Input<br />Other<br />Buttons<br />Buttons<br />X Y A B<br />Digital<br />DPad<br />Down Left Right Up<br />
    11. 11. Gamepad Input<br />Thumb Sticks<br />Left Right<br />Analogue, range -1..+1<br />
    12. 12. Gamepad Input<br />Buttons<br />LeftShoulder<br />RightShoulder<br />Digital<br />
    13. 13. Gamepad Input<br />Triggers<br />Left Right<br />Analogue, range 0..+1<br />
    14. 14. Gamepad Input - GamePad class<br />A static class<br />Do not need to create an instance to use<br />All methods are static<br />GetState<br />Retrieve current state of all inputs on one controller<br />SetVibration<br />Use to make controller vibrate<br />GetCapabilities<br />Determine which input types are supported. <br />Can check for voice support, and whether controller is connected.<br />
    15. 15. Gamepad Input - GamePadState Struct<br />Properties for reading state<br />Digital Input: Buttons, DPad<br />Analog Input: ThumbSticks, Triggers<br />Check connection state: IsConneced<br />PacketNumber<br />Number increases when gamepad state changes<br />Use to check if player has changed gamepad state since last tick<br />
    16. 16. Gamepad Input - GamePadState Struct<br />publicstruct GamePadState{public static bool operator !=(GamePadState left, GamePadState right);public static bool operator ==(GamePadState left, GamePadState right);<br />public GamePadButtons Buttons { get; }public GamePadDPad DPad { get; }public bool IsConnected { get; }public int PacketNumber { get; }public GamePadThumbSticks ThumbSticks { get; }public GamePadTriggers Triggers { get; }}<br />
    17. 17. Gamepad Input - GamePadButtons Struct<br />Properties for retrieving current button state<br />A, B, X, Y<br />Start, Back<br />LeftStick, RightStick<br />When you press straight down on each joystick, is a button press<br />LeftShoulder, RightShoulder<br />Possible values are given by ButtonState enumeration<br />Released – button is up<br />Pressed – button is down<br />
    18. 18. Gamepad Input - GamePadButtons Struct<br />Example<br />// create GamePadState struct <br />GamePadState m_pad; <br /> // retrieve current controller state <br />m_pad = GamePad.GetState(PlayerIndex.One); <br />if (m_pad.Buttons.A == ButtonState.Pressed) <br /> // do something if A button pressed| if (m_pad.Buttons.LeftStick == ButtonState.Pressed) <br /> // do something if left stick button pressed if (m_pad.Buttons.Start == ButtonState.Pressed) <br /> // do something if start button pressed<br />
    19. 19. Gamepad Input - GameDPad Struct<br />Properties for retrieving current DPad button state<br />Up, Down, Left, Right<br />Possible values are given by ButtonState enumeration<br />Released – button is up<br />Pressed – button is down<br />
    20. 20. Gamepad Input - GameDPad Struct<br />Example<br />// create GamePadState struct <br />GamePadState m_pad;<br />// retrieve current controller state m_pad = GamePad.GetState(PlayerIndex.One);<br />// do something if DPad up button pressed| if (m_pad.DPad.Up == ButtonState.Pressed) <br />// do something if DPad left button pressed if (m_pad.DPad.Left == ButtonState.Pressed) <br />
    21. 21. Gamepad Input – GamePadThumbsticks Struct<br />Each thumbstick has X, Y position<br />Ranges from -1.0f to 1.0f<br />Left (-1.0f), Right (1.0f), Up (1.0f), Down (-1.0f)<br />0.0f indicates not being pressed at all<br />Represented as a Vector2<br />So, have<br />Left.X, Left.Y, Right.X, Right.Y<br />
    22. 22. Gamepad Input – GamePadThumbsticks Struct<br />Example<br /> // create GamePadState struct<br /> GamePadState m_pad; <br /> // retrieve current controller statem_pad = GamePad.GetState(PlayerIndex.One); <br /> // do something if Left joystick pressed to right|if (m_pad.Thumbsticks.Left.X > 0.0f) <br /> // do something if Right joystick pressed down if (m_pad.Thumbsticks.Right.Y < 0.0f)<br />
    23. 23. Gamepad Input – GamePadTriggersStruct<br />Each trigger ranges from 0.0f to 1.0f <br />Not pressed: 0.0f<br />Fully pressed: 1.0f<br />Represented as a float<br />Have left and right triggers<br />Properties: Left, Right<br />// create GamePadState struct<br />GamePadState m_pad;<br />// retrieve current controller statem_pad = GamePad.GetState(PlayerIndex.One); <br />if (m_pad.Triggers.Left != 0.0f) <br /> // do something if Left trigger pressed down|if (m_pad.Triggers.Right >= 0.95f) <br /> // do something if Right trigger pressed all the way down <br />
    24. 24. Gamepad Input – GamePadTriggersStruct<br />Example<br />Each trigger ranges from 0.0f to 1.0f <br />Not pressed: 0.0f<br />Fully pressed: 1.0f<br />Represented as a float<br />Have left and right triggers<br />Properties: Left, Right<br />
    25. 25. Gamepad Input - Controller Vibration<br />Can set the vibration level of the gamepad<br />Call SetVibration() on GamePad class<br />Pass controller number, left vibration, right vibration<br />Left motor is low frequency<br />Right motor is high-frequency<br />Turn vibration full on, both motors<br />GamePad.SetVibration(PlayerIndex.One, 1.0f, 1.0f);<br />Turn vibration off, both motors<br />GamePad.SetVibration(PlayerIndex.One, 0f, 0f);<br />
    26. 26. Collision detection<br />protected bool Collide()<br />{<br /> Rectangle ringsRect = new Rectangle(<br /> (int)ringsPosition.X + ringsCollisionRectOffset,<br /> (int)ringsPosition.Y + ringsCollisionRectOffset,<br /> ringsFrameSize.X - (ringsCollisionRectOffset * 2),<br /> ringsFrameSize.Y - (ringsCollisionRectOffset * 2));<br /> Rectangle skullRect = new Rectangle(<br /> (int)skullPosition.X + skullCollisionRectOffset,<br /> (int)skullPosition.Y + skullCollisionRectOffset,<br /> skullFrameSize.X - (skullCollisionRectOffset * 2),<br /> skullFrameSize.Y - (skullCollisionRectOffset * 2));<br /> return ringsRect.Intersects(skullRect);<br />}<br />
    27. 27. Q&A<br />?<br />
    28. 28. References<br />XNA framework input<br />http:// msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.aspx<br />Controller Images<br />http:// creators.xna.com/en-us/contentpack/controller<br />Ebook<br />Learning XNA 3.0 – Aaron Reed<br />

    ×