XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Tutorial 3: Working With PrimitiveSet

back to the main tutorial guide page .

Reference: This is the third tutorial on how to work with the XNACS1Lib library. It is assumed you have read and understand the previous two tutorials:

Goals : This tutorial concentrates on illustrating working with PrimitiveSet for controlling the visibility (membership with AutoDrawSet) of any related set of primitives, and on the Random number utilities:

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.
If you double click and run the executable program, you will observe:
This application is almost identical to that from the second tutorial, except two important points:
  1. Button-X: hides/un-hides the ladder and the pink-ish circle.
  2. Random speed of the soccer ball after each collision with the ladder.
In this tutorial, we will examine the details of implementing these functionality.

2. The Source Code Files/Structure

Take a look at the ExampleProgram folder that you have unzipped and you will see a structure identical to that of previous tutorials. Once again, you can double click on the *.sln file to launch the IDE, we will concentrate on the Game1.cs file where all the relevant source code are located.

3. Examine Game1.cs Structure:

When you open the Game1.cs file in the IDE, you will see source code structure that is very similar to that of the second tutorial:

? Reference to libraries ?

XNACS1Lib_PrimitiveSet {

    public class Game1 : XNACS1Base {

        #region Instance Variables

        protected override void   InitializeWorld()

        protected override void UpdateWorld()


Once again, we will examine each of : Instance Variables , InitializeWorld() , and UpdateWorld() in details and understand how to implement the behaviors we have observed.

4. Examine Instance Variables of Game1.cs

When you expand the Instance Variable region, you will observe:

#region Instance Variables
   ... SPEED ... and A B C D that are identical to previous tutorial ...

   XNACS1PrimitiveSet m_VisibleSet;    // E. Button-X click makes this set invisible
bool m_SetIsVisible;                 //    Records if the set is currently visible

The five sets of instance variables: A to E where variable sets A to D are identical to previous tutorial. E: ( m_VisibleSet ) is a logical set that lets us insert related Primitives and control the visibility of the Primitives as a set.

5. Examine InitializeWorld() function of Game1.cs

The InitializeWorld() function is once again very similar to previous tutorial:

protected override void   InitializeWorld() {
    SetWorldCoordinate( ... );

    // A. Create the initialize the rotating ladder
    // B. Create and initialize the bouncing soccer ball
    // C. Create and initialize the cicle controlled by the right thumb stick
    // D. Create and initialize the eraser Rectangle

    // E. Insert: RightThumbCircle, and Ladder into the m_VisibleSet
    m_VisibleSet = new XNACS1 PrimitiveSet ();
    m_SetISVisible = true ;

At label E , after allocating the memory for the m_VisibleSet , we inserted the RightThumbCircle and the RotatingLadder to be members of the m_VisibleSet , and record the fact that current the m_VisibleSet is visible by setting m_SetIsVisibl e to true.

6. Examine UpdateWorld() function of Game1.cs

Now in the UpdateWorld() function, we control the visibility of the m_VisibleSet and the speed of the soccer ball after collision:

protected override void UpdateWorld() {
  if (GamePad.ButtonBackClicked())
      this .Exit();

 #region  A. Update the Rotating ladder

#region B. ...

  #region C. ...

 #region D. ...

 # region E. toggle VisibleSet by the X-Button
     if (GamePad.ButtonXClicked()) {
           if (m_SetIsVisible)
          m_SetIsVisible = !m_SetIsVisible;


  # region F. Collide the soccer with the rotating ladder
     bool collided = m_SoccerBall.Collided(m_RotatingLadder);
    if (collided) {
        m_SoccerBall.VelocityDirection = m_SoccerBall.Center - m_RotatingLadder.Center;
        m_SoccerBall.Speed = SPEED * RandomFloat(0.5f, 2.0f) ;



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.