XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)

Topic: Topic.2.Input_Output_PrimitiveDataTypes
Example: Ex_9.AdditionLocal

Math Operators: Addition (using Local Variables)



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.

Once we compile and run this project, the program displays three rectangles in the screen, labeled with each rectangle's current height.  At the bottom of the screen are instructions for playing game: by using the left thumbstick, you can control the current height of the left-most rectangle (rectangle "A"), by using the right thumbstick, you can control the current height of the middle rectangle (rectangle "B").  The height of the right-most rectangle is automatically set to be the sum of the heights of the other two rectangles, as pictured below.

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


  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. Understanding The Code
    Using the program that the above tutorial discusses, can you make the height of A (or B) negative?  When you do this, what happens?  Why?
  3. Program Tracing:
    Trace out the execution of the float addHeight = aHeight + bHeight; line, similar to how the tutorial does this, above. 
  4. Understanding The Program
    What happens if you leave out the lines that update the labels?  You should first think about what you believe will happen, then run the program to confirm that your predictions were correct.
  5. Thinking about usability
    Now that you've had a chance to write several C# programs, you should stop and think about what makes programs useable (what makes programs easy to use).  Using the project from the tutorial above as a starting point, try doing each of the following things.  For each one, think about how the change affects the program - does it improve the user's experience?  Does it make it easier or harder to use the program?  Why?
    1. What if we pick a starting location that puts a rectangle off-screen (either above, below, or off to one side of what we can see in the window)? 
    2. What if we set things up so that when the program starts, the sum of the heights of A + B is higher than the top of the screen? 
    3. What if the rectangles are so high that the labels are off-screen?

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.