Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Getting Player Input Using a Gamepad Session 3.1.

Similar presentations


Presentation on theme: "11 Getting Player Input Using a Gamepad Session 3.1."— Presentation transcript:

1 11 Getting Player Input Using a Gamepad Session 3.1

2 Session Overview  Introduce the gamepad device  Show how a C# object can be used to represent the state of a gamepad  Find out how to use the properties of the GamePadState object in a program  Create a Mood Light that is controlled by the gamepad  Discover how to use logical operators OR and AND  Create our first XNA multiplayer game  Introduce the gamepad device  Show how a C# object can be used to represent the state of a gamepad  Find out how to use the properties of the GamePadState object in a program  Create a Mood Light that is controlled by the gamepad  Discover how to use logical operators OR and AND  Create our first XNA multiplayer game Chapter 3.1: Getting Player Input Using a Gamepad2

3 A User-Controlled Mood Light  We are going to make a Mood Light that is controlled by the user  They will press the buttons on the gamepad to control the brightness and color of the screen background  The user can select any color that they want  This can also serve as the basis of a game  To do this we need to know how XNA games get input from the player  We are going to make a Mood Light that is controlled by the user  They will press the buttons on the gamepad to control the brightness and color of the screen background  The user can select any color that they want  This can also serve as the basis of a game  To do this we need to know how XNA games get input from the player Chapter 3.1: Getting Player Input Using a Gamepad3

4 The Xbox Gamepad  The gamepad is a very complex device  It is connected to the host either by USB or a wireless connection  It can be used with a Windows PC or an Xbox  You can obtain a wireless adapter for the Windows PC  The gamepad is a very complex device  It is connected to the host either by USB or a wireless connection  It can be used with a Windows PC or an Xbox  You can obtain a wireless adapter for the Windows PC Chapter 3.1: Getting Player Input Using a Gamepad4

5 The Zune Buttons  The Zune buttons work in the same way as the buttons on a gamepad  The same XNA program can work with a gamepad or a Zune  However, the Zune doesn’t provide all the buttons that a gamepad does  The Zune buttons work in the same way as the buttons on a gamepad  The same XNA program can work with a gamepad or a Zune  However, the Zune doesn’t provide all the buttons that a gamepad does Chapter 3.1: Getting Player Input Using a Gamepad5

6 Software and Objects  We know that C# programs are made up of objects that bring together data and behaviors  A game has Game World data and draw and update methods  The state of a gamepad can be represented by a C# object called a GamePadState object  This contains the information about the state of the gamepad at a particular instant  The GamePadState object also provides properties you can use to read this status information  We know that C# programs are made up of objects that bring together data and behaviors  A game has Game World data and draw and update methods  The state of a gamepad can be represented by a C# object called a GamePadState object  This contains the information about the state of the gamepad at a particular instant  The GamePadState object also provides properties you can use to read this status information Chapter 3.1: Getting Player Input Using a Gamepad6

7 The GamePadState object  A GamePadState object contains information about all the input devices the gamepad has  We are going to consider just the buttons and the DPad at the moment  When the Red button (B) is pressed we want the redIntensity value of the background color to increase  A GamePadState object contains information about all the input devices the gamepad has  We are going to consider just the buttons and the DPad at the moment  When the Red button (B) is pressed we want the redIntensity value of the background color to increase Chapter 3.1: Getting Player Input Using a Gamepad7 GamePadState Buttons DPad

8 Creating a GamePadState Variable  The game will use a variable to hold the state of the gamepad  It will then test the values of the buttons in this variable so that the gamepad can be used to control the game  The variable must be declared like any other variable in the game program  It has been given the identifier pad1  The game will use a variable to hold the state of the gamepad  It will then test the values of the buttons in this variable so that the gamepad can be used to control the game  The variable must be declared like any other variable in the game program  It has been given the identifier pad1 Chapter 3.1: Getting Player Input Using a Gamepad8 GamePadState pad1;

9 The pad1 Variable  Each time that update is called, the pad1 variable is set with the state of the gamepad for player 1  Code in the update method will use this variable  Should the pad1 variable be a local variable or a member of the Game1 class?  Member variables are shared between all the methods in a class  Local variables are just used inside the body of a method and discarded when no longer required  Each time that update is called, the pad1 variable is set with the state of the gamepad for player 1  Code in the update method will use this variable  Should the pad1 variable be a local variable or a member of the Game1 class?  Member variables are shared between all the methods in a class  Local variables are just used inside the body of a method and discarded when no longer required Chapter 3.1: Getting Player Input Using a Gamepad9

