XNA GameThemed 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 on the screen, each labeled with each rectangle's current height (and how that height was calculated) At the bottom of the screen are instructions for playing game: by using the left thumbstick, you can control the current height of the leftmost rectangle (rectangle "A").
The middle rectangle's height is set to be 20% of rectangle A. The rightmost rectangle's height is set to be equal to rectangle A's height, minus 20% of A's height. All numbers are calculated using floatingpoint numbers (instead of integer numbers).
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 onscreen
We need to declare our instance variables before we can use them.
public
class Game1 :
XNACS1Base
{
private XNACS1Rectangle aRec; // leftmost rectangle private XNACS1Rectangle percentRec; // middle rectangle private XNACS1Rectangle subRec; // rightmost rectangle

We told C# to create instance variables for our Game1 in the code that's described above. It's important that we give our variables welldefined values before we use them, like so:
protected
override
void
InitializeWorld()
World. SetWorldCoordinate( new Vector2 (0,0), 100.0f); float aHeight; // Height of rectangleA float percentHeight; // 20% of rectangleA height float subHeight; // Subtracted height: the height of A  (20% of the height of A)
percentHeight = aHeight * 0.2f; subHeight = aHeight  percentHeight; aRec = new XNACS1Rectangle (); aRec.LowerLeft= new Vector2 (20.0f, 20.0f); aRec.Height = aHeight; aRec.Width = 20.0f; percentRec = new XNACS1Rectangle (); percentRec.LowerLeft = new Vector2 (40.0f, 20.0f); percentRec.Height = percentHeight; percentRec.Width = 20.0f; subRec = new XNACS1Rectangle (); subRec.LowerLeft = new Vector2 (60.0f, 20.0f); subRec.Height = percentHeight; subRec.Width = 20.0f; } 
float percentHeight; // 20% of rectangleA height
float subHeight; // Subtracted height: the height of A  (20% of the height of A)
Technically, we could probably get away with not creating the aHeight variable, but we will do so anyways, as it will look slightly nicer when we use it to calculate the other values we need. The comments next to each variable are there to indicate what value the variable is intended to hold.
percentHeight = aHeight * 0.2f;
subHeight = aHeight  percentHeight;
Let's examine how the multiplication operator works, in detail, on the line percentHeight = aHeight * 0.2f;
percentRec.Height = percentHeight;
subRec.Height = percentHeight;
protected
override
void
UpdateWorld()
{ if (GamePad.ButtonBackClicked()) this .Exit(); float leftThumbY = GamePad.ThumbSticks.Left.Y; float aHeight = aRec.Height; // Height of rectangleA float percentHeight; // 20% of rectangleA height float subHeight; // Substracted height // accumuate rectangleA's height aHeight = aHeight + leftThumbY; // compute 20% of rectangleA's height percentHeight = aHeight * 0.2f; // compute the subtraction subHeight = aHeight  percentHeight; // Assign the heights and labels to the corresponding rectangles aRec.Height = aHeight; aRec.Label = "A Height=" + aRec.Height; percentRec.Height = percentHeight; percentRec.Label = "20% of A Height=" + percentRec.Height; subRec.Height = subHeight; subRec.Label = "A  20% of A=" + subRec.Height; EchoToBottomStatus( "LeftThumbY adjust AHeight" ); } 
float leftThumbY = GamePad.ThumbSticks.Left.Y;
float aHeight = aRec.Height; // Height of rectangleA
float percentHeight; // 20% of rectangleA height
float subHeight; // Substracted height
// accumuate rectangleA's height
aHeight = aHeight + leftThumbY;
// compute 20% of rectangleA's height
percentHeight = aHeight * 0.2f;
// compute the subtraction
subHeight = aHeight  percentHeight;
These should all be familiar from previously covered material.
aRec.Height = aHeight;
aRec.Label = "A Height=" + aRec.Height;
percentRec.Height = percentHeight;
percentRec.Label = "20% of A Height=" + percentRec.Height;
subRec.Height = subHeight;
subRec.Label = "A  20% of A=" + subRec.Height;
FURTHER EXERCISES::
Kelvin Sung
Computing and Software Systems University of Washington, Bothell ksung@u.washington.edu 
Michael Panitz
Business And Information Technology Cascadia Community College mpanitz@cascadia.eduu 
This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.  
2/8/2010 