XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.4.DecisionStructures
Example: Ex_7.MultipleNested

Decision Statements: More "If / Else", and Tracking Missed Balls



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 two changes:

  1. The game now tracks (and displays) how many times the player missed the ball (and as a result, the ball bounced off the left or right side of the screen).

  2. The program now makes use of nested conditional statements to detect whether the ball needs to be bounced off the edge of a screen. As part of this change, we will also play a sound when the ball bounces off an edge of the screen.

Let's examine the source code, change by change

2. Ball Bouncing Off The Screen Edges With A Nested Conditional Statement:

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.  Further, we'll move through the changes feature by feature, staring with the feature that in this version of the game, the ball will now bounce off all the edges of the screen using a nested conditional statement. 

While functionally equivalent to what we had previous done, this version will be slightly more efficient, and will also make it easier for other programmers to understand what our intent was, when we wrote this code

3. Tracking (And Displaying) Missed Balls

Eventually, the game will be set up so that when a player fails to bounce the ball off the paddle, and the ball hits the left/right wall behind the paddle, the ball will be placed back at it's starting position with a new, random velocity, and the count of times that the player has missed a ball will be incremented.  For right now, though, we want to slowly, incrementally work towards this overall goal, by implementing just part of that.  Specifically, we will implement the part that keeps track of the number of times that the player has missed a ball, and display that number to the player.  Since the ball is not allowed to leave the screen, 'missing' the ball will mean that the ball has bounced off the left (or right) edge of the screen.  Overall, the approach we'll use to implement this feature is very similar to what we did to keep track of the number of bounces: we'll declare and initialize an instance variable to store the current number of misses, we'll increment that counter whenever the player misses a ball, and we'll display that number at the bottom of the 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. Nested Conditional Statements

    For this exercise, you should use the same project that was explained in the above tutorial.
    See if you can change the program so that when the player has bounced the ball off of the paddles 10 times, the message at the top of the screen is changed to "You've Won!".  Next, see if you can set things up (using a nested if statement), so that you only display that message if the ball has bounced at least 10 times, and the number of times the ball was missed is LESS than 5 times.

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.