ASP.NET
  Home arrow ASP.NET arrow Page 10 - .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 - The Main Program: Final Version


    (Page 10 of 11 )

    In order to call the AI code, you create a new procedure, which will be called from the game main loop. The procedure, shown in the following code, just loops through the Netterpillars objects and, if they aren’t dead and are computer controlled, sets the current direction to the result of the ChooseNetterpillarDirection method:

    public static void MoveComputerCharacters() {
      //Move the Netterpillars.
      for(int i=0; i<objGameEngine.NetterpillarNumber; i++) {
        if (!objGameEngine.netterPillars[i].IsDead) {
          // A.I. for the computer-controled Netterpillars.
          if (objGameEngine.netterPillars[i].IsComputer) {
            objGameEngine.netterPillars[i].Direction =
              objAINetterpillar.ChooseNetterpillarDirection
               (objGameEngine.netterPillars[i].Location,
               objGameEngine.netterPillars[i].Direction);
          }
        }
      }
    }

    The main program loop should include one more section to call the MoveComputerCharacters procedure.

    while ( !objGameEngine.GameOver) {
      MoveComputerCharacters();
      objGameEngine.Render();
      Application.DoEvents();
    }

    This finishes the coding phase; some code to add polish to the final product is suggested in the next section.

    Adding the Final Touches

    In this section, you add some extra features to your game. These final touches, although simple, are important and need to be considered.

    Coding the Pause Game Feature

    As in the .Nettrix game, you could insert code to pause (and restart) the game when the Esc key is pressed. This basic improvement is shown here:

    private void frmGameField_KeyDown(object sender,
        KeyEventArgs e) {
      …
        case Keys.Escape:
          MainGame.objGameEngine.Paused = !
            MainGame.objGameEngine.Paused;
          if (MainGame.objGameEngine.Paused) {
            this.Text = “.Netterpillars - Press ESC to 
                        continue”;
          }
          else {
            this.Text = “.Netterpillars”;
          }
          break;
      }
    }

    Improving the Game Over Screen

    Your game over routine also needs an improvement. A good game programmer shouldn’t forget that a good game ending is far more important than a nice intro screen. Players must be rewarded for all their efforts in completing the game; it’s very frustrating for players to spend days and days finishing a game and not getting anything in return to give them a feeling of accomplishment. In this game, the Game Over message box is one of these frustrations. Although a high scores table would be better, let’s at least give players some feedback about the results of the game and how well they played.

    You can do this by creating a new Game Over window, where you can display some game statistics, as shown in Figure 2-18.

    Figure 2-18.  A Game Over screen

    This screen can access the objGameEngine, which is a public variable, and gather information about players and how long their netterpillars were when the game finished.

    To load the label with the statistics, you must access each of the netterPillars objects, checking the IsComputer property and the NetterBodyLength property. You’ll need to avoid unset objects (remember, the player could be playing with any number of opponents, from 0 to 3).

    The ternary operators in the next code sample (which must be placed in the Load event of the window) aren’t new to .NET, although they aren’t commonly used because sometimes they can lead to more complex code. The ternary operator tests the first parameter (an expression) and, if true, returns the second parameter; otherwise it returns the last parameter.

    LblPlayer1Length.Text =
      MainGame.objGameEngine.netterPillars[0]
        .NetterBodyLength.ToString();
    LblPlayer1Is.Text = MainGame.objGameEngine.netterPillars[0]
        .IsComputer ? “Computer” : “Human”;

    if (MainGame.objGameEngine.netterPillars[1]!=null) {
      LblPlayer2Length.Text =
        MainGame.objGameEngine.netterPillars[1]
          .NetterBodyLength.ToString();
      LblPlayer2Is.Text =
        MainGame.objGameEngine.netterPillars[1].IsComputer
          ? “Computer” : “Human”;
    }
    else {
      LblPlayer2Length.Text = “-”;
      LblPlayer2Is.Text = “-”;
    }

    if (MainGame.objGameEngine.netterPillars[2]!=null) {
      LblPlayer3Length.Text =
        MainGame.objGameEngine.netterPillars[2]
          .NetterBodyLength.ToString();
      LblPlayer3Is.Text = 
        MainGame.objGameEngine.netterPillars[2].IsComputer
          ? “Computer” : “Human”;
    }
    else {
      LblPlayer3Length.Text = “-”;
      LblPlayer3Is.Text = “-”;
    }
    if (MainGame.objGameEngine.netterPillars[3]!=null) {
      LblPlayer4Length.Text =
        MainGame.objGameEngine.netterPillars[3]
          .NetterBodyLength.ToString();
      LblPlayer4Is.Text =
        MainGame.objGameEngine.netterPillars[3].IsComputer
          ? “Computer” : “Human”;
    }
    else {
      LblPlayer4Length.Text = “-”;
      LblPlayer4Is.Text = “-”;
    }

    In final version of the main program, you must replace the Game Over message box by a call to the ShowDialog method of the game over form.

    Coding for the Garbage Collection

    A technical enhancement is to improve the speed of the garbage collection by calling the Collect method of the System.GC object, in the end of the Render method, as shown:

    public void Render() {
      …
      System.GC.Collect();

    }


    NOTE The .NET Framework provides an advanced garbage collector that frees the memory from all objects left behind by the program. The garbage collection takes place in idle system time, but you can force it to run by calling the Collect method, which is good practice if you are dealing with lots of memory allocations and reallocations—which you do, for example, with the Graphics object in each Draw method in the game objects.

    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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek