XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.6.Arrays
Example: Ex_2.LoopThroughArray

Arrays: Using Loops To Traverse Arrays


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:


As you can see, this program draws five soccer balls across the screen.  The program also draws a basketball on the screen, and allows you to move the basketball using the right thumbstick.  Finally, the program continues to draw the contents of the array at the bottom of the screen.

While this program behaves almost identically to the prior program, we've made a major change in how our code accesses the elements of the array: instead of hard-coding access to the five individual slots, this program uses a single loop to traverse the entire array.  In this context, when we say "traverse", we mean "go through all the elements of the array, and access any of the elements that we want to"


2. Examining The Program:

Let's examine the C# source code that produces the behavior we see on-screen


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. Changing What The Buttons Do
    For this exercise, you should use the same project that was explained in the above tutorial.  Modify the above program so that the button accomplish different things than they currently do.  Some ideas:
    1. When the user presses the A button, increment ALL the elements of the array.  You may want to start by using five separate statements to do this, and once you've got that working, then try doing this using a loop.
    2. When the user presses the B button, decrement all the elements of the array.  You may want to start by using five separate statements to do this, and once you've got that working, then try doing this using a loop.
    3. When the user presses the X button, randomly add a number to each element in the array.  For this exercise, randomly generate a single number, and add that to all the elements.  Example: Your program randomly generates 5, which you then add to all elements of the array.
    4. When the user presses the Y button, randomly add a number to each element in the array.  For this exercise, randomly generate a number for each element of the array.  Example: Your program randomly generates 5 (which is adds to the element #0), then randomly generates 2 (which is adds to the element #1), then randomly generates 15 (which is adds to the element #2), then randomly generates 5 (which is adds to the element #3), then randomly generates 52 (which is adds to the element #4).
  3. Changing The Size Of The Array
    For this exercise, you should use the same project that was explained in the above tutorial.
    Modify the provided program so that the array is three elements long, instead of five.  Once you've got that working, modify the program again so that the array is 10 elements long (don't worry if the message displayed at the bottom of the window goes off the screen)
  4. Initializing the Array With Different Values
    For this exercise, you should use the same project that was explained in the above tutorial.
    Modify the provided solution so that your program does the following:
    1. Instead of initializing the array elements to all store the value 10, have element #0 store the value 0, element #1 store the value 1, and so on.  Here's a table of values showing what the array should look like when InitializeWorld is finished:
      Index 0 1 2 3 4
      Value 0 1 2 3 4
    2. Instead of initializing the array elements to all store the value 10, have element #0 store the value 1, element #1 store the value 2, and so on.  Here's a table of values showing what the array should look like when InitializeWorld is finished:
      Index 0 1 2 3 4
      Value 1 2 3 4 5
    3. Instead of initializing the array elements to all store the value 10, have element #0 store the value 0, element #1 store the value 10, and so on.  Here's a table of values showing what the array should look like when InitializeWorld is finished:
      Index 0 1 2 3 4
      Value 0 10 20 30 40
    4. Instead of initializing the array elements to all store the value 10, have element #0 store the value 10, element #1 store the value 20, and so on.  Here's a table of values showing what the array should look like when InitializeWorld is finished:
      Index 0 1 2 3 4
      Value 10 20 30 40 50

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