XNA Game-Themed CS1 Examples ( XGC1 )

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

Topic: Topic.2.Input_Output_PrimitiveDataTypes
Example: Ex_14.Constants

Math Operators: Using Constant Values in C#


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.

This program draws two rectangles on the screen, as well as a circle.  The circle is labeled with it's area.

At the bottom of the screen are instructions for playing game.  By using the left thumbstick's vertical axis (the Y axis), you can control both the heights of the rectangles, as well as the radius of the circle.  Pushing the left thumbstick up (or forward) increases the heights & radius, pushing downward decreases them.  You'll notice that pushing the thumbstick doesn't change things very quickly; this is done on purpose. 

Additionally, when player presses the 'Back' button (or the keyboard equivalent), the program will exit.



2. Examining The Program:

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


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. Named Constants: Easy, Consistent Update
    Using the same project that the above tutorial makes use of, try changing the declaration of  REC_WIDTH so that it's value is 2.0f.  Notice that all the rectangles on the screen change automatically (the next time you compile and run the program).  Try changing it to 40.0f, and notice what changes. 
  3. Named Constants: Readable Code
    Using the same project that the above tutorial makes use of change the value of RATE_SCALE, and try rerunning the program.  Note that not only do you get a quick, easy, and consistent update, but it's very clear exactly what you're multiplying the left thumbstick by.  You're multiplying the left thumbstick by a value intended to scale the rate at which the rectangle (and circle) change.
  4. Named Constants: Finding and replacing numbers with named constants

    Start this exercise using Exercise_ 2 's starter project , which is a nearly identical copy of the project that was used in the above tutorial.
    Examine the code used in InitializeWorld.  Notice that the height of all the rectangles is 10.0f.  Notice also that this height changes as soon as the user pushes the thumbstick.  For this exercise, in the InitializeWorld method, replace the rectangle's initial heights of 10.0f with a new, named constant called something like REC_STARTING_HEIGHT.

  5. Named Constants: Finding and replacing numbers with named constants appropriately

    Continue working from the same project that you used in the prior exercise.  Notice that in the InitializeWorld method, the starting heights of both rectangles is 10.0f, as defined by the named constant REC_STARTING_HEIGHT.  Notice that the starting radius for the circle is also 10.0f.

    For this exercise, in a comment immediately above the line that assigns a starting value to the circle's radius, answer the following question:
    Should you replace the circle's starting radius with the named constant REC_STARTING_HEIGHT (since the starting radius is also 10.0f)?  What would the advantages of this be, and what would the disadvantages be?

  6. Named Constants: Finding and replacing numbers with named constants appropriately

    Continue working from the same project that you used in the prior exercise. 
    Go through the code, and see if there are other numbers that you can replace with named constants.  For each number, put in a quick (1-2 sentence) comment explaining why (or why not) you think that a named constant is a good thing to use.


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