Game Development of .Nettrix: GDI+ and Collision Detection - The Constructor
(Page 14 of 22 )
The constructor will receive two parameters: the block type and the location where the block will be created. Since you need random block types, you can pass an Undefined value for the block type when you want to randomly create a block.
You might wonder why you allow anything other than Undefined for the block type in the first place, since during gameplay the blocks are randomly generated. The reason is that it makes testing far easier—you can test specific block types as you build up your game, giving you more control over incrementally testing the game. The code to do this is shown in the following listing:
public Block(Point location, BlockTypes newBlockType) {
//Create the new block, choose a new type if necessary.
if (newBlockType==BlockTypes.Undefined) {
BlockType = (BlockTypes)(random.Next(7)) + 1;
}
else {
BlockType = newBlockType; }
// Create each of the squares of the block.
// Set the square colors, based on the block type.
square1 = new Square(new Size(squareSize, squareSize),
backColors[(int)BlockType], foreColors[(int)BlockType]);
square2 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
square3 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
square4 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
// Set the square positions based on the block type.
switch(BlockType) {
case BlockTypes.Square:
// Create a Square block….
break;
case BlockTypes.Line:
// Create a Line block….
break;
case BlockTypes.J:
// Create a J block….
break;
case BlockTypes.L:
// Create an L block….
break;
case BlockTypes.T:
// Create a T block….
break;
case BlockTypes.Z:
// Create a Z block….
break;
case BlockTypes.S:
// Create an S block….
break;
}
}
In this sample, the code inside each case statement must set the square positions, based on each block type, according to Figure 1-24. For example, analyze the Square block type, depicted in Figure 1-25.

Figure 1-25. The squares for the Square block type
The code for creating the Square block type is shown here:
case BlockTypes.Square:
square1.Location = new Point(Location.X, Location.Y);
square2.Location = new Point(Location.X+squareSize, Location.Y);
square3.Location = new Point(Location.X, Location.Y+squareSize);
square4.Location = new Point(Location.X+squareSize, Location.Y+squareSize);
break;
As for the Line block type, the squares that compose it are shown in Figure 1-26.

Figure 1-26. The squares for the Line block type
The code for the Line block type is as follows:
case BlockTypes.Line:
square1.Location = new Point(Location.X, Location.Y);
square2.Location = new Point(Location.X, Location.Y+squareSize);
square3.Location = new Point(Location.X, Location.Y+2*squareSize);
square4.Location = new Point(Location.X, Location.Y+3*squareSize);
break;
The code for the other blocks follows the same idea. For the full code of the constructor, check the downloadable source code. Once the blocks are created, you can start coding the moving operations over them, as described in the next section.
This chapter is from Beginning .NET Game Programming in C#, by David Weller, et al., (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today.
Buy this book now. |
Next: The Down, Right, and Left Methods >>
More .NET Articles
More By Apress Publishing