| XNA Game-Themed CS1 Examples ( XGC1 ) | |
|
Release 2.0 (XNA V3.1)
|
|
Goals:
1. Obtain the example code
Once we compile and run this project, the program display 'Thumbstick: {X:0 Y:0}" at the top of the screen.
If you move the left thumbstick towards
the right, you'll see the X value change in response. If you move the left
thumbstick towards the left, you'll see the X value change to a negative value.
Similarly with moving the left thumbstick forwards & backwards (sometimes
described as "up and down"), you'll see the Y values change appropriately.
The default keyboard to controller mapping assigned the
J
key to mean
"push the thumbstick to the left", the
K
key to mean "push the thumbstick
to the right", the
I
key to mean "push the thumbstick up", and the
M
key to mean "push the thumbstick to the down".
2. Background :
I/O in the context of an interactive, graphical application (such as a video game) commonly consists of determining the current status of the various parts of the video game controller. Is the 'A' button being pushed? Are the thumbsticks being held at an angle? And if so, how far right/left or forwards/backwards are they being held?. In this tutorial, we will examine how our code can detect input from the left thumbstick.
For each of the thumbsticks, our C# code can determine if the thumbstick is being held at an angle, and if so, by how much. As you will see in the example program, the current state of the thumbstick is expressed in C# as an (X,Y) pair, where the X value is how far left or right the thumbstick is being pushed, and the Y value is how far forwards or backwards it's being held. If the thumbstick is not being pushed, then both the X and Y values should be 0. These values can each range from -1 to +1. Using an actual XBox controller, you'll see that these values are continuous , meaning that the X and Y values can be ANY number between -1 and +1. If you're using the keyboard, you'll notice that you can only get -.75, 0, or +.75, because keyboard keys can only detect whether a key is being pushed down, not how hard the key is being pushed.
Before we go into the details of how to work with thumbstick input, it's worth taking a moment to consider what sorts of input a video game can and can't easily collected. Basically, anything on an XBox360 controller can be used as input. There are a number of 'buttons' on the XBox controller - each of the A, B, X and Y buttons, the left and right bumpers, the Start and Back buttons, and the thumbsticks (which can be being pushed down, like a button) are buttons. All of these buttons are either being pushed, or they're not - in this sense, they are discrete (meaning that have only one of a limited number of values). As discussed above, the thumbsticks can be pushed forwards, backwards, or side to side, in which case they give continuous input (potentially any number between -1 and +1). Keep in mind that for a normal video game, the player will NOT have a keyboard connected to their XBox360. It's actually quick and painless to plug a USB keyboard into your XBox360, but since most people don't play games with such a keyboard already connected, we should avoid depending on the presence of a keyboard.
Finally, a quick note on this tutorial: the goal is to explore the principles of I/O for an interactive, graphical program (like a video-game), and not to examine all possible details of all possible inputs. So for this tutorial, we will examine how to get input from only the left thumbstick. We will leave the details of getting input from buttons, the right thumbstick, etc, etc, till later.
3. Examining The Program:
Let's examine
the C# source code that produces the behavior we see on-screen
|
protected override void InitializeWorld()
{
World.SetWorldCoordinate(
|
We still have the 'exit when the user pushes the Back button' logic here, so that the player can actually quit the game when it's played on an XBox360. This is also where we read the current state of the left thumbstick, and then display a message at the top of the screen telling the user about the thumbstick.
There are a number of new things here, for right now, we'll start by looking at the first couple of new lines, and ignore everything else in the method:
|
protected
override
void
UpdateWorld()
// Version #1: EchoToTopStatus( "ThumbStick: " + GamePad.ThumbSticks.Left);} |
- Notice that the text "ThumbStick: " contains a blank space, right after the colon character, and before the closing double-quote.
Joining text with other text is called concatenation , so in this case, the + symbol represents the concatenation operator.
UpdateWorld(), Version #2
|
protected
override
void
UpdateWorld()
// Version #1: Basic input, basic output EchoToTopStatus( "ThumbStick: " + GamePad.ThumbSticks.Left);
// Version #2:
//EchoToTopStatus("ThumbStick: X is " + GamePad.ThumbSticks.Left.X);
|
|
protected
override
void
UpdateWorld()
{ if (GamePad.ButtonBackClicked()) this .Exit();
// Version #1: Basic input, basic output // EchoToTopStatus("ThumbStick: " + GamePad.ThumbSticks.Left); // Version #2: EchoToTopStatus( "ThumbStick: X is " + GamePad.ThumbSticks.Left.X);
|
NOTE:
that since the "
//
Version #2:
" line is intended to be a comment, we do NOT remove the //
from the start of it.
UpdateWorld(), Version #3
FURTHER EXERCISES::