.NET
  Home arrow .NET arrow Page 19 - 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 CheckLines Method


    (Page 19 of 22 )

    In the next GameField method, CheckLines, you need to check if a line is totally filled (all bits set) and, if so, erase this line and move down all the lines above it. You don’t need to copy the empty lines (all bits reset) one on top of another, but you must return the number of cleared lines. To improve the readability of your code, you define some private constants for the class.

    private const int bitEmpty = 0x0;   //00000000 0000000
    private const int bitFull = 0xFFFF; //11111111 1111111

    See the comments in the code and the following explanation to understand the function.

    public static int CheckLines() {

      int CheckLines_result = 0; // Returns the number of lines completed.
     
    int y = Height - 1;

      while ( y >= 0) {
       // Stops the loop when the blank lines are reached.
       if (arrBitGameField[y]==bitEmpty) y = 0;
       // If all the bits of the line are set, then increment the
       // counter to clear the line and move all above lines down.
       if (arrBitGameField[y]==bitFull) {
         // Same as: if ((arrBitGameField(y) ^ bitFull) = 0
         CheckLines_result++; 
        

         // Move all next lines down. 
         for(int index = y; index >= 0; index--) { 
           // If the current line is NOT the first of the game field, 
           // copy the line above. 
           if (index>0) {
             // Copy the bits from the line above.
             arrBitGameField[index] = arrBitGameField[index-1];

             // Copy each of the squares from the line above. 
             for(int x=0; x<Width; x++) {
               // Copy the square.
               arrGameField[x, index] = arrGameField[x, index-1]; 
               // Update the Location property of the square. 
               if (arrGameField[x, index] != null) 
                arrGameField[x, index].location = 
                 new Point(arrGameField[x, index].location.X, 
                arrGameField[x, index].location.Y+SquareSize);
              }
            } 
            else {
              //
    If the current line is the first of the game field 
              // just clear the line. 
              arrBitGameField[index] = bitEmpty;
              for(int x=0; x<Width; x++) { 
                arrGameField[x, index] = null;
              }
            }
          }
        } 
        else {
          y--;
        }
      }
      return CheckLines_result;
    }

    In the CheckLines method, you can see the real benefits of creating arrBitGameField for collision detection: You can check if a line is completely filled or empty with only one test, with the use of bitFull and bitEmpty constants you previously created, avoiding the 16 tests you would have had to create for each of the ArrGameField members in a line. The next code listing highlights these tests:

    if (arrBitGameField[y]==bitFull)  //The line is full.
    if (arrBitGameField[y]==bitEmpty) //The line is empty.

    The next section discusses the last two methods for the GameField class.

    The StopSquare and Redraw Methods

    The last two methods, StopSquare (which sets the arrays when a block stops falling) and Redraw (which redraws the entire game field), have no surprises. The code implementing these methods is shown in the next listing:

    public static void StopSquare(Square Square, int x, int y) {
    arrBitGameField[y] = arrBitGameField[y] | (1<<x);
    arrGameField[x, y] = Square;

    }

    public static void Redraw() {
      for(int y=Height-1; y>=0; y--)
        if (arrBitGameField[y]!=bitEmpty)
          for(int x=Width-1; x>=0; x--)
            if (arrGameField[x, y] != null) arrGameField[x, y].Show(WinHandle);
    }

    The next section shows the code for the final version of the main program, finishing your game code.

    This chapter&, 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 3 hosted by Hostway
    Stay green...Green IT