XNA Game-Themed CS1 Examples (XGC1) Release 2.0 (XNA V3.1) 2/8/2010
Topic: Topic.4.DecisionStructures
Example: Ex_2.SimpleIf

# Decision Statements: Simple "If" Statements

References:
• Pre-requisite: it is assumed that you have read through the prior tutorials, and are familiar with the concepts covered in those tutorials.

Goals:

• Throughout this chapter, we will work towards a simple game, with each tutorial adding a little bit more to what we've already done.  In this tutorial, we're going to:
1. Change the program so that the ball will bounce off the right edge of the screen

1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source 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 one new change: If the soccer ball reaches the right edge of the screen, it will 'bounce off' that edge, meaning that it will reverse it's horizontal direction.  Notice that if the ball is moving rightwards and upwards when it hits the right edge, it turns around so that it's moving leftwards but still upwards

2. Examining The Program:

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.  That leaves us with:

• UpdateWorld():

 protected override void UpdateWorld(){     // move the soccer center by velocity     m_TheSoccer.Center = m_TheSoccer.Center + m_BallVelocity;     // This is the if condition!     if (m_TheSoccer.CenterX > 100.0f)     {         m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX;     }     EchoToTopStatus("Left Paddle Center=" + m_LeftPaddle.Center + " Right Paddle Center=" + m_RightPaddle.Center);     EchoToBottomStatus("Soccer Ball position: " + m_TheSoccer.Center); }
• Let's first examine the syntax for the new code that has been added:
if (m_TheSoccer.CenterX > 100.0f)

{

m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX;

}

• The word if MUST be lowercase.  If you type it correctly, Visual Studio will color-code it blue, to tell you that it recognizes this word as being a keyword in the C# language.
• The if MUST be followed by the open parenthesis - (.  You can optionally choose to put blank spaces between the if  and the ( (as is done above), or you can put them right next to each other - that choice is yours.
• The opening parenthesis must have a closing ).  The closing parenthesis does not have to be on the same line, but for something as short as what we've written above, there's no reason to put it on any further down.
• Between the opening and closing parentheses, there must be an expression that evaluates to either the value true, or the value false.  We'll talk more about this in a minute.  We'll refer to this expression as the test, or the thing that we're testing.
• Notice that there is NO SEMI-COLON at the end of the line.  Putting a semi-colon at the end will compile, but will NOT do what you want it to!
• The line beneath the if statement contains an opening curly brace - {.  There's a matching, closing curly brace - }- a couple of lines down.
• The curly braces are used to surround all the commands that we want to execute, if the if  statement is true.
• Technically, since there's only one, single statement between the curly braces, we can actually omit the curly braces and still have everything work just fine.  However, it's not a bad idea to get into the habit of putting curly braces in, whether you need them or not, so that you don't forget them if you decide to go back & add more statements here later on.
• Now, let's examine the semantics of the new code:
• Conceptually, what's happening is that the program arrives at the line if (m_TheSoccer.CenterX > 100.0f),  and asks "is the value of the X part of the center point of the soccer ball larger than 100?"  If that's true, then the program will execute all the statements inside the curly braces.  If that's not true, then the program will skip over the statements in the curly braces. Pictorially, we can represent this like so:

• If the thing that we're testing (m_TheSoccer.CenterX > 100.0f) evaluates to true, then this means that we've gotten the object representing the soccer ball (m_TheSoccer), gotten the X value of the coordinates of it's center point (m_TheSoccer.CenterX), compared it to 100 (m_TheSoccer.CenterX > 100.0f), and found that the X value of the soccer ball's center is greater than 100.
• Since we set the width of the screen to be 100.0f (in the constructor), what we're really asking is "is the soccer ball's center point beyond the right edge of the screen?".
• If this is true, then we execute the code inside:
m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX;

This takes the current value of the ball's speed along the X axis, and makes it negative.  So if the ball was moving, say, +0.5f horizontally (meaning that at each update, it was moved 0.5f units towards the right), then after this statements is executed, the ball will be moving -0.5f horizontally (meaning that at each update, it was moved 0.5f units towards the left, because it's speed is -0.5f)

FURTHER EXERCISES::

• Start from a blank starter project (1000.201, if you need it), and re-do the code from memory as much as possible.  On your first try, do what you can, and keep the above code open so that when you get stuck, you can quickly look up what you forgot (and that after you finish a line, so that you can compare your line to the 'correct' line).  On the next try, do the same thing, but try to use the finished code less.  Repeat this until you can type everything, without refering the tutorial's code.
• Repeat this exercise daily for several days, so that you really get the hang of this.  As you go on, periodically review this by re-doing this exercise.
• Familiarizing Yourself With The Syntax For "If" Statements
For this exercise, you should use the same project that was explained in the above tutorial.
Go to the lines wherein the if statement is demonstrated:

// This is the if condition!

if (m_TheSoccer.CenterX > 100.0f)

{

m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX;

}

1. Try removing the curly braces, and see if you can get it to compile and run.  Will it compile?  Will it run without crashing?  Will it run correctly (i.e., will it do what we want it to do?)  Why?
2. Put the curly braces back in.  Remove the EchoToBottomStatus statement that's  a couple of lines down.  Now go back to the if statement, and add in a line to EchoToBottomStatus a mesage like "Soccer ball has bounced off of right edge", and make sure that you only see the message after the ball has bounced off the wall. Make sure that you add the EchoToBottomStatus command inside of the curly braces (before or after the m_TheSoccer.VelocityX = -m_TheSoccer.VelocityX; line)
3. Once you've added the EchoToBottomStatus command try removing the if statement's curly braces again, and see if you can get it to compile and run.  Will it compile?  Will it run without crashing?  Will it run correctly (i.e., will it do what we want it to do?)  Why?
• Familiarizing Yourself With The Syntax For "If" Statements: Adding another horizontal check in
For this exercise, you should use the same project that was explained in the above tutorial.
For this exercise, try adding in another if statement so that if the CenterX value of the soccer ball is less than 0, the horizontal velocity of the soccer ball should be flipped, again.
• Familiarizing Yourself With The Syntax For "If" Statements: Adding a vertical check in
For this exercise, you should use the same project that was explained in the above tutorial.
For this exercise, try adding in another if statement so that if the CenterY value of the soccer ball is greater than 56 (which is about where the top of the screen is), the vertical velocity of the soccer ball should be flipped.
• Familiarizing Yourself With The Syntax For "If" Statements: Adding another vertical check in
For this exercise, you should use the same project that was explained in the above tutorial.
For this exercise, try adding in another if statement so that if the CenterY value of the soccer ball is less than 0 (which is where the bottom of the screen is), the vertical velocity of the soccer ball should be flipped.
• Review: "If' statements, and formulas
For this exercise, you should use the same project that was explained in the above tutorial.
You'll notice that the soccer ball doesn't bounce off the edge of the screen until the soccer ball is halfway off the screen.  This is happening because we're checking if the X value of the center of the ball has gone beyond the right edge of the screen, rather than checking if the right edge of the ball has gone off-screen.  If we wanted to check if the right edge of the ball has gone off screen, we'd have to use a formula to figure out where the right edge of the ball is, then ask if that is greater than 100.
For this exercise, comment out your current, working if statement for bouncing the ball off the right edge of the screen.  Next, add in an if statement that will bounce the ball off the right edge of the screen, if the right edge of the soccer ball is greater than 100.  The following picture may help you visualize this: