Game Development of .Nettrix: GDI+ and Collision Detection - The Block Class
(Page 13 of 22 )
Second Draft: Coding the Block Class You can map the Block class, defined in the class diagram created for your game project, to the final class interface, including the data types for the properties and parameters for the methods. The proposed class interface is shown in the next code listing:
public class Block {
// The four squares that compose a block
public Square square1;
public Square square2;
public Square square3;
public Square square4;
private const int squareSize = GameField.SquareSize;
public Block(Point location, BlockTypes newBlockType)
{
}
public bool Down() {
}
public bool Right() {
}
public bool Left() {
}
public void Rotate() {
}
public void Show(System.IntPtr WinHandle) {
}
public void Hide(System.IntPtr WinHandle) {
}
}
In the game proposal, we said that the blocks will be composed of four squares (in every possible arrangement). You can start the coding by thinking about the possible combinations, and give each of them a name, as shown in Figure 1-24.

Figure 1-24. The square arrangements to form each block
Because each block will have a specific square combination, you can think of three new elements for your class: a BlockType property, an enumeration for the block types, and a constructor that creates the squares in the desired positions and the color of each square. To give a visual clue to the player, the colors must be fixed for each block type, so its a good idea to create arrays to hold the forecolor and backcolor for each type. The extra definitions for the class are shown in the next code listing:
public enum BlockTypes {
Undefined = 0,
Square = 1,
Line = 2,
J = 3,
L = 4,
T = 5,
Z = 6,
S = 7
};
public BlockTypes BlockType;
// The colors of each block type
private Color[] backColors = {Color.Empty, Color.Red,
Color.Blue, Color.Red, Color.Yellow, Color.Green,
Color.White, Color.Black};
private Color[] foreColors = {Color.Empty, Color.Purple,
Color.LightBlue, Color.Yellow,
Color.Red, Color.LightGreen,Color.Black, Color.White};
Next: The Constructor >>
More .NET Articles
More By Apress Publishing