Game Development of .Nettrix: GDI+ and Collision Detection - Testing the Program
(Page 12 of 22 )
Testing the Square Class
Now you are ready to test your program. To do this, you’ll need to create a driver to call the class (a window with a button and a pictureBox will suffice), and a stub for the GameField class, since your Square class uses the BackColor property of this class.
The stub is very simple, just a new file composed of the code lines shown in the next sample:
public class GameField {
public static Color BackColor;
}
The driver will be replaced by the main program in the final version, so you can implement it as code on the form that will be used as the game user interface. In this case, you can create a simple form with a picture (picBackground) and a button (cmdStart), with the code to create the objects and set the properties of the Square class, then call the Draw method.
private void cmdStart_Click(Object sender, System.EventArgs e) {
Square square = new Square();
square.Location = new Point(40, 20);
square.Size = new Size(10, 10);
square.ForeColor = Color.Blue;
square.BackColor = Color.Green;
// Set the background property of GameField class.
GameField.BackColor = picBackground.BackColor;
// Draw the square. square.
Draw(picBackground.Handle);
}
Running the code, you can see the fruits of your labor: a nice path gradient– colored square is drawn on screen as shown in Figure 1-23.

Figure 1-23. Your first results with GDI+
Because in your game the squares won’t change color or size, you can assign these values when creating the objects, creating a new constructor in the Square class to do this, as illustrated in the next code sample:
public Square(Size InitialSize,Color InitialBackcolor,Color InitialForecolor) {
size = InitialSize;
BackColor = InitialBackcolor;
ForeColor = InitialForecolor;
}
So the code for your Start button will be as follows:
private void cmdStart_Click(object sender, System.EventArgs e) {
// Clean the game field.
Square square = new Square(new Size(10, 10), Color.Blue, Color.Green);
// Set the location of the square.
square.Location = new Point(40, 20);
// Set the background property of GameField.
GameField.BackColor = picBackground.BackColor;
// Draw the square.
square.Draw(picBackground);
}
Now that everything is working correctly, continue with the coding by looking at the Block class.
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 Block Class >>
More .NET Articles
More By Apress Publishing