.NET
  Home arrow .NET arrow Page 3 - 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 - Creating Gradients


    (Page 3 of 22 )

    In the previous section, you saw some code samples used to create solid red rectangles via a SolidBrush object. GDI+ allows the programmer to go beyond flat colors and create linear and path gradients, using special gradient brushes that provide very interesting effects.

    GDI+ has features to create horizontal, vertical, and diagonal linear gradients. You can create linear gradients in which the colors change uniformly (the default behavior), or in a nonuniform way by using the Blend property of the gradient brush.

    The sample code here shows how to create a uniform gradient brush and draw a rectangle with color changing from red to blue from the upper-left to the lower-right vertex:

    Graphics graph; Drawing2D.LinearGradientBrush linGrBrush;
    graph = Graphics.FromHwnd(picSource.Handle);
    linGrBrush = new Drawing2D.LinearGradientBrush(
    new Point(10, 20), // Start gradient point.
    new Point(23, 27), // End gradient point.
    Color.FromArgb(255, 255, 0, 0), // Red
    Color.FromArgb(255, 0, 0, 255)) // Blue
    graph.FillRectangle(linGrBrush, 10, 20, 13, 7);

    Note:  The most important part of this sample code is the color definition using the FromArgb method of the Color object. As you can see, each color in GDI+ is always defined by four values: the red, green, blue (RGB) values used by the classic GDI functions, plus the alpha (A) value, which defines the transparency of the color. In the preceding example, you use an alpha value of 255 for both col ors, so they will be totally opaque. Using a value of 128, you create a 50 percent transparent color, so any graphics below are shown through the rectangle. Setting alpha to zero means that the color will be 100 percent transparent, or totally invisible. The in-between values allow different degrees of transparency.

    Path gradients allow you to fill a shape using a color pattern defined by a specified path. The path can be composed of points, ellipses, and rectangles, and you can specify one color for the center of the path and a different color for each of the points in the path, allowing the creation of many different effects.

    To draw an image using gradient paths, you must create a PathGradientBrush object, based on a GraphicsPath object that is defined by a sequence of lines, curves, and shapes. The code here shows how to draw the same rectangle from the previous examples, using a gradient that starts with a green color in the center of the rectangle and finishes with a blue color at the edges:

    Graphics graph;
    Rectangle rectSquare;
    Drawing2D.GraphicsPath graphPath;
    Drawing2D.PathGradientBrush brushSquare;

    graph = Graphics.FromHwnd(picSource.Handle);

    // Create a path consisting of one rectangle.
    graphPath = new Drawing2D.GraphicsPath();
    rectSquare = new Rectangle(10, 20, 23, 27);
    graphPath.AddRectangle(rectSquare);
    brushSquare = new Drawing2D.PathGradientBrush(graphPath);
    brushSquare.CenterColor = Color.FromArgb(255, 0, 255, 0);
    brushSquare.SurroundColors = new Color(){Color.FromArgb(255, 0, 0, 255)};

    // Create the rectangle from the path.
    Graph.FillPath(brushSquare, graphPath);

    Note:  We won’t go into much detail here about brushes and paths. Refer to the .NET SDK documentation for some extra examples about how to use these features. For a complete overview about this topic, look for “System.Drawing.Drawing2D Hierarchy” in the online help.

    In the next section we’ll discuss collision detection, after which you’ll have an understanding of all the basic concepts you need to implement your first 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

    - 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 4 hosted by Hostway
    Stay green...Green IT