XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)
2/8/2010

Topic: Topic.5.RepetitionStructures
Example: Ex_3.ExampleWithWhile

Repetition: Drawing A Sine Curve With A While Loop


References:

Goals:



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:


As you can see, this program draws a 'wave' of small soccer balls across the screen, plus a larger basket. You can move the basketball using the right thumbstick, and you change the amplitude (height) of the wave using the left thumbstick's Y part, and the frequency (how many waves on the screen at once) using the left thumbstick's X part.

We will get the wave using the mathematical sine function, which can be graphed like so:

Notice how the soccer balls in the screenshot follow the same pattern as the blue wave line in the above picture.  Essentially, if you plug lots of values (between 0 and 6.28318...  ( 2 * π, really) into the sine function, and then graph the y values that you get out, you'll get the above picture.  If you've taken trigonometry, then this is all review.  If you haven't yet taken trigonometry, then the key things to remember are that the 'amplitude' is the height of the wave, and that the 'frequency' is the number of times the wave is drawn in a given distance, more-or-less.  A fuller explanation of the sine function is beyond the scope of this tutorial.


2. Examining The Program:

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


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.
  2. Familiarizing Yourself With Loops: Identifying Errors
    Given the following code, what is wrong with the loop?  Without running the program, what will happen when the program is executed?  How would you fix it?


        float
    xPos = 0.0f, yPos;

       

        // here is the while loop

        while (xPos > World.WorldMax.X)

        {

            float radian = (xPos / World.WorldMax.X) * (2.0f * ((float)Math.PI)) * m_Frequency;

            yPos = BALL_INIT_Y + (m_Amplitude * ((float)Math.Sin(radian)));

            CreateABallAt(xPos, yPos, "SoccerBall");

            xPos = xPos + 1;

        }


  3. Familiarizing Yourself With Loops: Graphing Cosine
    For this exercise, you should use the same project that was explained in the above tutorial.
    Instead of graphing the sine function, try graphing the cosine function, instead.
  4. Familiarizing Yourself With Loops: Sequences
    For this exercise, you should use the same project that was explained in the above tutorial.
    Try moving the line "m_BasketBall.AddToDrawSet();" to before the loop that plots the sine wave.  What effect does this have on your program?

Project home page: The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu
Michael Panitz
Business And Information Technology
Cascadia Community College
mpanitz@cascadia.eduu

Microsoft Logo This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.
2/8/2010