10 Using Local Variables  The variable pad1 should be local to the update method  We don’t want to retain the value of gamepad states from previous calls of update  No other methods need to use the value of pad1  Deciding which variables should be local and which should be members is part of the program design process  The variable pad1 should be local to the update method  We don’t want to retain the value of gamepad states from previous calls of update  No other methods need to use the value of pad1  Deciding which variables should be local and which should be members is part of the program design process Chapter 3.1: Getting Player Input Using a Gamepad10

11 Setting the pad1 Variable  We can set the value of pad1 when we declare it  The XNA Framework provides a class called GamePad that exposes a getState method  The getState method is told which gamepad to get the state of  We don’t need to know how getState works, just how to call it and what the type of the result is  We can set the value of pad1 when we declare it  The XNA Framework provides a class called GamePad that exposes a getState method  The getState method is told which gamepad to get the state of  We don’t need to know how getState works, just how to call it and what the type of the result is Chapter 3.1: Getting Player Input Using a Gamepad11 GamePadState pad1 = GamePad.GetState (PlayerIndex.One) ;

12 GamePadState Properties  A property is member of a class that represents some data in the class  They can be set up to be read or written, or both  The GamePadState properties can only be read  Each of the properties can contain several values  A property is member of a class that represents some data in the class  They can be set up to be read or written, or both  The GamePadState properties can only be read  Each of the properties can contain several values Chapter 3.1: Getting Player Input Using a Gamepad12 GamePadState Buttons DPad

13 Game Update Behavior  This condition tests the state of the B (Red) button  If the button is Pressed the condition adds 1 to the value of redIntensity  If the button is held down successive calls of update will cause the redIntensity to increase  The equality operator is used because we are comparing the state of Buttons.B with the value ButtonState.Pressed  This condition tests the state of the B (Red) button  If the button is Pressed the condition adds 1 to the value of redIntensity  If the button is held down successive calls of update will cause the redIntensity to increase  The equality operator is used because we are comparing the state of Buttons.B with the value ButtonState.Pressed Chapter 3.1: Getting Player Input Using a Gamepad13 if (pad1.Buttons.B == ButtonState.Pressed) redIntensity++;

14 Game Update Behavior  This is the code that allows a game to control the red intensity using button B on a gamepad  Create a local variable called pad1 that contains the state of the gamepad for player 1  Get the state of button B out of the Buttons in the pad, and if the button is pressed, increase the value of redIntensity.  This is the code that allows a game to control the red intensity using button B on a gamepad  Create a local variable called pad1 that contains the state of the gamepad for player 1  Get the state of button B out of the Buttons in the pad, and if the button is pressed, increase the value of redIntensity. Chapter 3.1: Getting Player Input Using a Gamepad14 GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.B == ButtonState.Pressed) redIntensity++; GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.B == ButtonState.Pressed) redIntensity++;

15 1. User-Controlled Red Light Chapter 3.1: Getting Player Input Using a Gamepad15  This version of Mood Light uses the code above to control the redIntensity value  When the Red button is pressed the screen redIntensity value gets larger  This version of Mood Light uses the code above to control the redIntensity value  When the Red button is pressed the screen redIntensity value gets larger

16 Controlling Green and Blue  The same code construction can be used to control the green and blue intensity values  Note that we don’t need to get a new GamePadState value each time, the program can just read different properties from the same pad1 value  The same code construction can be used to control the green and blue intensity values  Note that we don’t need to get a new GamePadState value each time, the program can just read different properties from the same pad1 value Chapter 3.1: Getting Player Input Using a Gamepad16 GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.B == ButtonState.Pressed) redIntensity++; if (pad1.Buttons.A == ButtonState.Pressed) greenIntensity++; if (pad1.Buttons.X == ButtonState.Pressed) blueIntensity++; GamePadState pad1 = GamePad.GetState(PlayerIndex.One); if (pad1.Buttons.B == ButtonState.Pressed) redIntensity++; if (pad1.Buttons.A == ButtonState.Pressed) greenIntensity++; if (pad1.Buttons.X == ButtonState.Pressed) blueIntensity++;

