Upload
elaine-evans
View
232
Download
0
Tags:
Embed Size (px)
Citation preview
11
Getting Player Input Using a Gamepad
Session 3.1
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
Chapter 3.1: Getting Player Input Using a Gamepad 2
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
Chapter 3.1: Getting Player Input Using a Gamepad 3
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
Chapter 3.1: Getting Player Input Using a Gamepad 4
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
Chapter 3.1: Getting Player Input Using a Gamepad 5
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
Chapter 3.1: Getting Player Input Using a Gamepad 6
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
Chapter 3.1: Getting Player Input Using a Gamepad 7
GamePadState
Buttons
DPad
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
Chapter 3.1: Getting Player Input Using a Gamepad 8
GamePadState pad1;GamePadState pad1;
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
Chapter 3.1: Getting Player Input Using a Gamepad 9
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
Chapter 3.1: Getting Player Input Using a Gamepad 10
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
Chapter 3.1: Getting Player Input Using a Gamepad 11
GamePadState pad1 = GamePad.GetState (PlayerIndex.One) ;GamePadState pad1 = GamePad.GetState (PlayerIndex.One) ;
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
Chapter 3.1: Getting Player Input Using a Gamepad 12
GamePadState
Buttons
DPad
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
Chapter 3.1: Getting Player Input Using a Gamepad 13
if (pad1.Buttons.B == ButtonState.Pressed) redIntensity++;if (pad1.Buttons.B == ButtonState.Pressed) redIntensity++;
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.
Chapter 3.1: Getting Player Input Using a Gamepad 14
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++;
1. User-Controlled Red Light
Chapter 3.1: Getting Player Input Using a Gamepad 15
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
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
Chapter 3.1: Getting Player Input Using a Gamepad 16
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++;
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
Chapter 3.1: Getting Player Input Using a Gamepad 17
if (pad1.Buttons.Y == ButtonState.Pressed) { redIntensity++; greenIntensity++;}
if (pad1.Buttons.Y == ButtonState.Pressed) { redIntensity++; greenIntensity++;}
2. User-Controlled Light
Chapter 3.1: Getting Player Input Using a Gamepad 18
This version of Mood Light uses all the gamepad buttons to control the background color
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
Chapter 3.1: Getting Player Input Using a Gamepad 19
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
Chapter 3.1: Getting Player Input Using a Gamepad 20
if (pad1.DPad.Right == ButtonState.Pressed) redIntensity++;if (pad1.DPad.Right == ButtonState.Pressed) redIntensity++;
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
Chapter 3.1: Getting Player Input Using a Gamepad 21
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
Chapter 3.1: Getting Player Input Using a Gamepad 22
if (pad1.DPad.Right == ButtonState.Pressed || pad1.Buttons.B == ButtonState.Pressed){ redIntensity++;}
if (pad1.DPad.Right == ButtonState.Pressed || pad1.Buttons.B == ButtonState.Pressed){ redIntensity++;}
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?
Chapter 3.1: Getting Player Input Using a Gamepad 23
if (true || false ) redIntensity++;if (true || true ) redIntensity++;if (false|| false ) redIntensity++;
if (true || false ) redIntensity++;if (true || true ) redIntensity++;if (false|| false ) redIntensity++;
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
Chapter 3.1: Getting Player Input Using a Gamepad 24
if (true || false ) redIntensity++; // increasesif (true || true ) redIntensity++; // increasesif (false || false ) redIntensity++; // doesn’t increase
if (true || false ) redIntensity++; // increasesif (true || true ) redIntensity++; // increasesif (false || false ) redIntensity++; // doesn’t increase
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 &&
Chapter 3.1: Getting Player Input Using a Gamepad 25
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
Chapter 3.1: Getting Player Input Using a Gamepad 26
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;}
3. Fully Functional Light
Chapter 3.1: Getting Player Input Using a Gamepad 27
This version of the Mood Light will work on Zune, Windows PC or Xbox
It also provides the reset behavior with the shoulder buttons
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
Chapter 3.1: Getting Player Input Using a Gamepad 28
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
Chapter 3.1: Getting Player Input Using a Gamepad 29
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.
Chapter 3.1: Getting Player Input Using a Gamepad 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.
Chapter 3.1: Getting Player Input Using a Gamepad 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.
Chapter 3.1: Getting Player Input Using a Gamepad 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.
Chapter 3.1: Getting Player Input Using a Gamepad 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.
Chapter 3.1: Getting Player Input Using a Gamepad 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.
Chapter 3.1: Getting Player Input Using a Gamepad 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.
Chapter 3.1: Getting Player Input Using a Gamepad 36