XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.3.ModulesAndFunctions
Example: Ex_7.FunctionalParameters

Passing Data To Functions Using Parameters


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 are two soccer balls on the screen.  While you can simultaneously control both soccer balls' movement using the right thumbstick, you can clearly see that they are following the path of a parabola across the screen, in a simple simulation of gravity's pull on the soccer balls.  The message at the top of the screen has been updated, as well.


2. Making Two Balls Follow A Parabolic Arc, Without Doubling The Work We Need To Do.

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. 

The goal in this tutorial is to take the code that we used for the prior tutorial, and modify it so that we can easily add the second soccer ball without having to copy-and-paste all the work that we've done so far.


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. For this exercise, you should use the same project that was explained in the above tutorial.
    In the UpdateWorld function, try commenting out one of the "super steps", so that one of the soccer balls doesn't get updated.  Does the other one still get updated?  Why?  Once you've done that, uncomment that super-step, and comment out the other one.  Which soccer ball is getting updated now?  Why?

  3. For this exercise, you should use the same project that was explained in the above tutorial.
    You'll notice that in UpdateWorld, the same three methods are called on m_TheSoccer, then called again on m_SecondSoccer.  For this exercise, you should add a new function, named UpdateSoccerBall, which has a single parameter: an XNACS1Circle object (feel free to name the parameter something like aSoccer, so that the function similar to the ones you've defined above).  UpdateSoccerBall will then call MoveSoccerByThumbStick, ComputeSoccerSize, and ComputeSoccerPosition on the soccer ball that was passed in as a parameter.  Finally, have UpdateWorld call UpdateSoccerBall on m_TheSoccer, than have UpdateWorld call UpdateSoccerBall again on m_SecondSoccer

  4. For this exercise, you should use the same project that was explained in the above tutorial.
    You should do what you need to, in order to add a third soccer ball to the program.  Make sure that this third soccer ball starts out slightly higher than the first two, so that you can see it when the program runs.

  5. For this exercise, you should use the same project that was explained in the above tutorial.
    You should do what you need to, in order to add a fourth and fifth soccer ball to the program.  Make sure that when the program runs, you can see all five soccer balls clearly.


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