17 Controlling Yellow Intensity Using the Y Button  The gamepad has a Yellow button (Y)  We can increase the yellow intensity by increasing both red and green when this button is pressed  To achieve this, the two update statements must be placed in a block after the condition  The gamepad has a Yellow button (Y)  We can increase the yellow intensity by increasing both red and green when this button is pressed  To achieve this, the two update statements must be placed in a block after the condition Chapter 3.1: Getting Player Input Using a Gamepad17 if (pad1.Buttons.Y == ButtonState.Pressed) { redIntensity++; greenIntensity++; } if (pad1.Buttons.Y == ButtonState.Pressed) { redIntensity++; greenIntensity++; }

18 2. User-Controlled Light Chapter 3.1: Getting Player Input Using a Gamepad18  This version of Mood Light uses all the gamepad buttons to control the background color

19 Adding Zune Support Using the DPad  The Zune does not have all of the buttons on the gamepad  However it does have a DPad which can be pressed in one of four directions  We could make a “Mood Flashlight” by using the DPad to control the colors  Then the program would work on the Zune as well  The Zune does not have all of the buttons on the gamepad  However it does have a DPad which can be pressed in one of four directions  We could make a “Mood Flashlight” by using the DPad to control the colors  Then the program would work on the Zune as well Chapter 3.1: Getting Player Input Using a Gamepad19

20 Controlling Red Using the DPad.Right Value  The GamePadState type contains a DPad property which gives the states of the Left, Right, Up, and Down positions of the DPad  These are used in exactly the same way as the colored buttons on the gamepad  If any button state is equal to the value ButtonState.Pressed it means that the direction is selected on the gamepad  The GamePadState type contains a DPad property which gives the states of the Left, Right, Up, and Down positions of the DPad  These are used in exactly the same way as the colored buttons on the gamepad  If any button state is equal to the value ButtonState.Pressed it means that the direction is selected on the gamepad Chapter 3.1: Getting Player Input Using a Gamepad20 if (pad1.DPad.Right == ButtonState.Pressed) redIntensity++;

21 Combining Tests Using Logical OR  We want to make the red light brighter if button B (Red) is pressed or Right is pressed on the DPad  We can do this by having two separate if statements  However, it would be neater to be able to use a single condition: “If Button B is pressed OR DPad Right is pressed increase the value of redIntensity”  C# provides a logical operator to allow this  We must use the logical OR operator  We want to make the red light brighter if button B (Red) is pressed or Right is pressed on the DPad  We can do this by having two separate if statements  However, it would be neater to be able to use a single condition: “If Button B is pressed OR DPad Right is pressed increase the value of redIntensity”  C# provides a logical operator to allow this  We must use the logical OR operator Chapter 3.1: Getting Player Input Using a Gamepad21

22 The Logical OR operator  The logical OR operator is two vertical bars - ||  It works between Boolean values (i.e. true or false)  If either of the values on each side are true, the result it gives is true  The above code increases redIntensity if either button is pressed  The logical OR operator is two vertical bars - ||  It works between Boolean values (i.e. true or false)  If either of the values on each side are true, the result it gives is true  The above code increases redIntensity if either button is pressed Chapter 3.1: Getting Player Input Using a Gamepad22 if (pad1.DPad.Right == ButtonState.Pressed || pad1.Buttons.B == ButtonState.Pressed) { redIntensity++; } if (pad1.DPad.Right == ButtonState.Pressed || pad1.Buttons.B == ButtonState.Pressed) { redIntensity++; }

23 Logical OR Test  Logical OR works between any two conditions that return a Boolean result  We can use it between the values true and false  Although this might result in compiler warnings  Which of the above statements would increase the value of redIntensity ?  Logical OR works between any two conditions that return a Boolean result  We can use it between the values true and false  Although this might result in compiler warnings  Which of the above statements would increase the value of redIntensity ? Chapter 3.1: Getting Player Input Using a Gamepad23 if (true || false ) redIntensity++; if (true || true ) redIntensity++; if (false|| false ) redIntensity++; if (true || false ) redIntensity++; if (true || true ) redIntensity++; if (false|| false ) redIntensity++;

24 Logical OR Test  The OR operator returns true if either of the conditions on each side of the operator is true  Therefore it only returns false if both the conditions are false  The OR operator returns true if either of the conditions on each side of the operator is true  Therefore it only returns false if both the conditions are false Chapter 3.1: Getting Player Input Using a Gamepad24 if (true || false ) redIntensity++; // increases if (true || true ) redIntensity++; // increases if (false || false ) redIntensity++; // doesn’t increase if (true || false ) redIntensity++; // increases if (true || true ) redIntensity++; // increases if (false || false ) redIntensity++; // doesn’t increase

