XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)

Topic: Topic.6.Arrays
Example: Ex_7.ParallelArray

Parallel Arrays



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:

In this tutorial, you can control the location of the soccer ball using the right thumbstick. As you move the soccer ball around the screen, the game tells you if the soccer ball is currently not overlapping anything on the screen (which it does using the message "No Collision!" at the bottom of the screen), or if the ball is overlapping a rectangle, which rectangle is current being overlapped (with a message like "Collided with Block-8", at the bottom of the screen). 

For each rectangle, a count of 'hits' is kept.  While the soccer ball is overlapping a rectangle, the 'hit count' is incremented by one each time UpdateWorld is called. 

The major topic for this tutorial is that of 'parallel arrays' - two, separate arrays that both contain data about a related subject, set up in such a way that element #0 in the first array corresponds to element #0 in the second array.  For example, the program for this tutorial maintains two arrays: an array of integers named m_BlockHits, which contains the 'hits counter' for each of the blocks, and an array of rectangles named m_Blocks (these are the blocks).  In the above picture, m_BlockHits[1] is 60, and m_Blocks, which refers to the XNACS1Rectangle object that the soccer ball is currently overlapping.  Keep in mind that there is NO connection between these two elements, except for the code that the programmer has written.  In other words, the C# language is unaware of the connection, and there is no syntax that you can use to tell C# about the conceptual connection.  If we wanted to represent this with a picture, we might use something like:

You'll notice the bright red, vertical arrows that "link" the two arrays.  These arrows are purely in the mind of the programmer, and do not exist anywhere within the computer (unlike the black arrows which connect elements of the m_Blocks array to their respective XNACS1Rectangle objects).  You'll notice that element #1 in the m_BlockHits array, which stores the value 60, corresponds to the memory reference for the second XNACS1Rectangle, in the m_Blocks array.

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. Examining The Program
    For this exercise, you should use the same project that was explained in the above tutorial.
    When the soccer ball is placed onscreen in a way that overlaps with a block, the "hit count" keeps increasing (even though the soccer ball isn't moving).  Why does this happen?
  3. Listing All Matches
    For this exercise, you should use the same project that was explained in the above tutorial.
    Modify the program so that the program will tell you if the soccer ball has ever overlapped with each of the blocks.  To do this, try creating an array of bools, where each slot in the array stores either true or false.  Make sure that all the elements of the array start out with the value false, and when the soccer ball overlaps with a block, set it's corresponding bool (in the array of bools) to be true.  Display the contents of the array at the top of the screen.

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.