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
Declaring the instance variables and named constants, and initialize
them
For this tutorial, we declared (and initialize) both the m_BlockHits
array (whose elements all start with the value zero, so we don't need to do
anything other than declaring, then creating the array), and the m_Blocks
array (whose elements need to be assigned XNACS1Rectangle objects, as you've
seen previously)
If you look closely, you'll notice that the labels when we create are
actually spread over two lines: the first line includes the block
number, and the second line displays the number of "hits":
We do this using the
'newline' character,
which we can do using the special escape sequence \n.
The backslash means that the next character is special, and the n means that
this special character should be a newline. Here's the code that does
this:
m_Blocks[i].Label
= "Block-" + i +
"\nHit="
+ m_BlockHits[i];
UpdateWorld():
String foundString = "no collision!";
//
now try to find the colliding block
bool
found = false;
int
i = 0;
while
((!found) && (i < ARRAY_SIZE))
{
if
(m_Blocks[i].Collided(m_SoccerBall))
{
//
found!
found =
true;
// *** New
line: increment the number of hits in
// *** the
m_BlockHits[] array, representing number of
//
*** collisions for the corresponding block
m_BlockHits[i]++;
//
Update the block-label
m_Blocks[i].Label =
"Block-"
+ i + "\nHit="
+ m_BlockHits[i];
foundString =
"Collided
with Block-"
+ i;
}
else
i = i + 1;
}
EchoToBottomStatus(foundString);
UpdateWorld starts by handling the now-standard Back button and right
thumbstick, and then goes on to call the
DisplayNumbersToSearchAtTop method, which uses the same pattern
you've seen in previous tutorials to build up a string (containing all the
numbers in the m_NumbersToSearchThrough
array), and then display that message at the top of the screen.
Let's briefly examine how this loop works, and how the parallel arrays
are used, by stepping through the next call to the UpdateWorld function,
assuming that the game is set up exactly like the picture at the top of this
page.
The first time through the loop, i has the value 0, and we check if
if
(m_Blocks[i].Collided(m_SoccerBall)) is true. Since
the soccer ball isn't overlapping the very first block, it's false, and we
skip the stuff in curly braces. Pictorially, we're examining the
very first element of the m_Blocks array, like so:
The next time through the loop, i has the value 1, so check for a
collision against the block referred to by element 1 of the m_Blocks
array. Since the soccer ball does overlap with the second block, we
will execute the body of the loop. Pictorially, what we're doing is:
Because block #1 overlaps with the soccer ball, we then proceed to
increment the hits counter in the parallel array, from 60 to 61. We
know that we should increment element #1, because block #1 overlapped with
the soccer ball, and element #1 of both arrays store data about the same
on-screen block. Here's a picture:
Similar to what you've seen previously, the loop will continue to
iterate through all the slots the in parallel arrays, checking to see if
the soccer ball overlaps any of the other rectangles (incrementing the
counts as appropriate).
Once all that is done, the EchoToBottomStatus command prints out the
appropriate message on the screen.
FURTHER EXERCISES:
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.
Repeat this exercise daily for several days, so that you really get the
hang of this. As you go on, periodically review this by re-doing this
exercise.
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?
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.