XNA Game-Themed CS1 Examples (XGC1) Release 2.0 (XNA V3.1) 2/8/2010
Topic: Topic.5.RepetitionStructures
Example: Ex_7.ExampleWithDoWhile

Repetition: Multiple Do-While Loops At Once

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:

• In this tutorial, we will:
• Examine how to use multiple, non-nested loops at once.

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:

This program does not respond to use input, but does nicely demonstrate how to both (a) use multiple loops in one program, and (b) draw a straight line of things, which nicely complements the sinusoidal waves you've been seeing recently.

2. Examining The Program:

Let's examine the C# source code that produces the behavior we see on-screen

• Named Constants: private const float Y_INCREMENT = (9.0f / 16.0f);
• You'll notice that Y_INCREMENT is declared to be a ratio of two numbers: 9 : 16.  We will discuss this in more detail, below.
• UpdateWorld(): RemoveAllFromDrawSet(); // make sure remove all previously created balls from drawing         float xPos = 0.0f, yPos = World.WorldMax.Y / 2.0f;     // here are 2 do-while loops     do     {         CreateABallAt(xPos, yPos, "SoccerBall");         xPos = xPos + 1.0f;         yPos = yPos + Y_INCREMENT;     } while (yPos < World.WorldMax.Y);     do     {         CreateABallAt(xPos, yPos, "BasketBall");         xPos = xPos + 1.0f;         yPos = yPos - Y_INCREMENT;     } while (xPos < World.WorldMax.X);
• The new code in the UpdateWorld function can be grouped up into two categories, based on what the code accomplishes:
1. Starting at the left edge of the screen, draw the soccerballs in a straight line.
yPos is initialized to the World.WorldMax.Y / 2.0f, which is half way between the bottom and top of the screen.  xPos is initialized to be 0.0f, which is the left edge of the screen.  The following loop repeatedly draws a soccer ball, then moves rightward (along the X axis) by one unit, and upwards by Y_INCREMENT.
This loop continues to execute as long as the value of yPos is less than the Y value of the top of the screen.  Another way of saying this is that the loop will execute until the Y value of the center points of the soccerballs have gone above the top of the screen.
do

{

CreateABallAt(xPos, yPos, "SoccerBall");

xPos = xPos + 1.0f;

yPos = yPos + Y_INCREMENT;

} while (yPos < World.WorldMax.Y);

2. After the above loop finishes, xPos will be about 1/2 across the screen.  At that point, we then switch to drawing basketballs, and moving downward (by subtracting Y_INCREMENT) each time.

do

{

xPos = xPos + 1.0f;

yPos = yPos - Y_INCREMENT;

} while (xPos < World.WorldMax.X);
This loop continues until xPos has gone off the right edge of the screen, at which point it stops.

• Let's examine the Y_INCREMENT value in more detail.  Since the aspect ratio of the screen/window is 16:9 (meaning that the width of the screen is 16 units, and the the height of the screen is 9 units, for some unit of measure - maybe inches, maybe centimeters, maybe feet), if we want our line to appear to to have a slope of 1/1 (meaning that when we go over 1 unit, we move up 1 unit), then we'll need to scale the amount by which we move up to be less than 1 (when we move rightward 1 unit).  In this case, we'll need to divide the distance that we moved horizontally (1) by the width part of the aspect ratio (16), then multiply that number by the height part of the aspect ratio (9), in order to give us the proper vertical distance to move up.

FURTHER EXERCISES::

1. 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.
2. Examining the Do-While loop: Mirroring The Plot
For this exercise, you should use the same project that was explained in the above tutorial.
Building off what you've seen in the above tutorial, add two more loops, so that the following pattern is displayed on the screen:

• Examining the Do-While loop: Using Just One Loop
For this exercise, you should use the same project that was explained in the above tutorial.
Building off what you've seen in the above tutorial, combine the two loops in UpdateWorld into a single loop.
Hint: It may be helpful to think about how you could conditionally check whether to increment or decrement yPos by Y_INCREMENT within the single loop.
• Examining the Do-While loop: Implement Using A While Loop
For this exercise, you should use the same project that was explained in the above tutorial.
Building off what you've seen in the above tutorial, re-write the loops so that they still accomplish the exact same work, but are done using normal while loops.