ASP.NET
  Home arrow ASP.NET arrow Page 5 - .Netterpillars: Artificial Intelligence an...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

.Netterpillars: Artificial Intelligence and Sprites, Part 2
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2005-01-11

    Table of Contents:
  • .Netterpillars: Artificial Intelligence and Sprites, Part 2
  • The Branch Class
  • Main Program and GameEngine Class
  • Second Draft: Coding the Player Character
  • Main Program and GameEngine Class
  • Third Draft: Coding the Game Engine and Collision Detection
  • Fourth Draft: Coding the Config Screen and Game Over
  • Coding for the Introduction Screen
  • Final Version: Coding the Netterpillars AI
  • The Main Program: Final Version
  • Further Improvements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    .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.

    More ASP.NET Articles
    More By Apress Publishing


     

    ASP.NET ARTICLES

    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...
    - Building an In-Text Advertising System Under...
    - Developing a Mini ASP.NET AJAX Server Centri...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT