Game Development of .Nettrix: GDI+ and Collision Detection - Adding the Final Touches
(Page 21 of 22 )
After playing the first version of .Nettrix for a few minutes, every player will miss two important features present in almost every Tetris type of game: a feature to show the next block that will appear, and some way to pause the game, for emergency situations (like your boss crossing the office and heading in your direction).
Now that you have all base classes already finished, this is easily done. The next sections discuss these and some other features to improve your first game.
Coding the Next Block Feature To show the next block, you can create a new pictureBox on the form to hold the next block image, and adjust the click of the Start button and the timer_tick event. You can use the optional parameter you created on the Block constructor to create the new blocks following the block type of the next block.
To implement this feature, you create a variable to hold the next block in the general section of the form.
private Block NextBlock;
At the end of the cmdStartclick event, you add two lines to create the next block.
NextBlock = new Block(new Point(20, 10),
Block.BlockTypes.Undefined);
NextBlock.Show(PicNextBlock.Handle);
And finally you adjust the Tick event of the timer to create a new block every time the current block stops falling, and to force the CurrentBlock type to be the same as the NextBlock type.
// Replace the current block...
CurrentBlock= new Block(new Point(GameField.SquareSize*6,0), NextBlock.BlockType);
CurrentBlock.Show(PicBackground.Handle);
// Create the Next block.
NextBlock.Hide(PicNextBlock.Handle);
NextBlock = new Block(new Point(20,10), Block.BlockTypes.Undefined);
NextBlock.Show(PicNextBlock.Handle);
You can now run the game and see the next block being displayed in the picture box you’ve just created, as shown in Figure 1-33.

Figure 1-33. Showing the next block
The next section shows another improvement, the game pause feature.
Coding the Game Pause Feature To create a pause function, all you need to do is to stop the timer when a specific key is pressed—in this case, you use the Escape (Esc) key. A simple adjustment in the KeyDown event, including an extra case clause for the Keys.Escape value, will do the trick.
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;
case Keys.Escape:
tmrGameClock.Enabled = !tmrGameClock.Enabled;
if (tmrGameClock.Enabled)
this.Text = ".NETTrix";
else
this.Text = ".NETTrix -- Press 'Esc' to Continue";
break;
default: break;
}
Invalidate();
}
In the next section, we’ll discuss an improvement to the graphical part of your game.
Coding the Window Redraw A little problem with your game is that, when the .Nettrix window is covered by other windows, the game field isn’t redrawn. You can adjust this by including a call to the GameField’s Redraw method, at the Activate event of the form (the Activate event occurs every time the form gets the focus again, after losing it to another window).
private void NetTrix_Activated(object sender, System.EventArgs e) {
// This event occurs when the window receives back
// the focus after losing it to another window.
// So, redraw the whole game field.
PicBackground.Invalidate();
Application.DoEvents();
GameField.Redraw();
if (NextBlock != null)
NextBlock.Show(PicNextBlock.Handle);
}
Even using this approach there’ll be some situations when the windows won’t be redrawn properly. To achieve the best results, you should include the call to the Redraw method in the Tick event of the timer, but since it could compromise the speed of your game, keep the code as shown.
The next section discusses some suggestions for future enhancements to 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: Further Improvements >>
More .NET Articles
More By Apress Publishing