| XNA Game-Themed CS1 Examples ( XGC1 ) | |
|
Release 2.0 (XNA V3.1)
|
|
Goals:
1. Obtain the example code
Once we compile and run this project, the program displays three rectangles in the screen, labeled with each rectangle's current height. At the bottom of the screen are instructions for playing game: by using the left thumbstick, you can control the current height of the left-most rectangle (rectangle "A"), by using the right thumbstick, you can control the current height of the middle rectangle (rectangle "B"). The height of the right-most rectangle is automatically set to be the sum of the heights of the other two rectangles, as pictured below.
Additionally, when player presses the 'Back' button (or the keyboard equivalent), the program will exit.
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
We need to declare our instance variables before we can use them.
|
public
class Game1 :
XNACS1Base
{
// The rectangles: private XNACS1Rectangle aRec; // Rectangle "A" private XNACS1Rectangle bRec; // Rectangle "B" private XNACS1Rectangle addRec; // Rectangle that will be "A+B" |
We told C# to create instance variables for our Game1. It's important that we give our variables well-defined values before we use them, like so:
|
protected
override
void
InitializeWorld()
World.
SetWorldCoordinate(
new
Vector2
(0,0),
100.0f);
aRec =
aRec.LowerLeft = new Vector2 (10.0f, 20.0f); aRec.Height = 10.0f; aRec.Width = 20.0f; bRec = new XNACS1Rectangle (); bRec.LowerLeft = new Vector2 (40.0f, 20.0f); bRec.Height = 5.5f; bRec.Width = 20.0f; addRec = new XNACS1Rectangle (); addRec.LowerLeft = new Vector2 (70.0f, 20.0f); addRec.Width = 20.0f; addRec.Height = aRec.Height + bRec.Height; // Since 10f + 5.5f = 15.5, we could replace the above line with this one: // addRec.Height = 15.5f; } |
addRec.Height = aRec.Height + bRec.Height;
If we look at this part by part, we see that it breaks down into a series of simple steps, based on the order of operations for the various operators that are here:
|
protected
override
void
UpdateWorld()
{ if (GamePad.ButtonBackClicked()) this .Exit(); // copy the thumbstick values into local variables with shorter names: float leftThumbY = GamePad.ThumbSticks.Left.Y; float rightThumbY = GamePad.ThumbSticks.Right.Y; // Copy the current rectangle heights into local variables with shorter names: float aHeight = aRec.Height; // Height of A and B Rectangles float bHeight = bRec.Height; // Accumulate height for A and B aHeight = aHeight + leftThumbY; bHeight = bHeight + rightThumbY; // Addtion to compute addRed's height float addHeight = aHeight + bHeight; // Update the rectangles with the new heights aRec.Height = aHeight; bRec.Height = bHeight; addRec.Height = addHeight; // Update the labels, so they also have the new heights aRec.Label = "A Height=" + aRec.Height; bRec.Label = "B Height=" + bRec.Height; addRec.Label = "A+B Height=" + addRec.Height; // After this method ends, the three rectangles will continue to exist, // but the local variables leftThumbY, rightThumbY, aHeight, bHeight, and addHeight // will not exist. EchoToBottomStatus( "LeftThumb-Y adjust A-Height; RightThumb-Y adjust B-Height" ); } |
// copy the thumbstick values into local variables with shorter names:
float leftThumbY = GamePad.ThumbSticks.Left.Y;
float rightThumbY = GamePad.ThumbSticks.Right.Y;
// Copy the current rectangle heights into local variables with shorter names:
float aHeight = aRec.Height; // Height of A and B Rectangles
float bHeight = bRec.Height;
aHeight = aHeight + leftThumbY;
bHeight = bHeight + rightThumbY;
For both lines, we do the same sequence of actions, so let's step through 1 line in detail, in order to fully understand how it works.
// Addtion to compute addRed's height
float addHeight = aHeight + bHeight;
You'll notice that we are, once again, composing a slightly different line out of parts we already know: adding two numbers, assigning the results of addition to a variable, and declaring a variable (with immediate initialization).
// Update the rectangles with the new heights
aRec.Height = aHeight;
bRec.Height = bHeight;
addRec.Height = addHeight;
These should all be fairly straightforward - we
simply take the local variable that is storing the new height of
a rectangle, and then assign that value to the Height property
of the appropriate rectangle. When the XNACS1Lib next
draws the rectangles on the screen, they will be drawn with
their new heights.
// Update the labels, so they also have the new heights
aRec.Label = "A Height=" + aRec.Height;
bRec.Label = "B Height=" + bRec.Height;
addRec.Label = "A+B Height=" + addRec.Height;
FURTHER EXERCISES::