Game Development of .Nettrix: GDI+ and Collision Detection - The CheckLines Method
(Page 19 of 22 )
In the next GameField method, CheckLines, you need to check if a line is totally filled (all bits set) and, if so, erase this line and move down all the lines above it. You don’t need to copy the empty lines (all bits reset) one on top of another, but you must return the number of cleared lines. To improve the readability of your code, you define some private constants for the class.
private const int bitEmpty = 0x0; //00000000 0000000
private const int bitFull = 0xFFFF; //11111111 1111111
See the comments in the code and the following explanation to understand the function.
public static int CheckLines() {
int CheckLines_result = 0; // Returns the number of lines completed.
int y = Height - 1;
while ( y >= 0) {
// Stops the loop when the blank lines are reached.
if (arrBitGameField[y]==bitEmpty) y = 0;
// If all the bits of the line are set, then increment the
// counter to clear the line and move all above lines down.
if (arrBitGameField[y]==bitFull) {
// Same as: if ((arrBitGameField(y) ^ bitFull) = 0
CheckLines_result++;
// Move all next lines down.
for(int index = y; index >= 0; index--) {
// If the current line is NOT the first of the game field,
// copy the line above.
if (index>0) {
// Copy the bits from the line above.
arrBitGameField[index] = arrBitGameField[index-1];
// Copy each of the squares from the line above.
for(int x=0; x<Width; x++) {
// Copy the square.
arrGameField[x, index] = arrGameField[x, index-1];
// Update the Location property of the square.
if (arrGameField[x, index] != null)
arrGameField[x, index].location =
new Point(arrGameField[x, index].location.X,
arrGameField[x, index].location.Y+SquareSize);
}
}
else {
// If the current line is the first of the game field
// just clear the line.
arrBitGameField[index] = bitEmpty;
for(int x=0; x<Width; x++) {
arrGameField[x, index] = null;
}
}
}
}
else {
y--;
}
}
return CheckLines_result;
}
In the CheckLines method, you can see the real benefits of creating arrBitGameField for collision detection: You can check if a line is completely filled or empty with only one test, with the use of bitFull and bitEmpty constants you previously created, avoiding the 16 tests you would have had to create for each of the ArrGameField members in a line. The next code listing highlights these tests:
if (arrBitGameField[y]==bitFull) //The line is full.
if (arrBitGameField[y]==bitEmpty) //The line is empty.
The next section discusses the last two methods for the GameField class.
The StopSquare and Redraw Methods
The last two methods, StopSquare (which sets the arrays when a block stops falling) and Redraw (which redraws the entire game field), have no surprises. The code implementing these methods is shown in the next listing:
public static void StopSquare(Square Square, int x, int y) {
arrBitGameField[y] = arrBitGameField[y] | (1<<x);
arrGameField[x, y] = Square;
}
public static void Redraw() {
for(int y=Height-1; y>=0; y--)
if (arrBitGameField[y]!=bitEmpty)
for(int x=Width-1; x>=0; x--)
if (arrGameField[x, y] != null) arrGameField[x, y].Show(WinHandle);
}
The next section shows the code for the final version of the main program, finishing your game code.
This chapter&, by David Weller, et al., (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today.
Buy this book now. |
Next: The Game Engine >>
More .NET Articles
More By Apress Publishing