| 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 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 left-most rectangle (rectangle "A").
The middle rectangle's height is set to be 20% of rectangle A. The right-most rectangle's height is set to be equal to rectangle A's height, minus 20% of A's height. All numbers are calculated using floating-point 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 on-screen
We need to declare our instance variables before we can use them.
|
public
class Game1 :
XNACS1Base
{
private XNACS1Rectangle aRec; // left-most rectangle private XNACS1Rectangle percentRec; // middle rectangle private XNACS1Rectangle subRec; // right-most 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 well-defined values before we use them, like so:
|
protected
override
void
InitializeWorld()
World.
SetWorldCoordinate(
new
Vector2
(0,0),
100.0f);
float
aHeight;
//
Height of rectangle-A
float percentHeight; // 20% of rectangle-A 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 rectangle-A 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 rectangle-A float percentHeight; // 20% of rectangle-A height float subHeight; // Substracted height // accumuate rectangle-A's height aHeight = aHeight + leftThumbY; // compute 20% of rectangle-A'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( "LeftThumb-Y adjust A-Height" ); } |
float leftThumbY = GamePad.ThumbSticks.Left.Y;
float aHeight = aRec.Height; // Height of rectangle-A
float percentHeight; // 20% of rectangle-A height
float subHeight; // Substracted height
// accumuate rectangle-A's height
aHeight = aHeight + leftThumbY;
// compute 20% of rectangle-A'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::