| 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 four rectangles on the screen, each labeled with each rectangle's name, and 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 second rectangle from the left (rectangle "B") The third rectangle from the left (labled 'Fixed') has a fixed height, meaning that the height never changes. The height of the right-most rectangle (labeled 'Average') is the average of the heights of the first three.
Additionally, when player presses the 'Back' button (or the keyboard equivalent), the program will exit.
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
{
private XNACS1Rectangle aRec; private XNACS1Rectangle bRec; private XNACS1Rectangle fixedRec; private XNACS1Rectangle avgRec;
|
|
protected
override
void
InitializeWorld()
World.
SetWorldCoordinate(
new
Vector2
(0,0),
100.0f);
float bHeight = 20.2f; float fixedHeight = 27.8f; float averageHeight; // average of the above three values averageHeight = (aHeight + bHeight + fixedHeight) / 3.0f; aRec = new XNACS1Rectangle ();aRec.LowerLeft= new Vector2 (10.0f, 20.0f);aRec.Height = aHeight; aRec.Width = 20.0f;
bRec = new XNACS1Rectangle ();bRec.LowerLeft = new Vector2 (30.0f, 20.0f);bRec.Height = bHeight; bRec.Width = 20.0f; fixedRec = new XNACS1Rectangle ();fixedRec.LowerLeft = new Vector2 (50.0f, 20.0f);fixedRec.Height = fixedHeight; fixedRec.Width = 20.0f; fixedRec.Label = "Fixed Height=" + fixedRec.Height;avgRec = new XNACS1Rectangle ();avgRec.LowerLeft = new Vector2 (70.0f, 20.0f);avgRec.Height = averageHeight; avgRec.Width = 20.0f; } |
float aHeight = 15.2f;
float bHeight = 20.2f;
float fixedHeight = 27.8f;
float averageHeight; // average of the above three values
Technically, we could probably get away with not creating the first three variables, but we will do so anyways, as it will look slightly nicer when we use it to calculate the average. Notice that we've chosen to both declare and initialize each of the first three variables on one line each.
Let's examine how this works, in detail:
As explained above, the addition operators will be
evaluated from left to right, so we can start by mentally replacing the
two variables' names with their values. Notice that we don't do
anything with the other variables just yet.
averageHeight = (
15.2f
+
20.2f
+ fixedHeight
)
/ 3.0f;
UpdateWorld():
|
protected
override
void
UpdateWorld()
{ if (GamePad.ButtonBackClicked()) this .Exit(); float leftThumbY = GamePad.ThumbSticks.Left.Y; float rightThumbY = GamePad.ThumbSticks.Right.Y; float aHeight = aRec.Height; float bHeight = bRec.Height; float fixedHeight = fixedRec.Height; float averageHeight; // average of the above three values // Accumulate height for rectangles A and B aHeight = aHeight + leftThumbY; bHeight = bHeight + rightThumbY; // compute average height averageHeight = (aHeight + bHeight + fixedHeight) / 3.0f; // Assignment the height values to corresponding rectangles aRec.Height = aHeight; aRec.Label = "A Height=" + aRec.Height; bRec.Height = bHeight; bRec.Label = "B Height=" + bRec.Height; avgRec.Height = averageHeight; avgRec.Label = "Average Height=" + avgRec.Height; EchoToBottomStatus( "LeftThumb-Y adjust A-Height; RightThumb-Y adjust B-Height" ); } |
float leftThumbY = GamePad.ThumbSticks.Left.Y;
float rightThumbY = GamePad.ThumbSticks.Right.Y;
float aHeight = aRec.Height;
float bHeight = bRec.Height;
float fixedHeight = fixedRec.Height;
float averageHeight; // average of the above three values
aHeight = aHeight + leftThumbY;
bHeight = bHeight + rightThumbY;
These should all be familiar from previously covered material.
// compute average height
averageHeight = (aHeight + bHeight + fixedHeight) / 3.0f;
aRec.Height = aHeight;
aRec.Label = "A Height=" + aRec.Height;
bRec.Height = bHeight;
bRec.Label = "B Height=" + bRec.Height;
avgRec.Height = averageHeight;
avgRec.Label = "Average Height=" + avgRec.Height;
EchoToBottomStatus( "LeftThumb-Y adjust A-Height; RightThumb-Y adjust B-Height" );
FURTHER EXERCISES::