ASP.NET
  Home arrow ASP.NET arrow Page 2 - .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 Branch Class


    (Page 2 of 11 )

    The Branch class will also be derived from the Sprite base class, but because a branch can have different sizes, you’ll have to add extra variables to hold the branch bitmaps, and create a new Draw method and constructors that will do the branch creation and drawing. The next code sample presents the Branch class interface:

    public class Branch : Sprite {
      private Bitmap BranchStart;
      private Bitmap[] BranchMiddle;
      private Bitmap BranchEnd;
      public int branchSize;
      public Branch(CompassDirections branchDirection,
                    int intSize) {…}
      public void Draw(System.IntPtr winHandle, int x,
                       int y) {…}
    }

    As noted before, your Branch class will be used to improve the visual interface by placing branches around the game field. This class will have only two methods: the constructor, which will load the bitmaps from disk, and the Draw method, which will draw the branch on screen. Since the branches don’t move or disappear during the game, you won’t need to code an Erase method.

    Each branch will be composed by a set of at least three images: a “branch start,” a “branch end,” and one or more “branch middles.” Since you’ll need horizontal and vertical branches, you’ll need six different images, created with a specific naming convention to help you, as shown in Figure 2-13.

     

    Figure 2-13.  The branch images

    The constructor will use the concepts explained in the Load method of the Sprite class, extending the code to store the images in the specific properties of the Branch class—BranchStart, BranchMiddle array, and BranchEnd.

    public Branch(CompassDirections branchDirection,
                  int intSize) {
      BranchMiddle = new Bitmap[intSize-2];
      string strImagePrefix;
      branchSize = intSize;
      Direction = branchDirection;
      // Picks the prefix for the branch - Horizontal or
         vertical?
      strImagePrefix = “Hor”; // Default direction is east-west
                                 (Horizontal)
      if (Direction==Sprite.CompassDirections.North ||
            Direction==Sprite.CompassDirections.South) {
        strImagePrefix = “Vert”;
      }
      // Load the top, the middle parts, and the end of the
         branch.
      // Magenta is the colorkey (which will be transparent)
         for the Load method.
      BranchStart =
        Load(Application.StartupPath+”\\”+IMAGE_PATH+”\\”+
        strImagePrefix+”BranchStart.gif”, Color.FromArgb(255,
           255, 0, 204));
      for(int i=0; i<=branchSize-3; i++) {
        BranchMiddle[i] =
          Load(Application.StartupPath+”\\”+IMAGE_PATH+”\\”+
          strImagePrefix+”BranchMiddle.gif”, Color.FromArgb
           (255, 255, 0, 204));
      }
      BranchEnd =
        Load(Application.StartupPath+”\\”+IMAGE_PATH+”\\”+
        strImagePrefix+”BranchEnd.gif”, Color.FromArgb(255,
          255, 0, 204));
    }

    Here are some points to note about the preceding code:

    • You use the naming conventions stated before to load the appropriate images, including the prefix “Hor” for the horizontal images and “Vert” for the vertical ones. You use the branchDirection parameter of the CompassDirections enumeration (defined in the base class Sprite) to choose whether the branch will be vertical (north and south directions) or horizontal (west and east directions).

    • The image files were drawn using the magenta color where you need to create transparency, that’s why you use Color.fromARGB(255, 255, 0, 204) as the parameter for the keycolor of the Load function (defined in the Sprite base class).

    • The dimension of the BranchMiddle array is defined as intSize-3 because the size of the branch will take into account the start and the end of the branch, so you need an array with the defined size minus two. Since all arrays in C# are zero based, you have intSize-2 elements when defining an array that goes from 0 to intSize-3. A little tricky, isn’t it?

    The Draw method will be very similar to the method with the same name on the base class. In fact, you’ll be calling the base class method in order to draw each of the parts of the branch, so you won’t have any real drawing code in this method.

    public void Draw(System.IntPtr winHandle, int x, int y) {
      // Sets the location and draws the start of the branch.
      Location = new Point(x, y);
      base.Draw(BranchStart, winHandle);
      // Sets the location and draws each of the branch middle
         parts.
      if (Direction==Sprite.CompassDirections.North||
          Direction==Sprite.CompassDirections.South) {
        // It’s a horizontal branch.
        for(int i=0; i<=branchSize-3; i++) {
          y++;
          Location = new Point(x, y);
          base.Draw(BranchMiddle[i], winHandle);
        }
        y++;
      }
      else {
        // It’s a vertical branch.
        for(int i=0; i<=branchSize-3; i++) {
          x++;
          Location = new Point(x, y);
          base.Draw(BranchMiddle[i], winHandle);
        }
        x++;
      }
      // Sets the location and draws the start of the branch.
      Location = new Point(x, y);
      base.Draw(BranchEnd, winHandle);
    }

    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

    - 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...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach





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