| XNA Game-Themed CS1 Examples ( XGC1 ) | |
|
Release 2.0 (XNA V3.1)
|
|
Goals:
1. Obtain the example code
This program is intended to visually show much money you would have in the bank in 20 years, if you started with a particular amount of money now, and you were able to accrue a certain amount of interest each year. This is is calculated using the formula for compounding interest . Note that in this program, will will calculate the future value (i.e., how much money you'll have in the future), starting from the present value (i.e., how much money you have now). Another, different thing that we will NOT calculate here is the present value given a future value sum of money (i.e., how much money you should start with, if you want to end up saving up to a particular amount of money in the future).
Once we compile and run this project, the program displays two rectangles on the screen. The left rectangle is labeled with the present value (i.e., the sum of money that you start with), and the right rectangle is labeled with the future value (i.e., the sum of money you'll end up with). At the top of the screen, the number of years, and the interest rate are displayed. The heights of each rectangle are proportional to the values that they represent, and the distance between the 'future value' rectangle and the left edge of the screen is proportional to the number of years.
At the bottom of the screen are instructions for playing the game. By using the left thumbstick's vertical axis (the Y axis), you can control the present value of the money that you're saving (and thus, you can control the current height of the present value rectangle). By using the left thumbstick's horizontal axis (the X axis), you can control the rate. By using the right thumbstick's vertical (Y) axis, you can control the number of years that the money will be saved for (and thus, you can control the location of the future value rectangle). The height of the future value rectangle depends on the future value of the saved up money, which is calculated for you.
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 (to define, really) our instance variables before we can use them.
|
public
class Game1 :
XNACS1Base
{
private XNACS1Rectangle presentRec; private XNACS1Rectangle futureRec; private float presentValue; // present value of the money private float rate; // interest rate private float years; // number of years
|
|
protected
override
void
InitializeWorld()
World.
SetWorldCoordinate(
new
Vector2
(0,0),
100.0f);
presentValue = 10.0f;
years = 20;
rate = 0.02f;
presentRec = new XNACS1Rectangle (); presentRec.LowerLeft= new Vector2 (5.0f, 5.0f); presentRec.Height = presentValue; presentRec.Width = 10.0f; futureRec = new XNACS1Rectangle (); futureRec.LowerLeft = new Vector2 (years, 5.0f); futureRec.Height = futureValue; futureRec.Width = 10.0f; } |
presentValue = 10.0f;
years = 20;
rate = 0.02f;
Let's examine how this works, in detail:
As we established above, we need to first do the
Math
.Pow
,
before we can do the multiplication. And in order to do the
Math
.Pow
,
we need to do the addition.
In order to do the addition, we should first (mentally) substitute the
value that
rate
is storing
into the expression. We also need to make sure that both
numbers are of the same type - in this case, the 1 (which is
technically an integer) will be promoted to being a float. So
what we end up with is actually like this:
float futureValue = presentValue * (float)Math.Pow(( 1 .0f + 0.02f ), years);
float futureValue = presentValue * (float) 1.48594738295345 ;
UpdateWorld():
|
protected
override
void
UpdateWorld()
{ if (GamePad.ButtonBackClicked()) this .Exit(); float leftThumbY = GamePad.ThumbSticks.Left.Y; float leftThumbX = GamePad.ThumbSticks.Left.X; float rightThumbY = GamePad.ThumbSticks.Right.Y; // Get user's input, accumulate for: years, rate, and presentValue years = years + rightThumbY; rate = rate + (0.005f * leftThumbX); presentValue = presentValue + leftThumbY; // compute futureValue according to the formula float futureValue = presentValue * ( float ) Math .Pow((1 + rate), years); // Update the height & label of the present rectangle: presentRec.Height = presentValue; presentRec.Label = "present value=" + presentRec.Height; // Update the height & label of the future rectangle: futureRec.Height = futureValue; futureRec.Label = "future value=" + futureRec.Height; // Change futureValue's lower-left corner to show year futureRec.LowerLeft = new Vector2 (years, 5.0f); EchoToTopStatus( "Years=" + years + " rate=" + rate); EchoToBottomStatus( "LeftThumb-Y adjust Present Value; LeftThumb-X adjust rate; RightThumb-Y adjust Years" ); } |
float leftThumbY = GamePad.ThumbSticks.Left.Y;
float leftThumbX = GamePad.ThumbSticks.Left.X;
float rightThumbY = GamePad.ThumbSticks.Right.Y;
// Get user's input, accumulate for: years, rate, and presentValue
years = years + rightThumbY;
rate = rate + (0.005f * leftThumbX);
presentValue = presentValue + leftThumbY;
// compute futureValue according to the formula
float futureValue = presentValue * ( float ) Math .Pow((1 + rate), years);
presentRec.Height = presentValue;
presentRec.Label = "present value=" + presentRec.Height;
// Update the height & label of the future rectangle:
futureRec.Height = futureValue;
futureRec.Label = "future value=" + futureRec.Height;
// Change futureValue's lower-left corner to show year
futureRec.LowerLeft = new Vector2 ( years , 5.0f);
EchoToTopStatus( "Years=" + years + " rate=" + rate);
EchoToBottomStatus( "LeftThumb-Y adjust Present Value; LeftThumb-X adjust rate; RightThumb-Y adjust Years" );
FURTHER EXERCISES::
Hint: Make sure that you can do this using the Math.Pow method, so that you're able to use functions built into C#
Also: In addition to using Math.Pow, can you do this using nothing more than the arithmetic operators (i.e., nothing more than + , * , - and / ?)
For this exercise, you need to fill in each of the other rectangles, as they're labeled, with the following functions:
Notice that there are four rectangles - left-most one is rectangle min , the rightmost one is named max, and the two in the middle will be referred to as X (on the left) and Y (on the right). You should set the height of the min rectangle so that it's equal to the height of the smaller of the two middle rectangles, and set the height of the max rectangle so that it's equal to the height of the larger of the two middle rectangles. Additionally, you should label all rectangles with their name and current height.
- HINT: It may be useful to look these up online. The following seems to be a good resource: http://msdn.microsoft.com/en-us/library/system.math_methods.aspx , but you may find other resources online, too.