| XNA Game-Themed CS1 Examples ( XGC1 ) | |
|
Release 2.0 (XNA V3.1)
|
|
Goals:
1. Obtain the example code
This program draws two rectangles on the screen, as well as a circle. The circle is labeled with it's area.
At the bottom of the screen are instructions for playing game. By using the left thumbstick's vertical axis (the Y axis), you can control both the heights of the rectangles, as well as the radius of the circle. Pushing the left thumbstick up (or forward) increases the heights & radius, pushing downward decreases them. You'll notice that pushing the thumbstick doesn't change things very quickly; this is done on purpose.
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 and named constants before we can use them.
|
public
class Game1 :
XNACS1Base
{
// Named Constants private const float REC_WIDTH = 10.0f; // Width of the Rectangles private const float RATE_SCALE = 0.05f; // Multiplier for rate // Normal instance variables: private XNACS1Rectangle leftRec; private XNACS1Rectangle rightRec; private XNACS1Circle Circ;
|
|
protected
override
void
InitializeWorld()
World.SetWorldCoordinate(
new Vector2(0,0),
100.0f);
leftRec =
leftRec.LowerLeft = new Vector2 (5.0f, 5.0f); leftRec.Height = 10.0f; leftRec.Width = REC_WIDTH; rightRec = new XNACS1Rectangle (); rightRec.LowerLeft = new Vector2 (20.0f, 5.0f); rightRec.Height = 10.0f; rightRec.Width = REC_WIDTH; Circ = new XNACS1Circle (); Circ.Center = new Vector2 (70, 30.0f ); Circ.Radius = 10.0f; Circ.Label = "Area: " + ( float )( Math .PI * Math .Pow(Circ.Radius, 2)); } |
and
rightRec.Width = REC_WIDTH;
Let's examine how this works, in detail:
Next up, we do the multiplication. While we're at it, we'll remove the parentheses that surround the multiplication, since we won't need them after this step, giving us:
Circ.Label = "Area: " + (float) 314.159265358979 ;
Circ.Label = "Area: 314.1593" ;
UpdateWorld():
|
protected
override
void
UpdateWorld()
{ if (GamePad.ButtonBackClicked()) this .Exit(); float leftThumbY = GamePad.ThumbSticks.Left.Y * RATE_SCALE; // Update the rectangles leftRec.Height = leftRec.Height + leftThumbY; rightRec.Height = rightRec.Height + leftThumbY; // Update the circle Circ.Radius = Circ.Radius + leftThumbY; Circ.Label = "Area: " + ( float )( Math .PI * Math .Pow(Circ.Radius, 2)); EchoToBottomStatus( "LeftThumb-Y: adjust height of rectangles, and radius of circle" ); } |
You'll notice that there's an extra term, * RATE_SCALE , tacked onto the end. What this will do is multiply the value of the left thumbstick's vertical axis by the value of the named constant. So if you pushed the thumbstick all the way up, giving GamePad.ThumbSticks.Left.Y the value of 1, then GamePad.ThumbSticks.Left.Y * RATE_SCALE would have the value of 1 * 0.05f , or 0.05f.
EchoToBottomStatus( "LeftThumb-Y: adjust height of rectangles, and radius of circle" );
FURTHER EXERCISES::
Start this exercise using
Exercise_
2
's starter project
,
which is a nearly identical copy of the project that was used in the above
tutorial.
Examine the code used in InitializeWorld. Notice that the height of
all the rectangles is 10.0f. Notice also that this height changes as
soon as the user pushes the thumbstick. For this exercise, in the
InitializeWorld method, replace the rectangle's initial heights of 10.0f with
a new, named constant called something like REC_STARTING_HEIGHT.
Continue working from the same project that you used in the prior exercise. Notice that in the InitializeWorld method, the starting heights of both rectangles is 10.0f, as defined by the named constant REC_STARTING_HEIGHT. Notice that the starting radius for the circle is also 10.0f.
For this exercise, in a comment immediately above the line that assigns a
starting value to the circle's radius, answer the following question:
Should you replace the circle's starting radius with the named constant
REC_STARTING_HEIGHT
(since the starting radius is also 10.0f)? What would the advantages of this be, and
what would the disadvantages be?
Continue working from the same project that you used in the
prior exercise.
Go through the code, and see if there are other numbers that you can replace
with named constants. For each number, put in a quick (1-2 sentence)
comment explaining why (or why not) you think that a named constant is a
good thing to use.