| 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 rectangle on the far left is a 'paddle', which the soccer ball will bounce off of (if you move the paddle in the way of the soccer ball). You can move it up and down by pushing the left thumbstick up and down. The rectangle on the far right is a 'paddle', which the soccer ball will bounce off of if you move the paddle in the way of the soccer ball. You can move it up and down by pushing the right thumbstick up and down. The other, labeled rectangles are placed randomly in the middle of the screen. The soccer ball starts off in the middle of the screen, moving in a random direction at a random speed.
If the soccer ball hits the top or bottom of the screen, it will bounce off - it continue to move at the same speed, but the vertical component of it's velocity will be reversed (i.e., when it hits the top of the screen, instead of going upwards, it will switch and go downwards). If it hits one of the labeled rectangles, it will bounce off (reversing both it's horizontal & vertical speeds). If the soccer ball reaches the left or right edge of the screen then you "missed" it, and it is put back into the middle of the screen, with a new random direction. Missing the ball results in the CurrentBounces number going back to zero, and the "Balls Missed" number going up by one.
Your goal in this game is to bounce the soccer ball off the paddles as many times as you can. Once you've bounced it 5 times in a row (without missing it), then you go to expert mode, where things move a little bit faster. If you can bounce it 10 times in a row, and you've never missed more than two balls, then you win the game. If you can get to 10 bounces, but you've missed it more than twice, then each additional bounce beyond 10 will decrease your 'missed balls' count. What this means is that you can still win, although it will be more difficult the more you have missed the soccer ball. Notice that at the bottom of the screen, the game tells you whether you're in Novice or Expert mode, how many times (in a row) you've bounced the ball since the last time you missed it, the longest run of bounces you've managed to do, and the number of soccer balls that you've missed. At the top, the game tells you which labeled rectangle the soccer ball has most recently collided with.
| Controller Input: | Has this effect: |
| Left thumbstick | Moves the left paddle. Note that only the vertical input is used, because the paddle is only allowed to move up or down |
| Right thumbstick | Moves the right paddle. Note that only the vertical input is used, because the paddle is only allowed to move up or down |
| A button | Restarts the game: skill level goes back to "Novice", current bounces / max bounces / missed balls all go back to zero |
| Back button | Exits the game |
Once you reach expert mode, the screen should look
something like this:

Once you've won the game, the screen should look something
like this:

2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
|
public
class Game1 : XNACS1Base { #region random blocks and their positions private XNACS1Rectangle m_ABlock; private XNACS1Rectangle m_BBlock; private XNACS1Rectangle m_CBlock; private XNACS1Rectangle m_DBlock; private XNACS1Rectangle m_EBlock; private const float BLOCK_A_X_POSITION = 40.0f; private const float BLOCK_X_OFFSET = 10.0f;
|
|
m_CBlock.Label =
"C";
blockXPos = blockXPos + BLOCK_X_OFFSET; m_DBlock = InitializeRectangle(blockXPos,RandomFloat(BLOCK_POSITION_MIN_Y, BLOCK_POSITION_MAX_Y), BLOCK_WIDTH, BLOCK_HEIGHT); m_DBlock.Label = "D"; blockXPos = blockXPos + BLOCK_X_OFFSET; m_EBlock = InitializeRectangle(blockXPos,RandomFloat(BLOCK_POSITION_MIN_Y, BLOCK_POSITION_MAX_Y), BLOCK_WIDTH, BLOCK_HEIGHT); m_EBlock.Label = "E"; #endregion // initialize statisticsInitializeStats(); }
|
|
// Should we
move from Notice to Expert mode?
if
(m_MaxBounces > EXPERT_LEVEL_BOUNCES)
{ // String equality testif (m_SkillLevel == "Novice") { BackgroundTexture = "ExpertImage";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";} |
|
private
void
BounceOffBlocks() { if (m_ABlock.Collided(m_TheSoccer)){ m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; EchoToTopStatus( "Last block collision: A-Block");PlayACue( "Break");} else if (m_BBlock.Collided(m_TheSoccer)) { m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; EchoToTopStatus( "Last block collision: B-Block");PlayACue( "Break");} else if (m_CBlock.Collided(m_TheSoccer)) { m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; EchoToTopStatus( "Last block collision: C-Block");PlayACue( "Break");} else if (m_DBlock.Collided(m_TheSoccer)) { m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; EchoToTopStatus( "Last block collision: D-Block");PlayACue( "Break");} else if (m_EBlock.Collided(m_TheSoccer)) { m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; EchoToTopStatus( "Last block collision: E-Block");PlayACue( "Break");} } |
FURTHER EXERCISES::