.NET
  Home arrow .NET arrow Page 10 - 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 Game Engine


    (Page 10 of 22 )

    Using the C# events jargon, you can think about coding three main events to implement the behaviors described at the game proposal:

    1. When the form loads, you can create the first block.

    2. At the form KeyPress event, you can handle the keyboard input from the user.

    3. With a timer you can call the Down method at each clock tick, producing the desired falling effect for the blocks. As you’ll see later, using a timer isn’t a recommended practice when creating games that need to run at full speed, but that’s not the case here.

    Writing pseudo-code is helpful for validating the class diagram, checking whether you use every method and property, and determining whether you can achieve the results stated in the game proposal with those class members. The pseudo-code for your game is shown in the following code sample:

    Form_Load Creates an object (named currentBlock) of block class

    You’ll use the currentBlock object in all other events, so it must have the same scope as the form.

    Form_KeyPress
      If Left Arrow was pressed, call Left method of currentBlock
      If Right Arrow was pressed, call Right method of currentBlock
      If Up Arrow was pressed, call Rotate method of currentBlock
      If Down Arrow was pressed, call Down method of currentBlock

    In the previous pseudo-code, you use the up arrow key to rotate the block and the down arrow key to force the block to go down faster, while the right arrow key and left arrow key move the block in the horizontal direction.

    The game engine core will be the timer event. Reviewing the game proposal, you probably see what you must do here: Make the block fall, stop it according to the game rules, check to see if there are any full horizontal lines, and check for the game being over. Possible pseudo-code to do this is shown in the following sample:

    If there is no block below currentBlock,
      and the currentBlock didn't reach the bottom of the screen then
        Call the Down method of currentBlock
    Else
        Stop the block
        If it's at the top of the screen then
        The game is over
        If we filled any horizontal lines then
          Increase the game score
          Erase the line
        Create a new block at the top of the screen

    Analyzing this code, you may see some features your current class diagram doesn’t take into account. For instance, how can you check if there is no block below the current block? How can you erase the horizontal line you just managed to fill? We’ll discuss these points in the next section.

    The Class Diagram: Final Version

    In order to check the previous block positions to see if there are any blocks below the current block or if there are any filled lines, you must have a way to store and check each of the squares of the block, independently of the original blocks (remember, when you erase a line, you can erase just a square or two from a given block). You can do this by creating a new class representing the game field, which will store the information for all squares and have some methods that allow line erasing, among other features. With a quick brainstorm, you can add this class to your model, which will evolve into the diagram shown in Figure 1-22.


    Figure 1-22. The final class diagram

    Table 1-3 lists the methods and properties of the new class, along with a short description for each one.

    Table 1-3. The Game Field Class Members

    TYPE NAME DESCRIPTION
    Properties Width and Height Represents the width and height of the game field, measured in squares.
     
    Property SquareSize Indicates the size of each square, so you can translate pixels to squares.
    Property ArrGameField Constitutes an array to store all the squares from all the blocks that stopped falling.
    Method CheckLines Checks if there are any complete horizontal lines, erasing them if so, and returns the number of erased lines so the main program can increase the player’s score.
    Method IsEmpty Checks if the square at a particular location (a given x and y) is empty, therefore telling you when a block is in motion.
    Method Redraw Forces the full redraw of the game field. This will be used when a line has been erased or when another window has overlapped yours.

    In a real project, you would possibly go beyond this point, refining all methods to include their interfaces (received parameters and return values) and specifying the data types for the properties, which would probably lead to another revision of your class diagram. But we’ve given you the basic idea here, and that’s the main point.

    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

    - 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...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT