XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)

Topic: Topic.2.Input_Output_PrimitiveDataTypes
Example: Ex_10.IntFloatSubtraction

Math Operators: Subtraction (Integer and Floating-Point)



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 four rectangles on the screen, labeled with each rectangle's current height. 

At the bottom of the screen there 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 rectangle "B".  The height of the "Int" rectangle is automatically set to be the difference (subtraction) of the heights of the other two rectangles, when the those rectangles' heights are rounded down to whole numbers .  The height of the final, right-most rectangle, the "Float" rectangle, is the difference (subtraction) of the heights of rectangles A and B This subtraction is done as accurately as possible , as pictured below.  At the top of the screen, the heights of the A and B rectangles are displayed, both as the exact, floating-point height, as a rounded-down, integer height.

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. Exploring Errors: Type casting
    Using the same project that the above tutorial makes use of, try assigning a floating point number to an integer variable, but without using the typecasting .  For example:
        int bHeightInInt = bHeight;

    What error do you get?  Make sure to take careful note of this error, in case you accidentally make this mistake in the future, and you need to figure out what the error is, based on the error message(s).

  3. Understanding The Code
    Using the program that the above tutorial discusses, you can make the height of A (or B) negative.  When you do this, what happens?  Why?

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.