XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.3.ModulesAndFunctions
Example: Ex_10.MathLibrary

Math Library Functions



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 animates the movement of two soccer balls from left to right across the screen.  When the soccer balls go beyond the end of the screen, they "wrap around" and jump back to the left edge of the screen.  The top soccer ball traces out the path of a sine wave, while the bottom soccer ball traces out the path of a cosine wave.  (You may find it useful to search the web for terms like "sine wave", or "sine wave animation" if you'd like more information about these mathematical concepts)  There are only two ways to interact with the game (not counting the standard "Press the Back button to exit" way):

  1. By pushing the right thumbstick up or down, you can increase or decrease the amplitude of the waves, meaning that you can make the waves (vertically) bigger or smaller.

  2. By pushing the right thumbstick left or right, you can increase or decrease the period of the waves, meaning that you can make the waves (horizontally) longer or shorter.

2. Examining The Program:

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


  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. Trying out other mathematical functions
    For this exercise, you should use the same project that was explained in the above tutorial.
    Modify the provided solution so that your program demonstrates the use of several common mathematical functions, that are built into the core C# / .Net framework.  For each one, follow the pattern set forth in the tutorial's code, and set the Y value of the center of one of the circles equal to the output from each of the below functions, one at a time.  The goal for this exercise is to give you the opportunity to look up various math functions, and make use of them here, in a graphical manner.

Project home page: The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
Michael Panitz
Business And Information Technology
Cascadia Community College

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.