XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.3.ModulesAndFunctions
Example: Ex_4.LocalVars

Functions and Local Variables


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, 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 it prints out the current location of the soccer ball at the bottom of the screen.  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.


2. Making Radius Proportional To Height

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

In this tutorial, the size of the soccer ball is dependent on the height of the ball - as the Y value of the soccer ball's center point gets bigger, so does the radius of the circle object that represents the ball.  Because we're no longer looking to randomize the radius, we want the soccer ball's radius to start off at a standard, set size:


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. Local Variables
    For this exercise, you should use the same project that was explained in the above tutorial.
    You'll notice that we declare yPos in the ComputeSoccerSize() method as a local variable, meaning that we're not allowed to use that variable anywhere else.  Try doing each of the following, and for each one, see what happens.  Does the program compile? Does it run?  Does it run correctly?
    1. Try printing out the value of yPos in the PrintMessageToTopStatus method
    2. Try printing out the value of yPos in the UpdateWorld function
    3. Try printing out the value of yPos in the GetANumber function
    4. Why do you get the results that you're seeing?
  3. Instance and Local Variables; Math Formula Review
    For this exercise, you should use the same project that was explained in the above tutorial.
    Instead of making the radius of the soccer ball depend exclusively on the height, change the code so that it also depends on the distance from the left edge, too.  A formula like
    NewRadius = BALL_INIT_RADIUS + (RADIUS_SCALE_FACTOR * yPos) + (RADIUS_SCALE_FACTOR * xPos);

    Might work well.


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