25 Logical AND  There is also a logical AND operator  We could use this to make the program reset all the colors to 0 if both triggers are pressed on the gamepad  This makes it harder to reset the colors by mistake  The logical AND condition only works out to true if the conditions on both sides are true  The logical AND operator is the character sequence &&  There is also a logical AND operator  We could use this to make the program reset all the colors to 0 if both triggers are pressed on the gamepad  This makes it harder to reset the colors by mistake  The logical AND condition only works out to true if the conditions on both sides are true  The logical AND operator is the character sequence && Chapter 3.1: Getting Player Input Using a Gamepad25

26 Logical AND Value Reset  The program can test the values of the shoulder buttons on the controller  If both of them are pressed the block of code is performed  This resets the intensity values to 0  The program can test the values of the shoulder buttons on the controller  If both of them are pressed the block of code is performed  This resets the intensity values to 0 Chapter 3.1: Getting Player Input Using a Gamepad26 if (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed) { redIntensity = 0; greenIntensity = 0; blueIntensity = 0; } if (pad1.Buttons.LeftShoulder == ButtonState.Pressed && pad1.Buttons.RightShoulder == ButtonState.Pressed) { redIntensity = 0; greenIntensity = 0; blueIntensity = 0; }

27 3. Fully Functional Light Chapter 3.1: Getting Player Input Using a Gamepad27  This version of the Mood Light will work on Zune, Windows PC or Xbox  It also provides the reset behavior with the shoulder buttons  This version of the Mood Light will work on Zune, Windows PC or Xbox  It also provides the reset behavior with the shoulder buttons

28 Game Idea – Color Nerve  You can use this program as the basis of a game:  Players take turns to press any buttons they like on the gamepad (but they must change the color on the screen)  If the screen flashes from bright to dark (i.e. an intensity value wraps around) during a turn that player is out  The game continues until one player is left to become the winner  Press the shoulder buttons to reset for the next game  You can use this program as the basis of a game:  Players take turns to press any buttons they like on the gamepad (but they must change the color on the screen)  If the screen flashes from bright to dark (i.e. an intensity value wraps around) during a turn that player is out  The game continues until one player is left to become the winner  Press the shoulder buttons to reset for the next game Chapter 3.1: Getting Player Input Using a Gamepad28

29 Summary  The XNA Framework uses the type GamePadState to represent the state of a gamepad in a game  The GamePad class, which is part of XNA, provides a getState method that can be used to get the GamePadState value for the selected player  The GamePadState type exposes properties that can be read to get the state of the gamepad  These states can be combined using logical operators to allow more complicated behaviors to be created in a program  The XNA Framework uses the type GamePadState to represent the state of a gamepad in a game  The GamePad class, which is part of XNA, provides a getState method that can be used to get the GamePadState value for the selected player  The GamePadState type exposes properties that can be read to get the state of the gamepad  These states can be combined using logical operators to allow more complicated behaviors to be created in a program Chapter 3.1: Getting Player Input Using a Gamepad29

30 True/False Revision Quiz  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program.  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program. Chapter 3.1: Getting Player Input Using a Gamepad30

31 True/False Revision Quiz  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program.  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program. Chapter 3.1: Getting Player Input Using a Gamepad31

32 True/False Revision Quiz  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program.  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program. Chapter 3.1: Getting Player Input Using a Gamepad32

33 True/False Revision Quiz  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program.  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program. Chapter 3.1: Getting Player Input Using a Gamepad33

34 True/False Revision Quiz  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program.  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program. Chapter 3.1: Getting Player Input Using a Gamepad34

35 True/False Revision Quiz  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program.  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by || in a program. Chapter 3.1: Getting Player Input Using a Gamepad35

36 True/False Revision Quiz  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by|| in a program.  An XNA program can control the state of the buttons on a gamepad.  The GamePadState type holds information about the state of a gamepad.  Objects can expose information by means of properties.  An XNA program can only connect to one gamepad.  The Zune has a full set of colored buttons.  The AND operator is represented by|| in a program. Chapter 3.1: Getting Player Input Using a Gamepad36


Download ppt "11 Getting Player Input Using a Gamepad Session 3.1."

Similar presentations


Ads by Google