| 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:

The game behaves in a manner that is identical to the what was described for the previous tutorial, with two changes:
The game now tracks (and displays) how many times the player missed the ball (and as a result, the ball bounced off the left or right side of the screen).
The program now makes use of nested conditional statements to detect whether the ball needs to be bounced off the edge of a screen. As part of this change, we will also play a sound when the ball bounces off an edge of the screen.
Let's examine the source code, change by change
Let's examine the C# source code that produces the behavior we see on-screen. Since the code is nearly identical to the program that was presented in the previous tutorial, we'll leave out everything, except for code that has changed, or code that is new. Further, we'll move through the changes feature by feature, staring with the feature that in this version of the game, the ball will now bounce off all the edges of the screen using a nested conditional statement.
While functionally equivalent to what we had previous done, this version
will be slightly more efficient, and will also make it easier for other
programmers to understand what our intent was, when we wrote this code
CheckWorldBound():
|
///
<summary> /// Check the center position of the soccer ball, if it is outside /// of the world bound, flip the ball velocity accordingly /// </summary> private void CheckWorldBound() { if (m_TheSoccer.CenterX > World.WorldMax.X){ m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; PlayACue( "Wall");m_BallsMissed = m_BallsMissed + 1; } else if (m_TheSoccer.CenterX < World.WorldMin.X) { m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; PlayACue( "Wall");m_BallsMissed = m_BallsMissed + 1; } if (m_TheSoccer.CenterY > World.WorldMax.Y) { m_TheSoccer.VelocityY = -m_TheSoccer.VelocityY; PlayACue( "Bounce4");} else if (m_TheSoccer.CenterY < World.WorldMin.Y) { m_TheSoccer.VelocityY = -m_TheSoccer.VelocityY; PlayACue( "Bounce4");} } |
|
private
void
CheckWorldBound() { if (m_TheSoccer.CenterX > World.WorldMax.X){ m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; PlayACue( "Wall");m_BallsMissed = m_BallsMissed + 1; } else { if (m_TheSoccer.CenterX < World.WorldMin.X) { m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; PlayACue("Wall"); m_BallsMissed = m_BallsMissed + 1; } } if (m_TheSoccer.CenterY > World.WorldMax.Y) { m_TheSoccer.VelocityY = -m_TheSoccer.VelocityY; PlayACue("Bounce4"); } else { if (m_TheSoccer.CenterY < World.WorldMin.Y) { m_TheSoccer.VelocityY = -m_TheSoccer.VelocityY; PlayACue("Bounce4"); } } } |
3. Tracking (And Displaying) Missed Balls
Eventually, the game will be set up so that when a player fails to bounce the ball off the paddle, and the ball hits the left/right wall behind the paddle, the ball will be placed back at it's starting position with a new, random velocity, and the count of times that the player has missed a ball will be incremented. For right now, though, we want to slowly, incrementally work towards this overall goal, by implementing just part of that. Specifically, we will implement the part that keeps track of the number of times that the player has missed a ball, and display that number to the player. Since the ball is not allowed to leave the screen, 'missing' the ball will mean that the ball has bounced off the left (or right) edge of the screen. Overall, the approach we'll use to implement this feature is very similar to what we did to keep track of the number of bounces: we'll declare and initialize an instance variable to store the current number of misses, we'll increment that counter whenever the player misses a ball, and we'll display that number at the bottom of the screen.
|
<code above this point
omitted for clarity> private XNACS1Rectangle m_LeftPaddle; private XNACS1Rectangle m_RightPaddle; // collect statistics private int m_BallsMissed; private int m_NumBounces; private string m_SkillLevel; <code below this point omitted for clarity> |
|
protected
override
void
InitializeWorld() { World.SetWorldCoordinate(new Vector2(0,0), 100.0f); // initilaize the soccer ball InitializeSoccer(); // initialize the left and right paddles m_LeftPaddle = InitializeRectangle(LEFT_PADDLE_X, PADDLE_INIT_Y, PADDLE_WIDTH, PADDLE_HEIGHT); m_RightPaddle = InitializeRectangle(RIGHT_PADDLE_X, PADDLE_INIT_Y, PADDLE_WIDTH, PADDLE_HEIGHT); // initialize statistics m_NumBounces = 0; m_SkillLevel = "Novice";m_BallsMissed = 0; } |
|
private
void
CheckWorldBound() { if (m_TheSoccer.CenterX > World.WorldMax.X){ m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; PlayACue( "Wall");m_BallsMissed = m_BallsMissed + 1; } else if (m_TheSoccer.CenterX < World.WorldMin.X) { m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; PlayACue( "Wall");m_BallsMissed = m_BallsMissed + 1; } if (m_TheSoccer.CenterY > World.WorldMax.Y) { m_TheSoccer.VelocityY = -m_TheSoccer.VelocityY; PlayACue( "Bounce4");} else if (m_TheSoccer.CenterY < World.WorldMin.Y) { m_TheSoccer.VelocityY = -m_TheSoccer.VelocityY; PlayACue( "Bounce4");} } |
|
<code above this point
omitted for clarity> if (m_NumBounces > EXPERT_LEVEL_BOUNCES) { // String equality test if (m_SkillLevel == "Novice") { PlayACue("Victory"); m_TheSoccer.VelocityX = m_TheSoccer.VelocityX + Math.Sign(m_TheSoccer.VelocityX) * BALL_VELOCITY_EXPERT_BOOST_X; m_TheSoccer.VelocityY = m_TheSoccer.VelocityY + Math.Sign(m_TheSoccer.VelocityY) * BALL_VELOCITY_EXPERT_BOOST_Y; } m_SkillLevel = "Expert"; } EchoToBottomStatus("[" + m_SkillLevel + "]: bounces=" + m_NumBounces + " Balls Missed:" + m_BallsMissed); } // end of UpdateWorld |
FURTHER EXERCISES::
For this exercise, you should use the same project that was explained in the
above tutorial.
See if you can change the program so that when the player has bounced the
ball off of the paddles 10 times, the message at the top of the screen is
changed to "You've Won!". Next, see if you can set things up (using a
nested if statement), so that you only display that message if the ball has
bounced at least 10 times, and the number of times the ball was missed is
LESS than 5 times.