Developing Programming Assignments on the XBOX 360 Console

Section I: Introduction and Overview
d. Interactive control of the triangle

back to workshop main page


References:

Goals:

Notice:


Compile and Run: Download the source code, compile and run and you will see this output:
If you have a wired XBOX 360 controller connected to your PC, notice you can move the triangle around with the left thumbstick. If you do not have the controller, notice you can move the triangle around using the arrow keys.


Summary of changes to support interactive control: in order to move the triangle, there must be a reference position, and we must update the reference position based on user input. In this case, we have modified our code in four places:
  1. Instance variables: for defining the position of the triangle: Vector2.
  2. Initialize():  allocate and initialize the position of the triangle.
  3. Update(): change the reference position based on user input.
  4. Draw():  draws the triangle based on the reference position.

Here are the details, each of the following changes are enclosed by regions with label: __CODE_ADDED__:

  1. Declare new instance variables (in the beginning of the file) for reference position of the triangle:
    #region __CODE_ADDED__
       ... // EffectPool and BasicEffect exactly the same as previously
       Vector2 pos; // Center position of the triangle

    #endregion

  2. Initialize the triangle position  (in Initialize() function):
    #region __CODE_ADDED__ // Initialize the graphics rendering state
      ... // code exactly the same as previously
                    
      // Triangle position

      pos = new Vector2();
      pos.X = 0.0f;       // Initial position is at the center of the application window
      pos.Y = 0.0f;
    #endregion

  3. Updates the position based on user input (in Update() function):
    #region __CODE_ADDED__
       if (GamePad.GetState(PlayerIndex.One).IsConnected)  { // either from controller ...
           Vector2 delta = GamePad.GetState(PlayerIndex.One).ThumbSticks.Left;
           pos = delta;
       } else {    // or from the keyboard
      
        float dx = 0.0f, dy = 0.0f;
           if (Keyboard.GetState().IsKeyDown(Keys.Right)) dx += 0.01f;
           if (Keyboard.GetState().IsKeyDown(Keys.Left))  dx -= 0.01f;
           if (Keyboard.GetState().IsKeyDown(Keys.Up))    dy += 0.01f;
           if (Keyboard.GetState().IsKeyDown(Keys.Down))  dy -= 0.01f;
           pos.X += dx;
           pos.Y += dy;
       }

    #endregion
     
  4. Draw a triangle (in Draw() function):
    #region __CODE_ADDED__
       // exactly the same as previously, the only difference is the position ...
       ...
       // Triangle vertex position based on center at current position
       Vector3 p = new Vector3(pos.X, pos.Y, 0);

       ...    // rest of the code is identical to as previously
    #endregion

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