XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.3.ModulesAndFunctions
Example: Ex_2.FunctionWithReturnValue

Calling A Function That Produces A Return Value



Need (library reference):

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 the current location of the soccer ball at the bottom of the screen.  You may also notice that the size of the ball is continuously, randomly changing.


2. Examining The Program:Using a function that is built into the CS1XNALib

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. 

Let's start by examining how to use a pre-existing function, that returns a value.  
Our goal here is to randomly change the radius of the soccer ball with each update of the game.  We will call a pre-existing function, that will produce the random number, and we'll use that to set the radius of the circle.


2. Examining The Program:Creating Our Own, New Function

Let's move on to examining how to use a function that returns a value, that we've created ourselves.


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. Using GetANumber To Set The Soccer Ball's Radius
    For this exercise, you should use the same project that was explained in the above tutorial.
    See if you can build on what was shown to you above, and randomly set the radius to be an integer value between 8.0f, and 17.0f.  It should be possible for your code to set the radius to 8.0f, or to 17.0f.
  3. Using GetANumber To Update The Top Status Bar
    For this exercise, you should use the same project that was explained in the above tutorial.
    See if you can build on what was shown to you above, and modify the PrintMessageToTopStatus so that it prints out a randomly selected integer, using the GetANumber function.

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