|XNA Game-Themed CS1 Examples (XGC1)|
Release 2.0 (XNA V3.1)
Need (library reference):
1. Obtain the example code
When the game starts, you'll see a screen that
looks similar to this:
As you can see, there is a single soccer ball on the screen. Using the right thumbstick, you can move the soccer ball around (if you want, you can even move it off the screen). The program prints out a simple, purely textual message at the top of the screen, and the current location of the soccer ball at the bottom of the screen. You may also notice that the size of the ball is continuously, randomly changing.
2. Variable Naming Convention
Since the source code to this program is nearly identical to the program uses in the previous tutorial, we will only examine the code that's new, or different.
Let's start by examining the naming convention for our variables.
Having settled upon this naming scheme, we will use it for this tutorial, and in future tutorials.
We need to declare our instance variables before we can use them.
public class Game1 : XNACS1Base
private XNACS1Circle m_TheSoccer; // a soccer ball
// Constants for circle/ball definition
private const float BALL_INIT_X = 50.0f;
private const float BALL_INIT_Y = 35.0f;
private const float BALL_INIT_RADIUS = 3.0f;
Throughout the rest of the program, we also need to change aBall to m_TheSoccer. While this needs to be done everywhere, this tutorial won't examine any changes that consist solely of changing the variable's name.
3. Accessing Instance Variables From Other Methods.
As has been mentioned previously, instance variables' lifespans starts when the Game1 object is created (which is when the game starts), and lasts until the Game1 object is destroyed (which happens just before the program exits). This means that we can, for example, move code from the InitializeWorld method into a new method, and still be able to access the same instance variables from that new method, like so:
protected override void
World.SetWorldCoordinate(new Vector2(0,0), 100.0f);
// initialize the soccer ball
/// This function initializes the soccer ball, /// location, dimension, and texture. /// /// Notice, this function is "inside" the Game1 class and /// have access to _all_ "instance variables" of this class. /// To avoid confusion of "types" of variables, from now on /// all "instance variables" will begin with "m_". /// </summary> private void InitializeSoccer()
// initialize the soccer ball
m_TheSoccer =new XNACS1Circle();
m_TheSoccer.Center =new Vector2(BALL_INIT_X, BALL_INIT_Y);
m_TheSoccer.Radius = BALL_INIT_RADIUS + RandomFloat();
: The Game-Themed Introductory Programming Project.
Computing and Software Systems
University of Washington, Bothell
Business And Information Technology Cascadia Community College email@example.com
|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.|