.NET
  Home arrow .NET arrow Page 21 - 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 - Adding the Final Touches


    (Page 21 of 22 )

    After playing the first version of .Nettrix for a few minutes, every player will miss two important features present in almost every Tetris type of game: a feature to show the next block that will appear, and some way to pause the game, for emergency situations (like your boss crossing the office and heading in your direction).

    Now that you have all base classes already finished, this is easily done. The next sections discuss these and some other features to improve your first game.

    Coding the Next Block Feature

    To show the next block, you can create a new pictureBox on the form to hold the next block image, and adjust the click of the Start button and the timer_tick event. You can use the optional parameter you created on the Block constructor to create the new blocks following the block type of the next block.

    To implement this feature, you create a variable to hold the next block in the general section of the form.

    private Block NextBlock;

    At the end of the cmdStartclick event, you add two lines to create the next block.

    NextBlock = new Block(new Point(20, 10),
                    Block.BlockTypes.Undefined);
    NextBlock.Show(PicNextBlock.Handle);

    And finally you adjust the Tick event of the timer to create a new block every time the current block stops falling, and to force the CurrentBlock type to be the same as the NextBlock type.

    // Replace the current block...
    CurrentBlock= new Block(new Point(GameField.SquareSize*6,0), NextBlock.BlockType);
    CurrentBlock.Show(PicBackground.Handle);
    // Create the Next block.
    NextBlock.Hide(PicNextBlock.Handle);
    NextBlock = new Block(new Point(20,10), Block.BlockTypes.Undefined);
    NextBlock.Show(PicNextBlock.Handle);

    You can now run the game and see the next block being displayed in the picture box you’ve just created, as shown in Figure 1-33.


    Figure 1-33. Showing the next block

    The next section shows another improvement, the game pause feature.

    Coding the Game Pause Feature

    To create a pause function, all you need to do is to stop the timer when a specific key is pressed—in this case, you use the Escape (Esc) key. A simple adjustment in the KeyDown event, including an extra case clause for the Keys.Escape value, will do the trick.

    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;
        case Keys.Escape:
          tmrGameClock.Enabled = !tmrGameClock.Enabled;
          if (tmrGameClock.Enabled)
            this.Text = ".NETTrix";
          else
            this.Text = ".NETTrix -- Press 'Esc' to Continue";
          break;
        default: break;
      }
      Invalidate();
    }

    In the next section, we’ll discuss an improvement to the graphical part of your game.

    Coding the Window Redraw

    A little problem with your game is that, when the .Nettrix window is covered by other windows, the game field isn’t redrawn. You can adjust this by including a call to the GameField’s Redraw method, at the Activate event of the form (the Activate event occurs every time the form gets the focus again, after losing it to another window).

    private void NetTrix_Activated(object sender, System.EventArgs e) {
      // This event occurs when the window receives back
      // the focus after losing it to another window.
      // So, redraw the whole game field.
      PicBackground.Invalidate();
      Application.DoEvents();
      GameField.Redraw();
      if (NextBlock != null)
        NextBlock.Show(PicNextBlock.Handle);
    }

    Even using this approach there’ll be some situations when the windows won’t be redrawn properly. To achieve the best results, you should include the call to the Redraw method in the Tick event of the timer, but since it could compromise the speed of your game, keep the code as shown.

    The next section discusses some suggestions for future enhancements to 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek