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  
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
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 2
    2005-01-04

    Table of Contents:
  • .Netterpillars: Artificial Intelligence and Sprites
  • Object-Oriented Programming
  • Artificial Intelligence
  • Line of Sight
  • Use Your Imagination
  • Coding the Sprite Attributes
  • The Game Proposal
  • The Sprite Class
  • The Main Program Structure

  • 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 - Use Your Imagination


    (Page 5 of 9 )

    Although a lot of different techniques exist for solving problems relating to a game’s AI, there’s always room for new ideas. Learn from other people’s experience; see how the games behave and try to figure out how to mimic such behaviors in your game. There are a lot of good game developers’ sites where you can learn directly from the masters; a simple Web search using the keywords “artificial intelligence” and “games” will uncover the most interesting ones.

    Keep Libraries of Reusable Graphics and Objects

    Our final piece of advice is to always have your graphical routines and objects well polished and ready to use, so you can spend more time on the game’s physics and AI, the most important parts. Of course, you should also understand that the “first time around” isn’t always perfect, and be ready to refactor your code into more workable pieces as your knowledge about the objects improves. To this effect, we’ll show you how to start your library with a Sprite class, described in the next section.

    Sprites and Performance Boosting Tricks

    You’ll now start to create a set of generic classes that can be used in your future game projects, such as the Sprite class.

    In the food chain of game programming, sprites are like plankton. They’re at the very bottom of the food chain, but they’re fundamental building blocks to modern graphics programming. In game development, a sprite is a common term to specify any active object on the screen—for example, the player character, bullets, bonus objects, etc. You can also define sprite as any element on a game screen that is neither background nor information (such as menus or tips on the screen). A simple example of a sprite is your mouse pointer.

    In this chapter, you’ll create a simple Sprite class, which can be enhanced later to include additional features. Table 2-5 lists some of the basic attributes you may need.

    PROPERTY NAME DESCRIPTION
    Bitmap Holds a simple image for the sprite. In advanced sprite objects, you can have multiple arrays of images to deal with different animations (such as walking, jumping, dying, etc.).
    Position The actual x,y position of the sprite. Following the .NET property names, you can call this property Location.
    Scale The scale to be used for the position coordinates: pixel or the sprite’s size.
    Direction If the object is moving to (or “looking at”) a new position, you must have a direction property to hold this information.

    Table 2-5. Suggested Properties for a Simple Sprite Class

    As for the methods, three basic routines are obviously needed, and these are shown in Table 2-6.

    METHOD NAME DESCRIPTION
    Constructor You can create overloaded constructors that will receive different parameters: the sprite bitmap, the bitmap and the position, these two plus the direction, and so on. You will use method overloading to implement these different initialization methods.
    Draw This one is a must: All sprites must be drawn.
    Erase Erases the sprite, restoring the background picture, if it exists. To erase the sprite, this method must have access to the background bitmap, in order to copy the background over the previously drawn sprite.

    Table 2-6. Suggested Methods for a Simple Sprite Class


    Figure 2-5.
      The Sprite class

    Of course, you can come up with many other attributes and methods, such as velocity and acceleration attributes and a move method (which, using the direction, velocity, and acceleration, erases the sprite from the previous position and draws it in the new one). But let’s keep it simple for now! This kind of approach— defining the basic structure of a class or program, and then redefining it to produce many interactions (if needed)—is recognized as a good approach by the latest object-oriented software processes, such as the Microsoft Solutions Framework (MSF). We’ll not enter into any details here, but you’ll get a chance to see some simplified concepts from this software development philosophy in use.

    Sprite: Fast and Transparent

    Before you start coding the Sprite class, there are two things you must know:

    • How to draw the sprite as fast as possible.

    • How to draw nonrectangular sprites. Since most of your game objects won’t be rectangles or squares (like in the .Nettrix example), and all the functions draw rectangular images, you have to learn how to draw an image with a transparent color, in order to achieve the illusion of non-rectangular sprites.

    As for the first point, the GDI+ Graphics object has a method called DrawImage that draws an image at a given position in your work area. This method is very flexible, but it incurs a lot of overhead since it includes an internal method to scale the image, even when you don’t use the scaling parameters.

    Fortunately, you have a second method, DrawImageUnscaled, that just blits (copies a memory block directly to video memory) the source image, as is, to the destination position, with very low overhead. You’ll use this function, since it gives you all the speed you need.

    There’s also a third, even faster, function on the Graphics namespace, called DrawCachedBitmap, that maps the bitmap to the current memory video settings, so the drawing is just a matter of copying a memory position to video memory. This approach has only one drawback: If the player changes the monitor resolution when the game is playing, you’ll have unpredictable results. Unfortunately, this function is currently only available to C++ programs. Because you’ll learn how to work with high-speed graphics through DirectX in the next chapters, this limitation won’t be a problem if you want to create fast-paced action games.

    As for the transparent color, you have two possible approaches: You can set a so-called color key to be transparent, after loading the image, with the MakeTransparent Graphics method, or you can create a color-mapping array, which is much more flexible because you can set different degrees of transparency to different colors. We’ll be demonstrating the first approach here, because it’s simpler and all you need for now is a single transparent color, but we’ll also show you how to use a color map array, which can be used in other situations.

    The Sprite class is the base class for all active game objects, and since it must have access to some of the properties of the class that will manage the game (such as the background image used in erasing), some programmers like to derive it from that class. You’ll use this approach here, deriving the Sprite class from the GameEngine class (discussed later in the section, “The Game Proposal”).  

    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

    - 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
    - Building a Simple Storefront with LINQ
    - Developing a Dice Game Using ASP.NET Futures...





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