.NET
  Home arrow .NET arrow Page 17 - 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 Show and Hide Methods


    (Page 17 of 22 )

    The implementation of the Show and Hide methods is very straightforward; the Show and Hide methods are called for each of the block squares, as shown here:

    public void Show(System.IntPtr WinHandle) {
    // Draws each square of the block on the game field.
      square1.Show(WinHandle);
      square2.Show(WinHandle);
      square3.Show(WinHandle);
      square4.Show(WinHandle);
    }

    public void Hide(System.IntPtr WinHandle) {
    // Hides each square of the block on the game field.
      square1.Hide(WinHandle);
      square2.Hide(WinHandle);
      square3.Hide(WinHandle);
      square4.Hide(WinHandle);
    }

    To see the full code for the Block class, refer to the samples in the downloadable source code. To test your new class, you’ll have to create a new stub for the GameField class and update your main program, as shown in the next section.

    Testing the Block Class

    The new stub for the GameField class must include the properties and methods accessed by the Block class, as shown in the next code listing:

    public class GameField {
      public static System.IntPtr WinHandle;
      public static Color BackColor;

      public static bool IsEmpty(int x, int y) { 
        return true;
      }

      public static void StopSquare(Square Square, int x, int y) {
      }

    The IsEmpty method always returns True; you’ll add code for IsEmpty and StopSquare in the final version of the program. The next code listing shows the logic for testing the Block class, and must be included in the game field form:

    private Block CurrentBlock;

    private void NetTrix_Load(object sender, System.EventArgs e) {
    // Set the properties of GameField class.
      GameField.BackColor = PicBackground.BackColor;
      GameField.WinHandle = PicBackground.Handle;
    }

    private void CmdStart_Click(object sender, System.EventArgs e) {
      CurrentBlock = new Block(new Point(GameField.SquareSize * 6, 50), Block.BlockTypes.Undefined);
      CurrentBlock.Show(PicBackground.Handle);
      }
    private void NetTrix_KeyDown(object sender,  System.Windows.Forms.KeyEventArgs e) {
      switch(e.KeyCode) {
        case Keys.Right: CurrentBlock.Right();break;
        case Keys.Left : CurrentBlock.Left();break;
        case Keys.Up : CurrentBlock.Rotate();break;
        case Keys.Down : CurrentBlock.Down();break;
        default: break;
      }
    }

    Note:  All the constants in the .NET Framework are organized within enumerations. This approach allows a more intuitive orga nization, so it’s easier to find exactly what you need in the help feature. The intelligence of Visual Studio was also improved, giving more hints and softening the learning curve. In the preceding sam ple code, you use the Keys enumeration to get the key code (Left, Down, Up, and Right). There are also modifiers to test if Shift, Ctrl, and Alt keys are pressed. The namespace for the Keys enumeration can be found in System.Windows.Forms.

    To test the program, just run it, click the Start button, and press the various keys to move the blocks: The down arrow key makes the block go down, the up arrow key rotates the block, and the right arrow and left arrow keys move the block horizontally. Clicking the Start button again will create a new block, so you can test the random creation of different block types. A sample screen is shown in Figure 1-31.


    Figure 1-31. Testing the Block class

    In the next section you’ll implement the collision detection and the main program logic, finishing your game.

    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