.NET
  Home arrow .NET arrow Page 14 - 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 Constructor


    (Page 14 of 22 )

    The constructor will receive two parameters: the block type and the location where the block will be created. Since you need random block types, you can pass an Undefined value for the block type when you want to randomly create a block.

    You might wonder why you allow anything other than Undefined for the block type in the first place, since during gameplay the blocks are randomly generated. The reason is that it makes testing far easier—you can test specific block types as you build up your game, giving you more control over incrementally testing the game. The code to do this is shown in the following listing:

    public Block(Point location, BlockTypes newBlockType) {
    //Create the new block, choose a new type if necessary.
    if (newBlockType==BlockTypes.Undefined) {
    BlockType = (BlockTypes)(random.Next(7)) + 1;
    }
    else {
    BlockType = newBlockType; }
    // Create each of the squares of the block.
    // Set the square colors, based on the block type.
    square1 = new Square(new Size(squareSize, squareSize),
    backColors[(int)BlockType], foreColors[(int)BlockType]);
    square2 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
    square3 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);
    square4 = new Square(new Size(squareSize, squareSize), backColors[(int)BlockType], foreColors[(int)BlockType]);

    // Set the square positions based on the block type.
    switch(BlockType) {
      case BlockTypes.Square:
        // Create a Square block….
        break;
      case BlockTypes.Line:
        // Create a Line block….
        break;
      case BlockTypes.J:
        // Create a J block….
        break;
      case BlockTypes.L:
        // Create an L block….
        break;
      case BlockTypes.T:
        // Create a T block….
        break;
      case BlockTypes.Z:
        // Create a Z block…. 
        break;
      case BlockTypes.S:
        // Create an S block….
        break;
      }
    }

    In this sample, the code inside each case statement must set the square positions, based on each block type, according to Figure 1-24. For example, analyze the Square block type, depicted in Figure 1-25.


    Figure 1-25. The squares for the Square block type

    The code for creating the Square block type is shown here:

    case BlockTypes.Square:
      square1.Location = new Point(Location.X, Location.Y);
      square2.Location = new Point(Location.X+squareSize, Location.Y);
      square3.Location = new Point(Location.X, Location.Y+squareSize);
      square4.Location = new Point(Location.X+squareSize, Location.Y+squareSize);
       break;

    As for the Line block type, the squares that compose it are shown in Figure 1-26.


    Figure 1-26. The squares for the Line block type

    The code for the Line block type is as follows:

    case BlockTypes.Line:
      square1.Location = new Point(Location.X, Location.Y);
      square2.Location = new Point(Location.X, Location.Y+squareSize);
      square3.Location = new Point(Location.X, Location.Y+2*squareSize);
      square4.Location = new Point(Location.X, Location.Y+3*squareSize);
      break;

    The code for the other blocks follows the same idea. For the full code of the constructor, check the downloadable source code. Once the blocks are created, you can start coding the moving operations over them, as described 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

    - 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