.Netterpillars: Artificial Intelligence and Sprites, Part 2 - The Branch Class
(Page 2 of 11 )
The Branch class will also be derived from the Sprite base class, but because a branch can have different sizes, you’ll have to add extra variables to hold the branch bitmaps, and create a new Draw method and constructors that will do the branch creation and drawing. The next code sample presents the Branch class interface:
public class Branch : Sprite {
private Bitmap BranchStart;
private Bitmap[] BranchMiddle;
private Bitmap BranchEnd;
public int branchSize;
public Branch(CompassDirections branchDirection,
int intSize) {…}
public void Draw(System.IntPtr winHandle, int x,
int y) {…}
}
As noted before, your Branch class will be used to improve the visual interface by placing branches around the game field. This class will have only two methods: the constructor, which will load the bitmaps from disk, and the Draw method, which will draw the branch on screen. Since the branches don’t move or disappear during the game, you won’t need to code an Erase method.
Each branch will be composed by a set of at least three images: a “branch start,” a “branch end,” and one or more “branch middles.” Since you’ll need horizontal and vertical branches, you’ll need six different images, created with a specific naming convention to help you, as shown in Figure 2-13.
Figure 2-13. The branch images
The constructor will use the concepts explained in the Load method of the Sprite class, extending the code to store the images in the specific properties of the Branch class—BranchStart, BranchMiddle array, and BranchEnd.
public Branch(CompassDirections branchDirection,
int intSize) {
BranchMiddle = new Bitmap[intSize-2];
string strImagePrefix;
branchSize = intSize;
Direction = branchDirection;
// Picks the prefix for the branch - Horizontal or
vertical?
strImagePrefix = “Hor”; // Default direction is east-west
(Horizontal)
if (Direction==Sprite.CompassDirections.North ||
Direction==Sprite.CompassDirections.South) {
strImagePrefix = “Vert”;
}
// Load the top, the middle parts, and the end of the
branch.
// Magenta is the colorkey (which will be transparent)
for the Load method.
BranchStart =
Load(Application.StartupPath+”\\”+IMAGE_PATH+”\\”+
strImagePrefix+”BranchStart.gif”, Color.FromArgb(255,
255, 0, 204));
for(int i=0; i<=branchSize-3; i++) {
BranchMiddle[i] =
Load(Application.StartupPath+”\\”+IMAGE_PATH+”\\”+
strImagePrefix+”BranchMiddle.gif”, Color.FromArgb
(255, 255, 0, 204));
}
BranchEnd =
Load(Application.StartupPath+”\\”+IMAGE_PATH+”\\”+
strImagePrefix+”BranchEnd.gif”, Color.FromArgb(255,
255, 0, 204));
}
Here are some points to note about the preceding code:
- You use the naming conventions stated before to load the appropriate images, including the prefix “Hor” for the horizontal images and “Vert” for the vertical ones. You use the branchDirection parameter of the CompassDirections enumeration (defined in the base class Sprite) to choose whether the branch will be vertical (north and south directions) or horizontal (west and east directions).
- The image files were drawn using the magenta color where you need to create transparency, that’s why you use Color.fromARGB(255, 255, 0, 204) as the parameter for the keycolor of the Load function (defined in the Sprite base class).
- The dimension of the BranchMiddle array is defined as intSize-3 because the size of the branch will take into account the start and the end of the branch, so you need an array with the defined size minus two. Since all arrays in C# are zero based, you have intSize-2 elements when defining an array that goes from 0 to intSize-3. A little tricky, isn’t it?
The Draw method will be very similar to the method with the same name on the base class. In fact, you’ll be calling the base class method in order to draw each of the parts of the branch, so you won’t have any real drawing code in this method.
public void Draw(System.IntPtr winHandle, int x, int y) {
// Sets the location and draws the start of the branch.
Location = new Point(x, y);
base.Draw(BranchStart, winHandle);
// Sets the location and draws each of the branch middle
parts.
if (Direction==Sprite.CompassDirections.North||
Direction==Sprite.CompassDirections.South) {
// It’s a horizontal branch.
for(int i=0; i<=branchSize-3; i++) {
y++;
Location = new Point(x, y);
base.Draw(BranchMiddle[i], winHandle);
}
y++;
}
else {
// It’s a vertical branch.
for(int i=0; i<=branchSize-3; i++) {
x++;
Location = new Point(x, y);
base.Draw(BranchMiddle[i], winHandle);
}
x++;
}
// Sets the location and draws the start of the branch.
Location = new Point(x, y);
base.Draw(BranchEnd, winHandle);
}
This chapter is from Beginning .NET Game Programming in C# by Ellen Hatton et al. (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today. Buy this book now.
|
Next: Main Program and GameEngine Class >>
More ASP.NET Articles
More By Apress Publishing