.Netterpillars: Artificial Intelligence and Sprites, Part 2 - Main Program and GameEngine Class
(Page 5 of 11 )
To test your Netterpillar class, you can add the MoveNetterpillars procedure to the GameEngine and improve the keypress code of the frmGameField to update the direction of your netterpillar.
In order to make the code more readable, you’ll add a Player1 property, which will point to the netterpillar that the player controls. Your netterpillar won’t be eating anything for now; you’ll test the EatAndMove method after you code the collision detection in the GameEngine class, in the final version of the game.
public Netterpillar[] netterPillars = new Netterpillar[4];
public Netterpillar Player1;
You can update the constructor of the GameEngine class to add four new netterpillars, and point the Player1 property to the first one, adding the following lines of code:
netterPillars[0] =
new Netterpillar((int)(this.Width/3), (int)(this.Height)
/3,Sprite.CompassDirections.South, false);
netterPillars[1] =
new Netterpillar((int)(this.Width/3), (int)
(this.Height)/3*2,
Sprite.CompassDirections.East, true);
netterPillars[2] =
new Netterpillar((int)(this.Width/3)*2, (int)
(this.Height)/3*2,
s s Sprite.CompassDirections.North, true);
netterPillars[3] =
new Netterpillar((int)(this.Width/3)*2, (int)
(this.Height)/3,
Sprite.CompassDirections.West, true);
Player1 = netterPillars[0];
NOTE Notice that you put the netterpillars a distance apart from each other, and set their initial direction to be different, so they won’t hit each other just after the game starts.
Your Move method is ready to move the netterpillar using the direction dictated by the game engine, so you’ll create a simple MoveNetterpillars method in the GameEngine class that will update the x or y position of each of the netter-pillars, based on their current direction.
public void MoveNetterpillars() {
int incX = 0; int incY = 0;
for(int i=0; i<NetterpillarNumber; i++) {
// Moves all the Netterpillars.
switch(netterPillars[i].Direction) {
case Sprite.CompassDirections.East:
incX = 1;
incY = 0;
break;
case Sprite.CompassDirections.West:
incX = -1;
incY = 0;
break;
case Sprite.CompassDirections.North:
incX = 0;
incY = -1;
break;
case Sprite.CompassDirections.South:
incX = 0;
incY = 1;
break;
}
netterPillars[i].Move(netterPillars[i].Location.X+incX,
netterPillars[i].Location.Y+incY, ScreenWinHandle);
}
}
To finish the second draft of the GameField class, you need to call the MoveNetterpillars method from the Render procedure, as follows:
public void Render() {
MoveNetterpillars();
Redraw();
}
and update the Redraw method to include the lines that will draw the netterpillars.
for(int i=0; i<NETTERPILLARNUMBER; i++) {
netterPillars[i].Draw(ScreenWinHandle);
}
Your Main program won’t need any updates, but if you want to test the Move method, you’ll have to add some new lines in the keyboard handler to update the direction of the player’s character depending on which key is pressed.
private void frmGameField_KeyDown(object sender,
KeyEventArgs e) {
// Just set the next direction for the player.
// We will not let the player go backwards from the
current direction,
// because he would die if he does so.
switch(e.KeyCode) {
case Keys.Right:
if (MainGame.objGameEngine.Player1.Direction !=
Sprite.CompassDirections.West) {
MainGame.objGameEngine.Player1.Direction =
Sprite.CompassDirections.East;
}
break;
case Keys.Left:
if (MainGame.objGameEngine.Player1.Direction!=
Sprite.CompassDirections.East) {
MainGame.objGameEngine.Player1.Direction =
Sprite.CompassDirections.West;
}
break;
case Keys.Up:
if (MainGame.objGameEngine.Player1.Direction!=
Sprite.CompassDirections.South) {
MainGame.objGameEngine.Player1.Direction =
Sprite.CompassDirections.North;
}
break;
case Keys.Down:
if (MainGame.objGameEngine.Player1.Direction!=
Sprite.CompassDirections.North) {
MainGame.objGameEngine.Player1.Direction =
Sprite.CompassDirections.South;
}
break;
case Keys.Escape:
objGameEngine.GameOver = true;
break;
}
}
In the keyboard handler in the preceding code, note the conditional statements: These test the current player’s direction and stop it from running backwards, which will lead to the immediate death of the netterpillar when you include collision detection.
Figure 2-16. Testing the netterpillars

Figure 2-16. Testing the .Netterpillars
This test will be a really quick one: Because you aren’t implementing collision detection yet, nor the AI, the computer-controlled netterpillars will go straight through the field and disappear off the edge of the screen. The game will crash a few seconds after that.
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: Third Draft: Coding the Game Engine and Collision Detection >>
More ASP.NET Articles
More By Apress Publishing