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:
- Pre-requisite: this is the
example (Section 1c. Drawing with XNA) we are building off from.
- You can start with this template source file (resulting source code from the above example), and
here is the source code we will generate at the end of this tutorial.
Goals:
- To experience with and examine simple user interaction with XNA.
Notice:
- If we change a variable during the Update() function and
the result in the Draw() function then we can create very
smooth control of objects.
- In the Update() function, we poll user's actions (either
controller or keyboard) and changes instance variables. We
do not draw in the Update()
function.
- In the Draw() function, we draw based on instance
variables. We do not change any instance
variables in the Draw() function.
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:
- Instance variables: for defining the position of the triangle:
Vector2.
- Initialize(): allocate and initialize the position of the
triangle.
- Update(): change the reference position based on user input.
- 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__:
-
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 |
-
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
|
-
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 |
-
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.