| 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 program for this tutorial is almost exactly identical to the program used in the the previous tutorial. The difference is that for this tutorial, we're using a for loop to count down rather than up (to count backwards rather than forwards)
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
|
// here is the for loop for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1) { float radian = ComputeRadianFromXPos(xPos); yPos = ComputeSineYPos(radian) ; CreateABallAt(xPos, yPos, "SoccerBall");} |
| Variable Name | Value |
| xPos | 100.0f |
This asks "Is the current value of xPos (which is 100.0f) greater than (or equal to) 0.0f?" This is true, so the program will execute the body of the loop.
yPos = ComputeSineYPos(radian) ;
CreateABallAt(xPos, yPos,
"SoccerBall");The above code will place a soccer ball at the appropriate place along the sine wave.
which decrementsthe value stored in counter by one:
| Variable Name | Value |
| xPos | 99.0f |
This asks "Is the current value of xPos (which is 99.0f) greater than (or equal to) 0.0f?" This is true, so the program will execute the body of the loop.
yPos = ComputeSineYPos(radian) ;
CreateABallAt(xPos, yPos,
"SoccerBall");The above code will place a soccer ball at the appropriate place along the sine wave.
which decrementsthe value stored in counter by one:
| Variable Name | Value |
| xPos | 98.0f |
The loop continues to iterate through many, many more iterations. We will not be tracing through all the iterations here.
The loop continues to iterate. The final iteration looks like this:
- At the start of each iteration of the loop, the condition is checked:
for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)This asks "Is the current value of xPos (which is now 0.0f, in the final iteration of the loop) greater than (or equal to) 0.0f?" This is true (because they're equal), so the program will execute the body of the loop.
- The body of the loop consists of the following lines of code:
float radian = ComputeRadianFromXPos(xPos);yPos = ComputeSineYPos(radian) ;
CreateABallAt(xPos, yPos,
"SoccerBall");The above code will place a soccer ball at the appropriate place along the sine wave.
- After the body of the loop has finished executing, then the counting expression is executed
for (float xPos = World.WorldMax.X; xPos >= 0.0f; xPos -= 1)which decrementsthe value stored in counter by one:
Variable Name Value xPos -1.0f
and asks "Is the current value of xPos (which is now -1.0f) greater than (or equal to) 0.0f?" This is NOT true, so the program will NOT execute the body of the loop.
FURTHER EXERCISES::