ASP.NET
  Home arrow ASP.NET arrow Page 7 - .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 - Fourth Draft: Coding the Config Screen and Game Over


    (Page 7 of 11 )

    Before starting the AI code, let’s include some details in your game, adding the code for the configuration screen and the test for ending the game.

    Coding for the Configuration Screen

    Looking at the visual prototype of the configuration screen, you can see that you have two properties that don’t map directly to numbers: the game field size and the quantity of mushrooms on the screen. In order to create a more direct mapping from the screen to the game field properties, let’s add two property procedures: Size (which will set the width and height properties) and Mushrooms (which will set the MushroomNumber property, according to the current size of the game field), as shown in the following code.

    Here’s the Size property:

    public enum GameFieldSizes {
      Small = 2,
      Medium = 1,
      Big = 0
    };
    private GameFieldSizes size = GameFieldSizes.Medium;
    public GameFieldSizes Size {
      get {
        return size;
      }
      set {
        size = value;
        switch(value) {
          case GameFieldSizes.Small:
            Width = 15;
            Height = 15;
            break;
          case GameFieldSizes.Medium:
            Width = 25;
            Height = 25;
            break;
          case GameFieldSizes.Big:
            Width = 40;
            Height = 30;
            break;
        }
      }
    }

    And now the Mushroom property:

    public enum MushroomSizes {
      Few = 2,
      JustRight = 1,
      Many = 0
    };
    public MushroomSizes Mushrooms {

      get {
        return mushroomQty;
      }
      set {
        mushroomQty = value;
        switch(value) {
          case MushroomSizes.Few:
            MushroomNumber = 25;
            break;
          case MushroomSizes.JustRight:
            MushroomNumber = 75;
            break;
          case MushroomSizes.Many:
            MushroomNumber = 125;
            break;
        }

        if (Size==GameFieldSizes.Medium) {
          MushroomNumber *= 2;
        }
        else if(Size==GameFieldSizes.Big) {
          MushroomNumber *= 3;
        }
      }
    }

    You must adjust the constructor too, because you are always creating four netterpillars. Instead of using a fixed number, you should use the NetterpillarNumber property, which will be set in the configuration window.

    Because you’ll be creating one to four netterpillars, let’s define where each of them will be created:

    • If you have one netterpillar, create it in the center of the screen.

    • If you have two netterpillars, create them in the center of the y axis (vertical), and at 1/3 and 2/3 along the x axis (horizontal), so you’ll have a constant distance from the borders to the netterpillars and between the two netterpillars. It’s better to initialize them running in different directions; so one will head north and another south.

    • If you have three netterpillars, you’ll put them at 1/4, 2/4, and 3/4 along the x axis, and in the middle of the y axis, heading south, north, and south again.

    • If you have four netterpillars, you’ll put them in a square, each heading in the direction of the next vertex. The vertices will be at 1/3 vertical, 1/3 horizontal; 1/3 vertical, 2/3 horizontal; 2/3 vertical, 2/3 horizontal; and 2/3 vertical, 1/3 horizontal.

    The code for this logic is show here:

    // Create the Netterpillars.
    switch(NetterpillarNumber) {
      case 1:
        netterPillars[0] = new Netterpillar((int)
          (this.Width/2),(int)(this.Height)/2, 
           Sprite.CompassDirections.South, false);
        break;
      case 2:
        netterPillars[0] = new Netterpillar((int)
          (this.Width/3),(int)(this.Height)/2,
           Sprite.CompassDirections.South, false);
        netterPillars[1] = new Netterpillar((int)(this.Width/3)
          *2,(int)(this.Height)/2,
          Sprite.CompassDirections.North, true);
        break;
      case 3:
        netterPillars[0] = new Netterpillar((int)
          (this.Width/4),(int)(this.Height)/2,
           Sprite.CompassDirections.South, false);
        netterPillars[1] = new Netterpillar((int)(this.Width/4)
          *2,(int)(this.Height)/2,
          Sprite.CompassDirections.North, true);
        netterPillars[2] = new Netterpillar((int)(this.Width/4)
          *3,(int)(this.Height)/2,
          Sprite.CompassDirections.South, true);
        break;
      case 4:
        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,
          Sprite.CompassDirections.North, true);
        netterPillars[3] = new Netterpillar((int)(this.Width/3)
          *2,(int)(this.Height)/3, 
          Sprite.CompassDirections.West, true);
        break;
    }

    To allow you to test the configuration code, you need to add some lines to the Load event and the OK button of the configuration screen.

    When loading the form, you must set the controls to the current value of each of the objGameEngine configuration properties.

    private void frmConfig_Load(object sender,
        System.EventArgs e) {
      updGameField.SelectedIndex = (int)
        MainGame.objGameEngine.Size;
      updNetterpillars.Value =
        MainGame.objGameEngine.NetterpillarNumber;
     
    updMushrooms.SelectedIndex = (int)
        MainGame.objGameEngine.Mushrooms;
    }

    In the OK click procedure, you’ll do the opposite, setting the objGameEngine properties to the values set on the form.

    private void cmdOK_Click(System.Object sender,
         System.EventArgs e) {
      MainGame.objGameEngine.Size =
        (GameEngine.GameFieldSizes)updGameField.SelectedIndex; 
      MainGame.objGameEngine.NetterpillarNumber = (int)
        System.Math.Round(updNetterpillars.Value);
      MainGame.objGameEngine.Mushrooms =
        (GameEngine.MushroomSizes)updMushrooms.SelectedIndex;
    }

    Everything is now correctly positioned, but you need to show the configuration dialog box at some point in the program, or else you won’t be able to change the configuration settings. It’s time to go back to your Main procedure and include in it the main window, through which you can change the configuration, start a new game, or exit the game. The window will be the one that was shown as a visual prototype in the project phase, including some lines of code in the Config button to show the configuration screen, as demonstrated in the code that follows. Some code to close the window must also be included on the click event of the Exit button.

    private void cmdConfig_Click(System.Object sender,
            System.EventArgs e) {
      frmConfig WinConfig;
      WinConfig = new frmConfig();
      WinConfig.ShowDialog();
      WinConfig.Dispose();
    }

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek