XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.7.ObjectsAndClasses
Example: Ex_2.ConstructorWithParameters

OOP: Constructor with parameters ;
Null references



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:

The left thumbstick controls the pink/blue 'spot' on the screen (shown here near the bottom of the screen, beneath the soccerball).  Until the user presses 'A' button for the first time, the screen is blank except for the spot (and the top/bottom status messages).  When the player presses the 'A' button, the current soccer ball (if any) disappears from the screen (and from the program's internal logic), and a new one is created on the spot's current location. 

If there's a soccerball on the screen, then the right thumbstick can be used to roll it.  Note that when there's no soccerball on the screen, rolling the soccer ball does nothing.  While this seems like a minor, obvious point, it's important that the program not crash when the user attempts to roll the non-existent soccerball.  This is fairly easy to implement, but a very important detail to keep in mind.

2. SoccerBall.cs:

The code in this class is almost exactly the same as the prior tutorial.  The only two differences are that

  1. The constructor now has a parameter, that allows the code in the Game1 class to specify where to create the new SoccerBall, and
  2. Because the Game1 class now says where to create the soccerball, the INIT_X and INIT_Y constants have been moved into that class.

2. Game1.cs:

There are several changes that are being made here:

  1. The named constants INIT_X and INIT_Y are moved to this file from SoccerBall.cs.  We also add the SPOT_RADIUS constant to this file, so that we'll have a well-define radius for the on-screen spot.
  2. We'll add code to track where the spot is.  It'll start the program centered at (INIT_X, INIT_Y), and will be controlled by the left thumbstick.  Since you've seen this in previous tutorials, we won't go through any of the details here.
  3. The Game1 object's reference to the SoccerBall object will start out with the special value null, meaning that there is no SoccerBall (yet).  The program will not crash if the user attempts to roll the non-existent SoccerBall object.
  4. When the 'A' button is pushed, the old soccer ball is destroyed, and a new SoccerBall object is created whereever the spot currently is.  If there is already a SoccerBall object on the screen, it's discarded (and removed from the list of things that the XNACS1 library should draw)


  1. In the code listed below, remove the if statement (for example, comment it out), compile and run the program, and see what error message you get:

            if (null != m_TheBall)


  2. In the code listed below, remove the if statement (for example, comment it out), compile and run the program, and see what error message you get:

                if (null != m_TheBall)


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.