Last night, Reuben Ahmed of Bennett Adelson gave a presentation to the .NET SIG on XNA Game Studio 2.0. He took us through a simplified game creation process, creating a basic Invasion of Alien Lifeform (think “space invaders”) clone in a little over 2 hours.

Reuben kept relating the stages to Mario and the various Mario games, which got me excited, as I’ve always been a fan of Nintendo’s Mario games. From the old school NES days to Mario Kart on SNES and higher, I’ve always enjoyed a good Mario game. So this made his presentation even easier to relate to.

One of the first things he pointed was that XNA made games programming easy for the hobbyists. That means that those of us who didn’t get into hardcore Direct X programming and games programming actually could make our own games.

XNA, according to his presentation, stands for X-Platform Next Generation Architecture.  According to the XNA Frequently Asked Questions on MSDN, XNA is one of those recursive acronyms like GNU (GNU’s Not Unix) and PINE (PINE Is Not Elm), and it stands for XNA Not Acronymed. Either way, XNA is a really cool tool. It’s a game framework for C#, which is weird considering that a lot of game programming is done in C++ rather than C#.

To use XNA, you need at least Visual C# 2005 Express. It also can work with Visual Studio 2005, but due to the timing of its release, it does not currently work with Visual Studio 2008. The other key requirements are Direct X and Pixel Shader. You can tell which version of Direct X you have by running the command dxdiag from a Run prompt in Windows. There was uncertainty to how to tell if Pixel Shader is present. I found an XNA Requirements Checker Program while doing a search on XNA and Pixel Shader. XNA will work on Windows XP and Vista.

So what are some of the things that XNA has to offer?

  • Optimized game loop: A game loop is the sequence of sprites that are used for a jump or for moving forward or any other form of movement. XNA provides Update and Draw methods to handle this.
  • Content pipeline: This is an easy way to insert graphics, audio, and other assets for your game.
  • Can make games run on Windows and XBOX360: There’s a cost for XBOX360 that will get mentioned later.

At this point, Reuben put started the code portion of his demo. When you create a new game project in your development environment, you can run it with the default code and a windowed blue screen should show up. I find it funny that they use a blue screen by default – since when has a blue screen really been a good thing?

The next part of XNA that he got into is graphics and textures. Just like adding code files to App_Code, in XNA, your graphics and textures have their own special folder called Content. This folder is used with the built-in content pipeline, to make it extremely simple to use images in games programming. First, you add the assets to the Content folder. Then you need to load the graphic in a variable. Since his demonstration focused on jpg or gif files, he used the Texture2D class. Once a graphic is loaded into a variable, its position can be updated within the Update() method, and it can be drawn via the Draw() method.

There are certain factors that have to be taken into consideration with placing images. On a 2D level, graphics are placed based on the upper left corner. So keeping that in mind, it makes sense that when you are determining boundaries, you have to keep the image’s width and height in mind. If these are forgotten, then it’s very easy to place an image off the screen. Another thing to consider is something called overscan. When you are developing a game for the XBOX, you need to take this into consideration. Finally, keep in mind that the 2D vector system is a positive number system, where (0,0) is the upper left corner of the Viewport. Negative coordinates will render an object off the screen. Although the object wouldn’t be visible, it would still get loaded into memory.

After adding graphics, Reuben showed us how to tint images, so that player 1 could be a yellow ship and player 2 could be a different colored ship. For those of us who are Photoshop-challenged, this is actually a very simple, painless process.

From graphics and textures, he moved on to user input. With XNA programming, you can program for Keyboard input and XBOX Gamepad input at the same time, via enumerators. So for example, let’s go back to an old-school game – Wolfenstein 3D. There was a cheat code for it, where M-L-I had to be pressed simultaneously. Programming this in XNA, would look something like this:

if (Keyboard.GetState().IsKeyDown(Keys.M) && Keyboard.GetState().IsKeyDown(Keys.L) && Keyboard.GetState().IsKeyDown(Keys.I)){
// Give uber hacks here


In the demonstration, Reuben showed how to bind the left and right keys to move a ship (a graphic already displayed on the screen). The movement was done through simple vector addition and subtraction.

Another thing that can be controlled is the rumbling of the Gamepad. There are two motors in it – a low motor and a high motor. Through the Gamepad enumerator, you can call a SetVibration() method.

In the demonstration, an alien was added to the program, and this alien moved across the screen. Thanks to the MathHelper.Clamp() method, the alien was constrained to the Viewport. This particular method is used for setting boundaries, as it “clamps” a number within a range. So if you want a number to stay between 0 and 50, this would return 50 if the value exceeds 50.

Once the alien was added, it was time to add bullets and logic for hitting the alien.
As Reuben warned us at the beginning of the presentation, he was programming for the fun of it, not necessarily taking any best practices into consideration. So for his collision detection – determining whether two objects have collided – he used the rectangle method. Basically, draw rectangles around the objects that you want to test, and then use the Rectangle.Intersects() method to determine whether there’s a collision. The problem in doing this is that the rectangles also get loaded into memory. With games development, especially at a 3D level, you have to keep memory usage in mind. Other collision detection approaches that were suggested include per-pixel collision detection (determining collision detection at an exact pixel point) and color collision detection (based on a color map).

Writing text was included. From a simple “Hello World” to a hit counter, it was a simple call to write.

The last detail before packaging and distribution was adding audio to the game. In order to do this, you need to create an audio project with the Microsoft Cross Platform Audio Creation Tool (XACT). The interface allows the simple drag’n’drop familiarity for adding WAV files to wave banks and sound banks, in order to get played in the game. This tool creates an XAP file, which then can get added into the game code similarly to adding graphics. The MSDN Audio Overview explains how this works.

Now once everything is put together and you have a game that works, you definitely want to share it with your friends, right? If you’re doing a Windows game, you’re in luck. There are no fees for redistributing your game. Using a tool called XNA Pack, you can package your game into a redistributable executable file.

Writing games for the XBOX comes at a cost. In order to even debug your game for the XBOX, you need to have an XNA Creators Club license, which runs $49/4 months or $99/year (as of this posting). The other problem with writing games for the XBOX is that you need an active Internet connection, as the XBOX has to go out and validate the license. Reuben was not able to show us this part, as he could not get an Internet connection out for his XBOX.

The last thing he showed was a sample of what could be done with XNA. This particular program was written within 1 week. Here it is:

Whether it’s a space shooting game or some game to promote your business, XNA can be a useful tool. Even those of us with little to no game programming can get into using XNA!

Some links to check out include:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.