XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.3.ModulesAndFunctions
Example: Ex_5.DescribeAlgorithm

Functions As A Way To Describe An Algorithm



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, there is a single soccer ball on the screen.  Using the right thumbstick, you can move the soccer ball around (if you want, you can even move it off the screen).  The program prints out a simple, purely textual message at the top of the screen, and the current location of the soccer ball at the bottom of the screen (along with the ball's radius).  As you move the ball around on the screen, you'll notice that the size of the ball changes in proportion to the height of the ball - as you move the ball upwards, it gets bigger, and as you move it downwards, it gets smaller.  You'll also notice that the ball gradually sinks towards the bottom of the screen.

2. Clarifying The Algorithm Using Functions.

Since the source code to this program is nearly identical to the program used in the previous tutorial, we will only examine the code that's new, or different. 

The term "algorithm" is basically a fancy word for "a series of steps that you do in order to achieve some goal".  One could say that there's an algorithm for driving from your house to the grocery store (which might look something like: (1) go north on the street you live for 5 blocks, (2) take a left onto Grocery St, (3) go 3 blocks on Grocery St, (4) turn right into the grocery store's parking lot), that there's an algorithm for assembling a peanut butter and jelly sandwich, etc, etc.  In some ways, "algorithm" is very similar to "recipe", except that "algorithm" is usually used to describe a series of steps that are specific to calculating or computing something.  Let's look at how functions can help clarify an algorithm by examining the UpdateWorld routine:


  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. Adding Steps To Your Algorithm
    For this exercise, you should use the same project that was explained in the above tutorial.
    Let's say that you want to add a rectangle to the screen, which will move up and down along the vertical line X = 10.  The Y value of the center of the rectangle will always be exactly the same as the Y value of the soccer ball. 

    1. Let's say that you've created a new function, named AdjustRectangleLocation, which takes the current height of the circle's center, and moves the rectangle so it's center is at the same height.  In UpdateWorld, after which step should you call AdjustRectangleLocation?  Why?
    2. Let's say that you want the rectangle's width to change, so that it's equal to the circle's radius.  Let's say that you've created a new function, named AdjustRectangleWidth, to do exactly that.  In UpdateWorld, after which step should you call AdjustRectangleWidth?  Why?
    3. Modify the source code provided in this tutorial so that you can actually implement the two exercises above, in C# source code.

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.