XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.6.Arrays
Example: Ex_4.PongSoccerWithArray

Arrays: Using Arrays (And Loops) To Keep Track of Multiple Objects



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:

For this tutorial, we're going to go back and revisit a program that we examined earlier - the 'Pong Soccer' game that we looked at throughout the Chapter on decision structures, culminating in the finished game in tutorial 4000.900 (Pong Soccer).  The game plays exactly the same as before, although the appearance of the blocks has changed slightly (they're now numbered, instead of being labeled with letters).

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. Adding Blocks
    For this exercise, you should use the same project that was explained in the above tutorial, and modify it so that there are 2 more blocks on the screen.  When doing this exercise, you should strive to make the fewest possible changes to the source code, while still producing a correct program. Once you've done that, add change the code that there are a total of 10 blocks - notice how little code needs to be changed to effect this change!
  3. Labeling Blocks With Letters Instead Of Numbers
    For this exercise, you should use the same project that was explained in the above tutorial, and modify it so that instead of labeling the blocks with nubmer (e.g., "Block 1"), instead the blocks are labeled with letters (e.g., "Block-A"). 
    Hint: Instead of creating a giant if...else if statement, how can you use an array containing the strings { "A", "B", "C", "D", "E" }to fix this problem?

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.