XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.3.ModulesAndFunctions
Example: Ex_3.AccessingInstanceVars

Functions and Instance Variables

Need (library reference):



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.

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.

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:


  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 refering the tutorial's code.
  2. Setting The Soccer Ball's Radius Using A Separate Function
    For this exercise, you should use the same project that was explained in the above tutorial.
    You'll notice that the code that is used to set the soccer ball's radius in the InitializeSoccer() function is the exact same line that we use to change the soccer ball's radius in the UpdateWorld method.  Create a new method, named something like SetRandomRadius(), which sets the soccer ball's radius in the above manner (i.e., the ball's radius will be set to a value as low as 3.0f, or as high as (but not including) 4.0f).  Then, call that function from both UpdateWorld, and InitializeSoccer.

Project home page: The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
Michael Panitz
Business And Information Technology
Cascadia Community College

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.