XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.4.DecisionStructures
Example: Ex_5.StringEquality

Decision Statements: Nested If Statements



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:

The game behaves in a manner that is almost identical to the what was described for the previous tutorial, with one new feature:

  1. When the game goes from goes from Novice mode to Expert mode, the game will play a sound, and increase the speed of the ball.

  2. Internally (i.e., in a way that the player won't notice), the game will change how it keeps track of whether it's in Novice or Expert mode using an instance variable.

Let's examine the source code, feature by feature.

2. Keeping Track of The Current Mode:

Let's examine the C# source code that produces the behavior we see on-screen.  Since the code is nearly identical to the program that was presented in the previous tutorial, we'll leave out everything, except for code that has changed, or code that is new. 

Starting with this tutorial (and increasing, as we move onwards), our program will need to know what mode the game is currently in.  So instead of repeatedly asking if (m_NumBounces > EXPERT_LEVEL_BOUNCES), we would instead like to calculate this just once in each UpdateWorld, and store that result into a variable.  That way, we can quickly check that variable, and not have to worry about accidentally miscalculating on subsequent times (for example, accidentally typing if (m_NumBounces < EXPERT_LEVEL_BOUNCES), which compiles, but doesn't do what we want.).

For this tutorial, we will store the mode into an instance variable that is a string.  In computer programming, string is the term used to indicate text.  In a nutshell, we will store the string "Novice" into the variable if the game is in Novice mode (which is how the game starts), and then store the string "Expert" into that variable once the player has bounced the ball enough.

3. Increasing The Speed Of The Ball In Expert Mode

When the player advances to Expert mode, we want the game to change.  Since the player has done well at the Novice level, we'd like to increase the difficulty of the game, in order to make sure that the game remains challenging and engaging to the player.  We will do this by speeding up the ball when the game switches from Novice to Expert mode.  We will do this by adjusting the starting speed of the ball to be slightly slower, and define a couple of new constants to define how much we will boost the ball's speed by.


  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 referring the tutorial's code.
  2. Practicing Nested If's
    For this exercise, you should use the same project that was explained in the above tutorial.
    Let's say that we want to add another mode to the game - another skill level.  Let's say that after the player has bounced the ball twice as many times as EXPERT_LEVEL_BOUNCES, we want to again play the 'Victory' sound, change the message at the bottom of the screen to "Super Expert", and again increase the speed (by the same amount as the speed was increased when the game changed from Novice to Expert).  You should go through the code, and make all the changes necessary to add this feature.

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.