Game Development of .Nettrix: GDI+ and Collision Detection - The Show and Hide Methods
(Page 17 of 22 )
The implementation of the Show and Hide methods is very straightforward; the Show and Hide methods are called for each of the block squares, as shown here:
public void Show(System.IntPtr WinHandle) {
// Draws each square of the block on the game field.
square1.Show(WinHandle);
square2.Show(WinHandle);
square3.Show(WinHandle);
square4.Show(WinHandle);
}
public void Hide(System.IntPtr WinHandle) {
// Hides each square of the block on the game field.
square1.Hide(WinHandle);
square2.Hide(WinHandle);
square3.Hide(WinHandle);
square4.Hide(WinHandle);
}
To see the full code for the Block class, refer to the samples in the downloadable source code. To test your new class, you’ll have to create a new stub for the GameField class and update your main program, as shown in the next section.
Testing the Block Class
The new stub for the GameField class must include the properties and methods accessed by the Block class, as shown in the next code listing:
public class GameField {
public static System.IntPtr WinHandle;
public static Color BackColor;
public static bool IsEmpty(int x, int y) {
return true;
}
public static void StopSquare(Square Square, int x, int y) {
}
The IsEmpty method always returns True; you’ll add code for IsEmpty and StopSquare in the final version of the program. The next code listing shows the logic for testing the Block class, and must be included in the game field form:
private Block CurrentBlock;
…
private void NetTrix_Load(object sender, System.EventArgs e) {
// Set the properties of GameField class.
GameField.BackColor = PicBackground.BackColor;
GameField.WinHandle = PicBackground.Handle;
}
private void CmdStart_Click(object sender, System.EventArgs e) {
CurrentBlock = new Block(new Point(GameField.SquareSize * 6, 50), Block.BlockTypes.Undefined);
CurrentBlock.Show(PicBackground.Handle);
}
private void NetTrix_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
switch(e.KeyCode) {
case Keys.Right: CurrentBlock.Right();break;
case Keys.Left : CurrentBlock.Left();break;
case Keys.Up : CurrentBlock.Rotate();break;
case Keys.Down : CurrentBlock.Down();break;
default: break;
}
}
Note: All the constants in the .NET Framework are organized within enumerations. This approach allows a more intuitive orga nization, so it’s easier to find exactly what you need in the help feature. The intelligence of Visual Studio was also improved, giving more hints and softening the learning curve. In the preceding sam ple code, you use the Keys enumeration to get the key code (Left, Down, Up, and Right). There are also modifiers to test if Shift, Ctrl, and Alt keys are pressed. The namespace for the Keys enumeration can be found in System.Windows.Forms. |
To test the program, just run it, click the Start button, and press the various keys to move the blocks: The down arrow key makes the block go down, the up arrow key rotates the block, and the right arrow and left arrow keys move the block horizontally. Clicking the Start button again will create a new block, so you can test the random creation of different block types. A sample screen is shown in Figure 1-31.

Figure 1-31. Testing the Block class
In the next section you’ll implement the collision detection and the main program logic, finishing your game.
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: Final Version: Coding the GameField Class and the Game Engine >>
More .NET Articles
More By Apress Publishing