| XNA Game-Themed CS1 Examples (XGC1) | |
|
Release 2.0 (XNA V3.1) |
|
References:
Goals:
1. Obtain the example 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
|
protected
override
void
InitializeWorld() { World.SetWorldCoordinate(new Vector2(0,0), 100.0f); // Allocate memory for the array m_MyArray = new int[ARRAY_SIZE];for (int i = 0; i<ARRAY_SIZE; i++) m_MyArray[i] = 10; #region From previous example (create and allocate 5 soccers and one basketball // initialization for the soccer balls<code omitted, for clarity> |
m_MyArray =
new int[ARRAY_SIZE];The only difference being that the literal number 5 has been replaced by the named constant ARRAY_SIZE, which has the value 5.
m_MyArray[i] = 10;



|
String
arrayMsg = "Array Contents:"; for (int i= 0; i<ARRAY_SIZE; i++)
arrayMsg = arrayMsg +
" ["
+ i + "]="
+ m_MyArray[i]; EchoToBottomStatus(arrayMsg); |
| Variable Name | Value | ||||||||||||
| m_MyArray | an array
with these values:
|
||||||||||||
| arrayMsg | "Array Contents:" |
| Variable Name | Value | ||||||||||||
| m_MyArray | an array
with these values:
|
||||||||||||
| arrayMsg | "Array Contents:" | ||||||||||||
| i | 0 |
However, we know that i has the value 0, so this is really the same as:
arrayMsg = arrayMsg +
" ["
+ 0 + "]="
+ m_MyArray[0];
Checking the above table of variable values, we can see that arrayMsg
has the value
"Array Contents:",
and m_MyArray[0] stores the value
10, so we can mentally rewrite the above statement as
arrayMsg =
"Array Contents:" +
" ["
+ 0 + "]="
+ 10;
At this point, the concatenations proceed left to right, one at a time.
Thus, the statement will go through the following steps of evaluation
(remember that C# will actually translate the numbers into strings before
doing the concatenation:
arrayMsg = "Array Contents [" + 0 + "]=" + 10;
arrayMsg = "Array Contents [" + "0" + "]=" + 10;
arrayMsg = "Array Contents [0" + "]=" + 10;
arrayMsg =
"Array Contents
[0]="
+ 10;
arrayMsg =
"Array Contents
[0]="
+
"10";
arrayMsg = "Array Contents [0]=10";
At this point, the new string
"Array Contents
[0]=10" is assigned to arrayMsg, replacing the previous string.
| Variable Name | Value | ||||||||||||
| m_MyArray | an array
with these values:
|
||||||||||||
| arrayMsg | "Array Contents [0]=10" | ||||||||||||
| i | 1 |
However, we know that i has the value 1, so this is really the same as:
arrayMsg = arrayMsg +
" ["
+ 1 + "]="
+ m_MyArray[1];
Checking the above table of variable values, we can see that arrayMsg
has the value
"Array Contents
[0]=10", and m_MyArray[1]
stores the value 11, so we can mentally rewrite the above statement as
arrayMsg =
"Array Contents
[0]=10" +
" ["
+ 1 + "]="
+ 11;
At this point, the concatenations proceed left to right, one at a time.
Thus, the statement will go through the following steps of evaluation
(remember that C# will actually translate the numbers into strings before
doing the concatenation:
arrayMsg = "Array Contents [0]=10 [" + 1 + "]=" + 11;
arrayMsg = "Array Contents [0]=10 [" + "1" + "]=" + 11;
arrayMsg = "Array Contents [0]=10 [1" + "]=" + 11;
arrayMsg = "Array Contents [0]=10 [1]=" + 11;
arrayMsg = "Array Contents [0]=10 [1]=" + "11";
arrayMsg = "Array Contents [0]=10 [1]=11";
At this point, the new string
"Array Contents
[0]=10 [1]=11" is assigned to arrayMsg, replacing the previous
string.
| Variable Name | Value | ||||||||||||
| m_MyArray | an array
with these values:
|
||||||||||||
| arrayMsg | "Array Contents [0]=10 [1]=11" | ||||||||||||
| i | 2 |
FURTHER EXERCISES::
| Index | 0 | 1 | 2 | 3 | 4 |
| Value | 0 | 1 | 2 | 3 | 4 |
| Index | 0 | 1 | 2 | 3 | 4 |
| Value | 1 | 2 | 3 | 4 | 5 |
| Index | 0 | 1 | 2 | 3 | 4 |
| Value | 0 | 10 | 20 | 30 | 40 |
| Index | 0 | 1 | 2 | 3 | 4 |
| Value | 10 | 20 | 30 | 40 | 50 |