.NET
  Home arrow .NET arrow Page 15 - 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  
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? 
.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 / 8
    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 Down, Right, and Left Methods


    (Page 15 of 22 )

    The next methods, following the class diagram order, are Down, Right, and Left. These methods are fairly simple, since all you need to do is to update the block position in the defined direction, regardless of the block type. The basic code for the Down procedure could be as simple as this:

    public bool Down() {
      // Hide the block (in the previous position).
      Hide(GameField.WinHandle);
      // Update the block position.
      square1.location = new Point(square1.location.X,
      square1.location.Y+squareSize);
      square2.location = new Point(square2.location.X, square2.location.Y+squareSize);
      square3.location = new Point(square3.location.X, square3.location.Y+squareSize);
      square4.location = new Point(square4.location.X,
    square4.location.Y+squareSize);
      // Draw the block in the new position.
      Show(GameField.WinHandle);
      return true;
    }

    Because you need to hide and redraw the block every time these methods are called, you can reduce the calling overhead by creating a new static property on the GameField class, the WinHandle, which was used in the preceding code.

    This handle is a copy of the handle of the PicBackground, which is used as the game field on the form. With this approach, you can set this property in the constructor and use it for every drawing operation, instead of passing the handle as a parameter to the drawing methods every time it’s called.

    The Right and Left methods will be similar to this one, except this time the horizontal block position is changed—incremented to move the block to the right and decremented to move the block to the left. You move the blocks using the default value of the SquareSize property, assigned to 10 in the class definition. This means that the blocks will always move a square down, left, or right, so you don’t have to worry about the square’s alignment.

    There’s one more detail to include in this procedures: the test for collision detection. The block can’t move down, left, or right if there are any squares (or screen limits) in the way. Since the block itself can’t know if other blocks are in the way, it must ask the GameField class if it can move this way. This is already considered in the game project: The IsEmpty method of the GameField class will check if a specified square in the game field is empty.

    In the Down method, you must check if there are any blocks in the way and stop your block from falling if it hits an obstacle. When the block stops falling, you must inform the GameField class of this, so it can update its internal controls to allow the proper function of the IsEmpty method. You can do this by creating a new method, named StopSquare, which will inform the GameField that a specific square is now not empty, and pass the square object and its coordinates as parameters. After that, each square will be treated separately from each other (no more blocks) by the GameField class, because when a line is removed, some squares of the block can be removed while others remain.

    Since the IsEmpty and StopSquare methods are based on an array of Squares, ArrGameField (as defined in your game project), the logical approach is for these methods to receive the array coordinates to be used. You can translate screen coordinates to array positions by simply dividing the x and y position of each square by the square size.

    The final code for the Down procedure will now be as follows:

    public bool Down() {
      // If there's no block below the current one, go down.
      if (GameField.IsEmpty(square1.location.X/squareSize,
    square1.location.Y/squareSize+1) &&
       GameField.IsEmpty(square2.location.X/squareSize,  square2.location.Y/squareSize+1) &&
       GameField.IsEmpty(square3.location.X/squareSize, square3.location.Y/squareSize+1) &&
       GameField.IsEmpty(square4.location.X/squareSize, square4.location.Y/squareSize+1)) { 
          // Hide the block (in the previous position).
          Hide(GameField.WinHandle);

        // Update the block position.
        square1.location = new Point(square1.location.X, square1.location.Y+squareSize);
        square2.location = new Point(square2.location.X, square2.location.Y+squareSize);
        square3.location = new Point(square3.location.X, square3.location.Y+squareSize);
        square4.location = new Point(square4.location.X,
    square4.location.Y+squareSize); 
        // Draw the block in the new position. 
        Show(GameField.WinHandle);
        return true;
      }
      else {
        // If there's a block below the current one, doesn’t go down.
        // -> Put it on the array that controls the game and return FALSE. 
        GameField.StopSquare(square1,square1.location.X/squareSize,
    square1.location.Y/squareSize);
        GameField.StopSquare(square2,square2.location.X/squareSize, square2.location.Y/squareSize);
        GameField.StopSquare(square3,square3.location.X/squareSize, square3.location.Y/squareSize);
        GameField.StopSquare(square4,square4.location.X/squareSize, square4.location.Y/squareSize);
        return false;
      }
    }

    In this code sample, you use the GameField class again with static methods (no objects created). The concepts of static properties and methods were explained earlier in this chapter.

    The Right and Left methods are very similar to this one, with the slight difference that you don’t stop the block if it can’t go right or left. The code for the Right method is shown next. The Left method is built upon the same basic structure.

    public bool Right() {
      // If there's no block to the right of the current one, go right.
      if (GameField.IsEmpty(square1.location.X/squareSize+1,
    square1.location.Y/squareSize) &&
      GameField.IsEmpty(square2.location.X/squareSize+1, square2.location.Y/squareSize) &&
      GameField.IsEmpty(square3.location.X/squareSize+1, square3.location.Y/squareSize) &&
      GameField.IsEmpty(square4.location.X/squareSize+1, square4.location.Y/squareSize)) {
        // Hide the block (in the previous position).
        Hide(GameField.WinHandle);
        // Update the block position.
        square1.location = new Point(square1.location.X+squareSize,
    square1.location.Y);
        square2.location = new Point(square2.location.X+squareSize, square2.location.Y);
        square3.location = new Point(square3.location.X+squareSize, square3.location.Y);
        square4.location = new Point(square4.location.X+squareSize, square4.location.Y);
        // Draw the block in the new position.
        Show(GameField.WinHandle)
        return true;
      }
      else {
        // If there's a block to the right of the current one,
        // don't go right and return FALSE. return false;
      }
    }

    The next method for the Block class, Rotate, is a little more complicated, so we’ll give you a closer look at it in the next section.

    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

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek