XNA Game-Themed CS1 Examples ( XGC1 )

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

Topic: Topic.2.Input_Output_PrimitiveDataTypes
Example: Ex_11.PercentAndMultiplication

Math Operators: Percent and Multiplication


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.

Once we compile and run this project, the program displays three rectangles on the screen, each labeled with each rectangle's current height (and how that height was calculated)  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").

The middle rectangle's height is set to be 20% of rectangle A.  The right-most rectangle's height is set to be equal to rectangle A's height, minus 20% of A's height.  All numbers are calculated using floating-point numbers (instead of integer numbers).

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. Changing the Percentage
    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.
    You should modify the project so that the middle rectangle is 50% of the height of the left one.
    Once you've got that done, keep the middle rectangle at 50% height, and then modify the right-most one so that it's 70% less than the height of the left one (i.e., so that the formula you'll use will be (height of left) - (70% of height of left)
  3. Changing the Percentage: Greater than 100%
    Start this exercise using Exercise_ 3 's starter project , which is a nearly identical copy of the project that was used in the above tutorial.
    You should modify the project so that the middle rectangle is 125% of the height of the left one. 
    Keep the formula that you're using to set the right-most rectangle as (height of left) - (70% of height of left)
  4. Scaling Input
    Start this exercise using Exercise_ 4 's starter project , which is a nearly identical copy of the project that was used in the PRIOR tutorial (the one about integer and floating point substraction)
    For this exercise, we want to scale the input that we're getting from the thumbsticks.  This means that if the user is pushing the thumbstick enough so that we get the value +0.75, we want to multiply that number by another number, say, 0.1f, in order to scale down the input to 0.075.  That way, the number we use is 10% of the value of the 'raw' input value.
    The reason for doing this (in addition to demonstrating how to scale a number) is to slow down the rate at which the rectangles change their height.  You'll notice, now that things are moving much slower, that the integer subtraction is much easier to see.  If you hold the left thumbstick up, you can see the floating point subtraction rectangle changing it's height very smoothly, while the integer division rectangle's height 'pops' as it crosses the round-off threshold, and changes by an entire, whole, integer value.

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