XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.4.DecisionStructures
Example: Ex_9.CaseStatement

Decision Statements: Case Statements


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.

When the game starts, you'll see a screen that looks similar to this:


The game behaves in a manner that is identical to the what was described for the previous tutorial, with a couple of new features:

  1. The code has been re-factored, so that CheckForWorldBound now uses a case statement.
  2. When the player misses the ball, and the ball hits the left or right side of the screen, then the ball is moved back to the ball's original starting point, given a new, random velocity, and the player gets to try bouncing the ball again.
  3. The game now tracks the number of times that the ball has bounced off a paddle, since the last time the player missed the ball and the ball bounced off the left or right wall.  The game now also tracks the largest number of times that the ball's been bounced off of the paddles without being missed.  This is displayed to the user as "MaxBounces:" at the bottom of the screen.  Further, moving from Novice to Expert mode now depends on this maximum number of bounces non-missed bounces, rather than the current number of bounces.

Let's examine the source code, feature by feature


2. Refactoring CheckForWorldBound To Use A Case Statement:

3. Putting The Ball Back In The Middle When The Player Misses



4. Tracking the Maximum Number Of Bounces

Let's examine this new distinction between 'current' and 'maximum' number of bounces.  In a nutshell, the instance variable m_NumBounces tracks the number of times that the ball has bounced off either paddle since the last time the ball was missed.  m_MaxBounces tracks the highest number of bounces in row that the player has ever reached.  m_MaxBounces is used to determine if the game is in Novice or Expert mode, and m_NumBounces is used to determine if m_MaxBounces has increased.


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. Case Statements (C# switch statements)

    For this exercise, you should use the same project that was explained in the above tutorial.
    Examine the BounceOffBlocks routine.  First, without trying to compile such a solution, answer the following question: Does it make sense to refactor this code using a Case structure (i.e., using a C# switch statement?)?  Why, or why not?

  3. Case Statements (C# switch statements)

    For this exercise, you should use the same project that was explained in the above tutorial.
    Let's say that you want to add a couple of levels to the game, so that instead of just "Novice", and "Expert", there are the following levels:


    Number of Uninterrupted Bounces Level Name
    0 - 5 Novice
    6 - 10 Expert
    11-15 Super Expert
    16 - 20 Ultra Super Expert
    21+ Mega Ultra Super Expert


    Refactor the code in the UpdateWorld method so that as the player reaches the appropriate number of (uninterrupted) paddle bounces, each of the above levels are displayed in bottom status bar, using a Case statement.



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