.Netterpillars: Artificial Intelligence and Sprites, Part 2
(Page 1 of 11 )
In this article on artificial intelligence, you will take a computer game from first draft to final version. It is excerpted from chapter two of the book
Beginning .NET Game Programming in C#, by Ellen Hatton et. al. (Apress, 2004, ISBN: 1590593197) and is the second of two parts.
The Coding Phase
As you did in the previous chapter, you’ll start coding the basic objects for the game (simplest first), and then tackle the more difficult code of the game engine and the netterpillar AI classes.
To allow you to test every new method created, you’ll do your code in five steps:
- First draft: Code the static objects.
- Second draft: Code the player character.
- Third draft: Code the game engine and collision detection.
- Fourth draft: Code the configuration screen and game over.
- Final version: Code the netterpillars AI.
The details of each of these versions are shown in the next sections.
First Draft: Coding the Static Objects In the next sections, we show the code and discuss the details of the classes for the static objects, mushrooms, and branches, and create an early version of the main program and the GameEngine class, so you can test these classes.
The Sprite Class
You’ll only add a new property in this class, the IMAGE_PATH constant, which will be used by all the child classes to compose the full path from where the images should be loaded.
The Mushroom Class
There’s not much to say about the Mushroom class. It just has an overloaded constructor that creates a sprite with the mushroom drawing, to be used instead of the original sprite constructor with a parameter. This will allow cleaner code when creating mushrooms.
public class Mushroom : Sprite {
public Mushroom() {…}
}
The code for the constructor will be as follows:
public class Mushroom : Sprite {
public Mushroom() :
base(Application.StartupPath+”\\”+IMAGE_PATH+
”\\Mushroom.gif”) {}
}
Note that all you do is call the base class’s new method, passing the appropriate parameters.
NOTE When a child class defines a method that already exists in the base class, any object created from the child class will call the code in the method of this class, unless you explicitly call the base class method, as in the preceding code sample (using base() after the colon, plus any necessary parameters).
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: The Branch Class >>
More ASP.NET Articles
More By Apress Publishing