| XNA Game-Themed CS1 Examples ( XGC1 ) | |
|
Release 2.0 (XNA V3.1)
|
|
References:
Goals:
1. Obtain the example code
When the game starts, you'll see a screen that
looks similar to this:
In addition to the standard "exit when you push the Back button" functionality, you can adjust the size of the right soccer ball by pushing the right thumbstick left or right, and you can move both soccer balls up or down by pushing the right thumbstick up or down.. Notice that this "game" is extremely simple - we have written it purely to demonstrate the difference between passing an argument to a function by reference, and passing an argument by value.
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
|
public
class Game1 : XNACS1Base
{
private XNACS1Circle m_RefSoccer; // soccer ball radius changed by passing as reference private XNACS1Circle m_ValSoccer; // soccer ball radius changed by passing as value (default) #region Constants for circle/ball definitionprivate const float BALL_INIT_VALUE_X = 35.0f; private const float BALL_INIT_REF_X = 65.0f; private const float BALL_INIT_Y = 10.0f; private const float BALL_INIT_RADIUS = 2.5f; #endregion
|
We told C# to create instance variables for our Game1. 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);
m_RefSoccer = InitializeSoccer(BALL_INIT_REF_X);
m_ValSoccer =
InitializeSoccer(BALL_INIT_VALUE_X);
} |
|
///
<summary>
/// Allcoate memory and initialize soccer at different X positions /// </summary> /// <param name="xPos"> The x-position </param> /// <returns> An initialized Soccer with proper texture </returns> private XNACS1Circle InitializeSoccer( float xPos) { // initialize the soccer ball XNACS1Circle aSoccer = new XNACS1Circle (); aSoccer.Center = new Vector2 (xPos, BALL_INIT_Y);aSoccer.Radius = BALL_INIT_RADIUS; aSoccer.Texture = "SoccerBall" ;return aSoccer; } |
return aSoccer;
|
protected
override
void
UpdateWorld()
{ if (GamePad.ButtonBackClicked()) this.Exit(); // Pay attention to the following function calls!!! // 1. store radii of the two circles to local vars float byValRadius = m_ValSoccer.Radius; float byRefRadius = m_RefSoccer.Radius;
// 2. Compute the new radius // NOTICE: besides the "ref" keyword, the following two functions are identical! UpdateRadiusByVal(byValRadius); UpdateRadiusByRef( ref byRefRadius);// 3. assign the newly comptued radii to the respective circles m_ValSoccer.Radius = byValRadius; m_RefSoccer.Radius = byRefRadius; // 4. let user move the soccer balls up and down // You should think about byRef and byVal in this case! MoveSoccerByThumbStick(m_ValSoccer); MoveSoccerByThumbStick(m_RefSoccer); // 5. Output info to top & bottom of screen // after calling the above, the two radii should be the same?! EchoToTopStatus( "ByValSoccer_Y=" + m_ValSoccer.CenterY +" ByRefSoccer_Y=" + m_RefSoccer.CenterY); EchoToBottomStatus( "ByValRadius=" + byValRadius +" ByRefRadius=" + byRefRadius); } |
// 1. store radii of the two circles to local vars
float byValRadius = m_ValSoccer.Radius;
float byRefRadius = m_RefSoccer.Radius;
// NOTICE: besides the "ref" keyword, the following two functions are identical!
UpdateRadiusByVal(byValRadius);
UpdateRadiusByRef( ref byRefRadius);
m_ValSoccer.Radius = byValRadius;
m_RefSoccer.Radius = byRefRadius;
// You should think about byRef and byVal in this case!
MoveSoccerByThumbStick(m_ValSoccer);
MoveSoccerByThumbStick(m_RefSoccer);
// after calling the above, the two radii should be the same?!
EchoToTopStatus(
"ByValSoccer_Y=" + m_ValSoccer.CenterY +" ByRefSoccer_Y=" + m_RefSoccer.CenterY);
EchoToBottomStatus(
"ByValRadius=" + byValRadius +" ByRefRadius=" + byRefRadius);
|
///
<summary>
/// change the radius value according to right /// ThumbStick.X value /// /// </summary> /// <param name="radius"> value to be scaled. </param> private void UpdateRadius ByVal ( float radius) { radius = radius + GamePad.ThumbSticks.Right.X; } |
///
<summary>
/// change the radius value according to right /// ThumbStick.X value /// notice the "ref" keyword /// </summary> /// <param name="radius"> value to be scaled. </param> private void UpdateRadius ByRef ( ref float radius) { radius = radius + GamePad.ThumbSticks.Right.X; } |
|
///
<summary>
/// Change the centerY of aSoccer ball by right thumb stick y-movements. /// </summary> /// <param name="aSoccer"> The soccer to be operated on </param> private void MoveSoccerByThumbStick( XNACS1Circle aSoccer) { // Change Ball position with Right Thumb Stick aSoccer.CenterY += GamePad.ThumbSticks.Right.Y; } |
// 4. let user move the soccer balls up and down
// You should think about byRef and byVal in this case!
MoveSoccerByThumbStick(m_ValSoccer);
MoveSoccerByThumbStick(m_RefSoccer);
FURTHER EXERCISES:
Adding Parameters To A Function
For this exercise, you should use the same project that was explained in
the above tutorial.