Game Development of .Nettrix: GDI+ and Collision Detection - The Coding Phase
(Page 11 of 22 )
When coding any project, it’s always useful to create drivers and stubs to allow you to test each component separately. Drivers are programs that control other lower-level programs, and stubs are programs that mimic low-level programs’ behavior, allowing the testing of higher level code. To provide a vision of a real coding phase, you’ll sometimes use such techniques to validate the code written step by step.
You’ll go through three versions, from your first draft to the final code:
- First draft: Code the Square class.
- Second draft: Code the Block class.
- Final version: Code the GameField class and the game engine.
You start coding from the lowest level class, Square, in the next section.
First Draft: Coding the Square Class Reviewing the game project, you find the basic structure of the class and create the public class interface.
public class Square {
public Point Location;
public Size Size;
public Color ForeColor;
public Color BackColor;
public void Show(System.IntPtr WinHandle) {}
public void Hide(System.IntPtr WinHandle) {}
}
The class methods are shown in the next section.
The Show and Hide Methods
In the Show method all you need to do is to adapt the code for creating a path gradient rectangle you saw in the previous section. For the Hide method, you can hide the rectangle in an easier way: Since you’ll be working with a one-color background (no textures or bitmaps yet), you can simply draw the rectangle again, this time using a solid color, the same as the background.
To create a generic code that can be updated later by any programmer, it’s always a good idea to not use fixed values inside your program. In this example, you’d better read the game field background color from some variable, so that if it’s updated later to another color, your Hide method will still work. This color value should be a property of the GameField class, but since this property doesn’t appear in your game project, you’ll need to update it with this new property. In a real project it’s common for some details (like this one) to only become visible at the coding phase, since it’s usually not possible for the project to predict all possible details.
The code for the Square class is shown here:
public class Square {
public Point Location;
public Size Size;
public Color ForeColor;
public Color BackColor;
// Draws a rectangle with gradient path using the properties above.
public void Show(System.IntPtr WinHandle)
{
Graphics GameGraphics;
GraphicsPath graphPath;
PathGradientBrush brushSquare;
Color[] surroundColor;
Rectangle rectSquare;
// Gets the Graphics object of the background picture.
GameGraphics = Graphics.FromHwnd(WinHandle);
// Creates a path consisting of one rectangle.
graphPath = new GraphicsPath();
rectSquare = new Rectangle(Location.X,Location.Y,Size.Width,Size.Height);
graphPath.AddRectangle(rectSquare);
// Creates the gradient brush that will draw the square.
// Note: There's one center color and an array of border colors.
brushSquare = new PathGradientBrush(graphPath);
brushSquare.CenterColor = ForeColor;
surroundColor = new Color[]{BackColor };
brushSquare.SurroundColors = surroundColor;
// Finally draws the square.
GameGraphics.FillPath(brushSquare, graphPath);
}
public void Hide(System.IntPtr WinHandle)
{
Graphics GameGraphics;
Rectangle rectSquare;
// Gets the Graphics object of the background picture.
GameGraphics = Graphics.FromHwnd(WinHandle);
// Draws the square.
rectSquare = new Rectangle(Location.X,Location.Y,Size.Width,Size.Height);
GameGraphics.FillRectangle(new SolidBrush(GameField.BackColor), rectSquare);
}
}
Note: In the Hide method shown previously, you can see an unusual use of the BackColor property: You are using the property directly from the class definition, instead of from a previously created object in this class. In this case, you are using a new feature of .NET: static properties and methods. Defining a method or a property as public static makes it available for any part of the program directly from the class name, without the need for explicitly creating an object. An important point is that the property or method is shared by all the instances of the objects created from the class. For example, you can have a static counter property that each object increments when it’s created and decrements when it’s destroyed, and any object can read this counter at any time in order to see how many objects are available at any given time. |
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: Testing the Program >>
More .NET Articles
More By Apress Publishing