.NET
  Home arrow .NET arrow Page 11 - Game Development of .Nettrix: GDI+ and Col...
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? 
.NET

Game Development of .Nettrix: GDI+ and Collision Detection
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2004-08-23

    Table of Contents:
  • Game Development of .Nettrix: GDI+ and Collision Detection
  • Performing Graphic Operations with a Graphics Object
  • Creating Gradients
  • Collision Detection
  • Proximity Algorithms
  • Optimizing the Number of Calculations
  • Extending the Algorithms to Add a Third Dimension
  • Develop a Real Game Proposal
  • Diagrams of Basic Game Objects
  • The Game Engine
  • The Coding Phase
  • Testing the Program
  • The Block Class
  • The Constructor
  • The Down, Right, and Left Methods
  • The Rotate Method
  • The Show and Hide Methods
  • Final Version: Coding the GameField Class and the Game Engine
  • The CheckLines Method
  • The Game Engine
  • Adding the Final Touches
  • 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


    Game Development of .Nettrix: GDI+ and Collision Detection - The Coding Phase


    (Page 11 of 22 )

    When coding any project, it’s always useful to create drivers and stubs to allow you to test each component separately. Drivers are programs that control other lower-level programs, and stubs are programs that mimic low-level programs’ behavior, allowing the testing of higher level code. To provide a vision of a real coding phase, you’ll sometimes use such techniques to validate the code written step by step.

    You’ll go through three versions, from your first draft to the final code:

    1. First draft: Code the Square class.

    2. Second draft: Code the Block class.

    3. Final version: Code the GameField class and the game engine.

    You start coding from the lowest level class, Square, in the next section.

    First Draft: Coding the Square Class

    Reviewing the game project, you find the basic structure of the class and create the public class interface.

    public class Square {
       public Point Location;
       public Size Size;
       public Color ForeColor;
       public Color BackColor;

       public void Show(System.IntPtr WinHandle) {}
       public void Hide(System.IntPtr WinHandle) {}
    }

    The class methods are shown in the next section.

    The Show and Hide Methods

    In the Show method all you need to do is to adapt the code for creating a path gradient rectangle you saw in the previous section. For the Hide method, you can hide the rectangle in an easier way: Since you’ll be working with a one-color background (no textures or bitmaps yet), you can simply draw the rectangle again, this time using a solid color, the same as the background.

    To create a generic code that can be updated later by any programmer, it’s always a good idea to not use fixed values inside your program. In this example, you’d better read the game field background color from some variable, so that if it’s updated later to another color, your Hide method will still work. This color value should be a property of the GameField class, but since this property doesn’t appear in your game project, you’ll need to update it with this new property. In a real project it’s common for some details (like this one) to only become visible at the coding phase, since it’s usually not possible for the project to predict all possible details.

    The code for the Square class is shown here:

    public class Square {
      public Point Location;
      public Size Size;
      public Color ForeColor;
      public Color BackColor;

      // Draws a rectangle with gradient path using the properties above.
      public void Show(System.IntPtr WinHandle)
      {
        Graphics GameGraphics;
        GraphicsPath graphPath;
        PathGradientBrush brushSquare;
        Color[] surroundColor;
        Rectangle rectSquare;

        // Gets the Graphics object of the background picture.
        GameGraphics = Graphics.FromHwnd(WinHandle);

        // Creates a path consisting of one rectangle.
        graphPath = new GraphicsPath();
        rectSquare = new Rectangle(Location.X,Location.Y,Size.Width,Size.Height);
        graphPath.AddRectangle(rectSquare);

        // Creates the gradient brush that will draw the square.
        // Note: There's one center color and an array of border colors.
        brushSquare = new PathGradientBrush(graphPath);
        brushSquare.CenterColor = ForeColor;
        surroundColor = new Color[]{BackColor };
        brushSquare.SurroundColors = surroundColor;

        // Finally draws the square.
        GameGraphics.FillPath(brushSquare, graphPath);
      }
      public void Hide(System.IntPtr WinHandle)
      {
        Graphics GameGraphics;
        Rectangle rectSquare;

        // Gets the Graphics object of the background picture.
        GameGraphics = Graphics.FromHwnd(WinHandle);

        // Draws the square.
        rectSquare = new Rectangle(Location.X,Location.Y,Size.Width,Size.Height);
        GameGraphics.FillRectangle(new SolidBrush(GameField.BackColor), rectSquare);

      }
    }
     

    Note:  In the Hide method shown previously, you can see an unusual use of the BackColor property: You are using the property directly from the class definition, instead of from a previously created object in this class. In this case, you are using a new feature of .NET: static properties and methods. Defining a method or a property as public static makes it available for any part of the program directly from the class name, without the need for explicitly creating an object. An important point is that the property or method is shared by all the instances of the objects created from the class. For example, you can have a static counter property that each object increments when it’s created and decrements when it’s destroyed, and any object can read this counter at any time in order to see how many objects are available at any given time.

    This chapter is from Beginning .NET Game Programming in C#, by David Weller, et al., (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today.

    Buy this book now.

    More .NET Articles
    More By Apress Publishing


       · Examples from the first page are invalid. You cannot do:Graphics g = new...
     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





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