ASP.NET
  Home arrow ASP.NET arrow Page 3 - .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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 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.

    More ASP.NET Articles
    More By Apress Publishing


     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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