Developing Programming Assignments on the XBOX 360 Console

Section III: The Block Breaker Game
a. The simple circle again

back to workshop main page



Building and Running: Here is the source code to a very simple XNA project based on the XnaAssignmentBase library. Download and unzip this file and you should see a SourceCode folder. With XNA Game Studio Express installed on your PC, you should be able to double click on the SimpleXNA.sln file to invoke the IDE.

You can now Build and run this program. When the application first starts, you will see:
The right thumbstick on the gamepad controller controls the relative position of the circle. Notice that, in this case, you can also use the arrow keys on your keyboard to control the circle. The XnaAssignmentBase maps all the controller functionality to the keyboard. Here is the mapping between keyboard and the controller.
 

Implementation:  comparing this project to the one we worked on previous, we notice a few differences. Each of the following changes are enclosed by regions with label: __CODE_ADDED__:
  1. Super class of Game1: is now XnaAssignmentBase. 
  2. Declare library usage:

    #region __CODE_ADDED__ using
        using XnaAssignments;       // Using the XnaAssignments library

    #endregion


  3. Constructor:

    #region __CODE_ADDED__ constructor
            public Game1() : base (new Vector2(0, 0), 15) { }

    #endregion


  4. Initialization: (in InitializeWorld() function):

    #region __CODE_ADDED__ InitializeWorld
        protected override void InitializeWorld() {
           pos = new Vector2(); // Memory allocation 
           pos.X = 5.0f;        // Initial X position
           pos.Y = 4.0f;        // Initial Y position
        }
    #endregion

     
  5. Poll the game pad thumbstick position (in UpdateWorld() function):

    #region __CODE_ADDED__ UpdateWorld
       protected
    override void UpdateWorld() {
          if (XnaAssignmentBase.GamePad.ButtonBackClicked())
                this.Exit();

          Vector2 p = XnaAssignmentBase.GamePad.ThumbSticks.Left;
          pos += p;;  // left thumbstick state
       }
    #endregion


  6. Draw the triangle based on the thumbstick position (in Draw() function):

    #region __CODE_ADDED__ DrawWorld
         protected
    override void DrawWorld() {
             XnaAssignmentBase.DrawCircle(
               pos,              /* center of circle */
              
    1.0f,             /* Radius of circle */
              
    0.0f,             /* rotation */
               C
    olor.Aquamarine, /* outside color */
              
    Color.Purple,     /* inside color */
              
    null);            /* texture file */
          }

    #endregion



Lesson Learned: Mush simpler code supporting similar functionality!

Also, notice the clear structure of event-driven model:

After the two one-time only functions, the rest of the application is a continuous cycle of:

It is important to note that, under this model, our program should not draw during UpdateWorld() and should not update application state during DrawWorld().


This document and the related materials are developed with support from Microsoft Research Computer Gaming Initiative under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871.


Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu