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 program for this tutorial is almost exactly identical
to the program used in the
drawing A Sine curve with a
While
Loop tutorial
.
The difference is that for this tutorial,
we're using a
for loop.
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
First, you'll notice that we create the
yPos
variable, and give
it an initial value of 0.0f. While this isn't strictly necessary (we
will assign it the value of sin(x) before using it), it's good to see that
there are different styles of writing equivalent, correct code. Some
people prefer to initialize all of their variables, so we're showing you
this example code in order to show you that style of coding.
Let's briefly examine the details of how the
for loop.
First, we can see that the for loop is a normal, counting for loop:
for
(
float
xPos = 0.0f; xPos < World.WorldMax.X; xPos += 1)
You'll notice that we choose to declare
xPos
as a float
(instead of an integer), mainly to show you that one can declare any type of
variable you want in the initialization step of the for loop. You'll
also notice that we increment
xPos
using the counting expression "
xPos
+= 1
", which is equivalent to "
xPos
= xPos + 1
" , both of which are equivalent to "
xPos++
".
Again, the goal here is to show you that there are multiple, correct ways of
writing this loop.
Second, you can see that the body of the loop should all be familiar to
you - using the functions
ComputeRadianFromXPos
and
ComputeSineYPos
, the Y value of the center of the new soccer ball is
calculated, just like in the previous tutorial(s).
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 Loops: Modifying the program
For this exercise, you should use the same project that was explained in the
above tutorial.
Go back to the
previous
tutorial about animating sinusoidal motion,
and run that program. Observe how the program moves the
small basketball along the sine wave. You should implement identical
functionality here in this program, and you should do that from memory as
much as possible. To be clear: do not refer to the source code in that
previous tutorial unless you absolutely have to - the more of this exercise
that you can and figure out (and do) on your own, the more you'll learn from
it.