| XNA Game-Themed CS1 Examples (XGC1) | |
|
Release 2.0 (XNA V3.1) |
|
Need (library reference):
References:
Goals:
1. Obtain the example code
When the game starts, you'll see a screen that looks similar to this:

The game behaves in a manner that is identical to the what was described for the previous tutorial, with a couple of new features:
It is possible to 'win' the game. The player can win the game by bouncing the ball of the paddles more than BOUNCES_NEEDED_TO_WIN times (in a row, without missing it), and missing the ball less than MAX_BALLS_MISSED_TO_WIN times. However, once the player has bounced the ball BOUNCES_NEEDED_TO_WIN times (in a row, without missing it), then further bounces will decrease the number of misses, until the player wins.
When the player wins, the game displays a "You've Won!"
screen, which looks like:

Once the player has won, the game stops updating (i.e., the ball & paddles stop
moving), and almost all user input is disabled.
At any time (before, or after winning), the player may press the 'A' button, and restart the game
Let's examine the source code, feature by feature
2. Winning the game
|
<code above this point
omitted for clarity> // collect statistics private int m_NumBounces; private String m_SkillLevel; private int m_MaxBounces; private bool m_YouHaveWon; <code below this point omitted for clarity> |
|
///
<summary> /// Initialize the statistics information /// </summary> private void InitializeStats() { m_YouHaveWon = false; m_BallsMissed = 0; m_NumBounces = 0; m_MaxBounces = 0; m_SkillLevel = "Novice";} |
|
<code above this point
omitted for clarity> // Bounces for Expert level private const int EXPERT_LEVEL_BOUNCES = 5; private const int BOUNCES_NEEDED_TO_WIN = 10; private const int MAX_BALLS_MISSED_TO_WIN = 2;
#endregion <code below this point omitted for clarity> |
|
<code above this point
omitted for clarity> EchoToBottomStatus("[" + m_SkillLevel + "] " + "CurrentBounces:" + m_NumBounces + " MaxBounces:" + m_MaxBounces + " Total Balls Missed:" + m_BallsMissed); // Check to see if the player has won if ((m_NumBounces >= BOUNCES_NEEDED_TO_WIN) && (m_BallsMissed < MAX_BALLS_MISSED_TO_WIN)) { m_YouHaveWon = true; BackgroundTexture = "WinImage"; } } // end of UpdateWorld |
if ((m_NumBounces >= BOUNCES_NEEDED_TO_WIN) && (m_BallsMissed < MAX_BALLS_MISSED_TO_WIN))
|
protected
override void
UpdateWorld() { if (GamePad.ButtonBackClicked())this.Exit(); if (GamePad.ButtonAClicked()) { m_TheSoccer.RemoveFromDrawSet(); InitializeStats(); InitializeSoccer(); BackgroundTexture = "";} // The player has won, then don't do anything else // Don't let the player move the paddles, // Don't move the ball, etc if (m_YouHaveWon) { return; } #region from previous tutorial // update the left and right paddles with thumbstick |
// Don't let the player move the paddles,
// Don't move the ball, etc
if (m_YouHaveWon)
{
return;
}
{
m_TheSoccer.RemoveFromDrawSet();
InitializeStats();
InitializeSoccer();
BackgroundTexture =
"";}
The if statement will evaluate to true if the player has just pushed the 'A' button, meaning that the player wants to restart the game.
Next, we reset all the numbers, including the current and maximum number of
bounces, and the number of missed balls (which all go back to zero), as well
as putting the game back into Novice mode.
InitializeStats();
Having done that, we can then create a brand new soccer ball, and pick a new, random velocity for it:
InitializeSoccer();
Finally, we want to remove the "You've Won" screen, which we do by
setting the BackgroundTexture property to be the empty string:
BackgroundTexture =
"";
|
private
void
BounceOffPaddles() { if (m_RightPaddle.Collided(m_TheSoccer)){ m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; PlayACue( "Bounce");if (m_NumBounces > BOUNCES_NEEDED_TO_WIN && m_BallsMissed >= MAX_BALLS_MISSED_TO_WIN) m_BallsMissed--; else m_NumBounces = m_NumBounces + 1; } else if (m_LeftPaddle.Collided(m_TheSoccer)) { m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; PlayACue( "Bounce");if (m_NumBounces > BOUNCES_NEEDED_TO_WIN && m_BallsMissed >= MAX_BALLS_MISSED_TO_WIN) m_BallsMissed--; else m_NumBounces = m_NumBounces + 1; } } |
There are basically four cases:
m_BallsMissed--;
else
m_NumBounces = m_NumBounces + 1;
The if statement asks "Does the player have enough bounces to win AND also have too many misses to win?" If that's true, then this new bounce 'cancels out' an earlier miss (by decrementing m_BallsMissed). In the other three cases, we simply increment m_NumBounces (which is handled for us by the else clause)
FURTHER EXERCISES::
You should create a method that will create all the blocks in the game, similar to the way in which you have a method which initializes the soccer ball. Make sure that this method can be used not only to initialize the game, but also to reset the positions of the blocks when the game is restarted. Once this is done, pushing the 'A' button should not only reset all the numbers, but should also create a new bunch of blocks on the screen (in new locations)