XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)
2/8/2010

Topic: Topic.2.Input_Output_PrimitiveDataTypes
Example: Ex_2.SimpleInputOutput

Simple Input and Output (I/O)


References:

Goals:




1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source 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

UpdateWorld(), Version #2

UpdateWorld(), Version #3


FURTHER EXERCISES::  

  1. Start from a blank starter project (1000.201, if you need it), and re-do the code from memory as much as possible.  On your first try, do what you can, and keep the above code open so that when you get stuck, you can quickly look up what you forgot (and that after you finish a line, so that you can compare your line to the 'correct' line).  On the next try, do the same thing, but try to use the finished code less.  Repeat this until you can type everything, without referring the tutorial's code.
  2. After you work the tutorial's "Version #2" material, try doing the following:
  3. If you uncomment Version #2, but you DO NOT COMMENT OUT Version #1, you still get the same message as the one you see when the follow the directions for Version #2.  Why?

Project home page : The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu
Michael Panitz
Business And Information Technology
Cascadia Community College
mpanitz@cascadia.eduu

Microsoft Logo This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.
2/8/2010