.Netterpillars: Artificial Intelligence and Sprites, Part 2 - Main Program and GameEngine Class
(Page 3 of 11 )
Since you already have two of the base classes, it’s time to do some tests to check whether everything is okay so far. Instead of doing a simple test program, let’s go one step ahead and start implementing the game Main procedure and the GameEngine class, so you can start to understand the game logic, and add to them when new features become available.
Looking at the class diagram, you can pick some properties and methods that will help you to create a subset of the final GameEngine class, which will allow you to test the classes you created. You’ll need to code the properties associated with mushrooms and branches, the constructor (to initialize the objects), the Redraw method (to draw the objects), and a Render object, the method which will do all the game physics (for now, only calling the Redraw method). Your stub class will be as follows:
public class GameEngine {
public int Width = 25;
public int Height = 25;
public static Image BackgroundImage;
private System.IntPtr ScreenWinHandle;
// Game objects
private Branch[] branches;
private Mushroom objMushrooms;
private int MushroomNumber = 75;
//Controls the game end.
public bool GameOver;
public void Render() {…}
public void Redraw() {…}
}
In the constructor, all you do is create the object arrays and each of the objects. You’ll also store the window handle received in the function to be used by the Redraw procedure.
public void GameEngine(System.IntPtr WinHandle) {
int x; int y;
objBranchs = new Branch[5];
// Reset the mushroomNumber, forcing a call to the
property procedure.
Mushrooms = Mushrooms;
ScreenWinHandle = WinHandle;
// Create the branches.
objBranchs[0] = new Branch
(Sprite.CompassDirections.North, this.Height);
objBranchs[1] = new Branch
(Sprite.CompassDirections.North, this.Height);
objBranchs[2] = new Branch(Sprite.CompassDirections.East,
this.Width-2);
objBranchs[3] = new Branch(Sprite.CompassDirections.East,
this.Width-2);
// Create the mushrooms.
objMushrooms = new Mushroom();
for(int i=0; i<MushroomNumber; i++) {
x = rand.Next(0, this.Width-2)+1;
y = rand.Next(0, this.Height-2)+1;
}
}
For now, the Render method just calls the Redraw method; in future versions it will call the functions to implement the game physics.
public void Render() {
Redraw();
}
As for your Redraw method, all you need to do is call the Draw method of each game object.
public void Redraw() {
for(int x=0; x<Width; x++) {
for(int y=0; y<Height; y++) {
if (GameField[x, y]==GameObjects.Mushroom) {
objMushrooms.Location = new Point(x, y);
objMushrooms.Draw(ScreenWinHandle);
}
}
}
objBranchs[0].Draw(ScreenWinHandle, 0, 0);
objBranchs[1].Draw(ScreenWinHandle, (this.Width-1), 0);
objBranchs[2].Draw(ScreenWinHandle, 1, 0);
objBranchs[3].Draw(ScreenWinHandle, 1, (this.Height-1));
}
Now, with the GameEngine class stub done, you need to create a Main procedure that will generate the game engine object and call the Render method until the game is over (in this case when the Esc key is pressed). To do this, add a module to the solution and include the following code:
class MainGame {
public static GameEngine netterpillarGameEngine;
public static void Main(string [] args) {
// Create the game engine object.
netterpillarGameEngine = new GameEngine();
WinGameField = new frmGameField();
WinGameField.Show();
// Create a copy of the background image to allow
erasing the sprites.
GameEngine.BackgroundImage =(Image)
WinGameField.PicGameField.Image.Clone();
while ( !objGameEngine.GameOver) {
netterpillarGameEngine .Render();
Application.DoEvents();
}
WinGameField.Dispose();
netterpillarGameEngine = null;
}
}
To finish this first draft, capture the Esc key to end the game. This can be done using the KeyDown event of frmGameField.
private void frmGameField_KeyDown
(object sender, System.Windows.Forms.KeyEventArgs e) {
// Just showing Esc key behavior right now.
switch(e.KeyCode) {
case Keys.Escape:
MainGame.objGameEngine.GameOver = true;
break;
}
}
Running your program now, you can see a basic game field filled with mushrooms, as shown in Figure 2-14.

Figure 2-14. Testing the first basic classes
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: Second Draft: Coding the Player Character >>
More ASP.NET Articles
More By Apress Publishing