MULTIMEDIA

Microsoft XNA Game Studio 3.0 : Making a Game Program

2/5/2011 9:59:47 AM
To see how a game program can produce a display, you need to look inside one of the C# programs that XNA built. You used XNA Game Studio to create a game program. Now you are going to look at this program and discover how it works.

The file that contains the game behavior is called Game1.cs. The name Game1 was generated automatically when the project was created; the .cs part is the file extension for C# programs. If you want to look inside this file, start XNA Game Studio and open the file from Solution Explorer. You can find Solution Explorer, shown in Figure 1, in the top right corner of the XNA Game Studio screen. If you double-click the name of the file that you want to work with, the file opens in the editing window.

If you look at the content of Game1.cs, which drew that impressive blue screen, you can see how the program works. The program code that XNA Game Studio created when you made an empty game contains the following method:

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here

base.Draw(gameTime);
}

Figure 1. Solution Explorer


A method is a named part of a program. In this case, the method has the name Draw (you can ignore the protected override void part for now). All you need to know at the moment is that when XNA wants to draw the screen, it uses this method. You can change what gets drawn by altering the content of this method. At the moment, we just get a blue screen; if you look at the second line of the preceding code, you can see where the blue screen comes from.

1. Statements in the Draw Method

The Draw method contains a block of statements. C# programs are expressed as a series of statements that are separated by a semicolon (;). Each statement describes a single action that your program needs to do. There are a number of different kinds of statements; you discover new ones as you learn more about programming. The statements are organized into a single block. A block is a way to lump statements together. The start of a block is marked with an open curly bracket character ({ )and the end of the block is marked with a closing curly bracket (}). These curly kinds of brackets are sometimes called braces. The C# compiler, which is trying to convert the program text into something that can actually run, notices and complains if you use the wrong kind of bracket.

In the preceding code, there is also a comment. Comments are ignored by the compiler; they let you put text into your program to describe the program or to remind you to do things. In the preceding code, the comment is a "TODO," which tells programmers that they need to do something. In this case, a programmer must add drawing statements at that position in the program file. The compiler can tell that the text is a comment because it starts with the character sequence //. For instance, look at the following example:

// This is a comment. It can be any text.

You can add comments anywhere in your program.


Note:

Our Great Programmer likes comments. She says that a well-written program is like a story in the way that the purpose of each part is described. She says that she will be looking at our code and making sure that we put the right kind of comments in.


From the point of view of changing the color of your screen, the statement that is most interesting is this one:

GraphicsDevice.Clear(Color.CornflowerBlue);

Clear is a method that is part of XNA. You will see precisely how it fits into the framework later; for now, all you need to know is that the Clear method is given something that describes a color, and the method clears the screen to that color. At the moment, you are sending the Clear method the color CornflowerBlue, and it is clearing the screen to be that color. If you want a different color, you just have to send a different value into Clear:

GraphicsDevice.Clear(Color.Red);

If you change the color as shown in the preceding line and run the program, you should see that the screen is now set to red.

Other  
  •  iPhone 3D Programming : Textures and Image Capture - Texture Compression with PVRTC
  •  iPhone 3D Programming : Textures and Image Capture - Texture Formats and Types
  •  iPhone 3D Programming : Textures and Image Capture - Fight Aliasing with Filtering
  •  iPhone 3D Programming : Textures and Image Capture - Texture Coordinates Revisited
  •  Programming with DirectX : Additional Texture Mapping - Sprites
  •  Programming with DirectX : Additional Texture Mapping - Alpha Mapping
  •  Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 2) - Running the Same XNA Game on Different Devices
  •  Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 1)
  •  Programming with DirectX : Shading and Surfaces - Additional Texturing Topics
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 4) - Enabling Textures with ES2::RenderingEngine
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 3) - Enabling Textures with ES1::RenderingEngine
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 2) - Generating Texture Coordinates
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 1) - Enhancing IResourceManager
  •  Programming with DirectX : Shading and Surfaces - Implementing Texture Mapping (part 2) - Multi Texture Demo
  •  Programming with DirectX : Shading and Surfaces - Implementing Texture Mapping (part 1) - 2D Texture Mapping Demo
  •  Building Out Of Browser Silverlight Applications - Using COM Interoperability and File System Access
  •  Building Out Of Browser Silverlight Applications - Controlling the Application Window
  •  iPhone 3D Programming : Adding Depth and Realism - Loading Geometry from OBJ Files
  •  iPhone 3D Programming : Adding Depth and Realism - Better Wireframes Using Polygon Offset
  •  Programming with DirectX : Textures in Direct3D 10 (part 2)
  •  
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Bamboo Splash - Powerful Specs And Friendly Interface
    Powered By Windows (Part 2) - Toshiba Satellite U840 Series, Philips E248C3 MODA Lightframe Monitor & HP Envy Spectre 14
    MSI X79A-GD65 8D - Power without the Cost
    Canon EOS M With Wonderful Touchscreen Interface (Part 1)
    Windows Server 2003 : Building an Active Directory Structure (part 1) - The First Domain
    Personalize Your iPhone Case
    Speed ​​up browsing with a faster DNS
    Using and Configuring Public Folder Sharing
    Extending the Real-Time Communications Functionality of Exchange Server 2007 : Installing OCS 2007 (part 1)
    Google, privacy & you (Part 1)
    iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
    Microsoft Surface With Windows RT - Truly A Unique Tablet
    Network Configuration & Troubleshooting (Part 1)
    Panasonic Lumix GH3 – The Fastest Touchscreen-Camera (Part 2)
    Programming Microsoft SQL Server 2005 : FOR XML Commands (part 3) - OPENXML Enhancements in SQL Server 2005
    Exchange Server 2010 : Track Exchange Performance (part 2) - Test the Performance Limitations in a Lab
    Extra Network Hardware Round-Up (Part 2) - NAS Drives, Media Center Extenders & Games Consoles
    Windows Server 2003 : Planning a Host Name Resolution Strategy - Understanding Name Resolution Requirements
    Google’s Data Liberation Front (Part 2)
    Datacolor SpyderLensCal (Part 1)