| 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:
As you can see, this program draws three soccer balls across the screen. The program also draws a basketball on the screen, and allows you to move the basketball using the right thumbstick.
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
|
protected
override
void
InitializeWorld()
{ World.SetWorldCoordinate(new Vector2(0,0), 100.0f); m_BasketBall = CreateABallAt((BALL_INIT_X*5), "BasketBall" );} |
|
///
<summary>
/// Allocates and initialize the memory for a ball with /// y-predefined and x located at xPos and with aTex texture /// </summary> /// <param name="xPos"> x-location to create the new ball </param> /// <param name="tex"> the texture for the ball </param> private XNACS1Circle CreateABallAt( float xPos, String aTex) { XNACS1Circle aBall = new XNACS1Circle (); aBall.Center = new Vector2 (xPos, BALL_Y_POS);aBall.Radius = BALL_RADIUS; aBall.Texture = aTex; return aBall; } |
|
protected
override
void
UpdateWorld()
{ if (GamePad.ButtonBackClicked()) this .Exit(); #region Clear the display of all soccer balls and draw new ones!
// Remove all primitives from being drawn! (including the basket ball!!) RemoveAllFromDrawSet(); // Re-add the basket ball into the set to be drawn m_BasketBall.AddToDrawSet(); int counter = 0; float xOffset = 0.0f; // here is the while loop while (counter < 3) { CreateABallAt(xOffset + BALL_INIT_X, "SoccerBall" );counter = counter + 1; xOffset = xOffset + SPACE_BETWEEN_BALLS; } #endregion
m_BasketBall.CenterX += GamePad.ThumbSticks.Right.X; m_BasketBall.CenterY += GamePad.ThumbSticks.Right.Y; String msg = "Interaction: Can only interact with variables!" ; EchoToTopStatus(msg); EchoToBottomStatus( "but ... while loop is cool?" ); } |
RemoveAllFromDrawSet();
This command will remove all the circles (and rectangles) that are currently being drawn on the screen. This includes the soccer balls (that we want to remove) and the basketball (which we want to leave on the screen).
m_BasketBall.AddToDrawSet();
| Variable Name | Value |
| counter | 0 |
| Variable Name | Value |
| counter | 0 |
| xOffset | 0.0f |
counter = counter + 1;
xOffset = xOffset + SPACE_BETWEEN_BALLS;
Once the body of the loop has executed, there will be a new ball
(located at (
BALL_INIT_X
,
BALL_Y_POS
),
aka (10.0f, 30.0f), because xOffset has the value 0.0f),
Additionally, the variables
will have the following values after the body of the loop has executed:
| Variable Name | Value |
| counter | 1 |
| xOffset | 15.0f |
counter = counter + 1;
xOffset = xOffset + SPACE_BETWEEN_BALLS;
Once the body of the loop has executed, there will be second new ball
(located at (25.0f, 30.0f), and the variables will have the following
values:
| Variable Name | Value |
| counter | 2 |
| xOffset | 30.0f |
counter = counter + 1;
xOffset = xOffset + SPACE_BETWEEN_BALLS;
Once the body of the loop has executed, there will be second new ball
(located at (40.0f, 30.0f), and the variables will have the following
values:
| Variable Name | Value |
| counter | 3 |
| xOffset | 45.0f |
After that, it will execute the remainder of UpdateWorld, as you are already familiar with.
FURTHER EXERCISES::