If this page does not print out automatically, select Print from the File menu.

Hands on: Create a game with XNA

Try your hand at game development with Microsoft’s new free tool

Tim Anderson, Personal Computer World 27 Oct 2006

Microsoft has kick-started hobbyist game development with its free XNA Game Studio Express. XNA is a .Net Framework class library focused on game development. It is a wrapper for DirectX – the multimedia API which is implemented on both Windows and the Xbox 360.

Using XNA, you can target both Windows and the Xbox 360 easily, although deploying to an Xbox is tricky. There is also a high-end XNA Studio product aimed at game development studios, but Express enables the rest of us to play as well. Despite the multilanguage support in .Net, the only supported language for XNA is C#.

Given that .Net applications tend to be slower than their native code equivalents, XNA may seem a strange choice for game development. On the other hand, .Net applications do compile to native code at runtime, so excellent performance should be possible. The trade-off is that XNA is great for productivity. It also gives you a legal and supported way to try out your code on the Xbox 360 console; something previously impossible for hobbyist developers.

Garage Games, which makes the Torque Game Engine and other tools for game developers, is porting many of its libraries to XNA. It also claims to have ported its successful Xbox Live Arcade title, Marble Blast Ultra, to XNA without ill effect, demonstrating that performance is at least good enough.

Modern game development is often compared with making a movie, with all the expense and complexity that suggests. Clearly something like Half-Life or Grand Theft Auto is beyond the grasp of any individual, let alone a part-timer. Fortunately, the mood of the industry is changing.

The most successful aspect of the Xbox 360’s chequered launch has not been the big-budget titles, but low-budget, highly playable offerings on the Xbox Live Arcade. Some of these are puzzle games that are not particularly complex to code.

Another sign of the times is that Nintendo’s family-friendly DS handheld has been more successful than Sony’s more powerful Playstation Portable. Creative, fun ideas count for more than the ultimate in pixel-pushing power, so there is no reason to hold back from having a go.

It is easy to target Windows with XNA, but how do you run your game on an Xbox 360? This is impossible with the beta release, since Microsoft says it will not risk beta code on its console.

In the final release, you will be able to sign up for a Game Studio Express subscription at what Microsoft describes as a “nominal cost”. Then you can create projects that target the Xbox 360 and download them to a network-attached console for testing. This requires an Xbox 360 with a hard drive. You will be able to use remote debugging in the same way as for Windows Mobile devices.

Sharing binary executables with other Xbox 360 owners is prohibited. To share your game you have to distribute the source code to other XNA Game Studio Express users. That is far from ideal, especially if you want to make money from your game, but the situation may change in future. The other option is to upgrade to XNA Game Studio Professional, to be released next year, which will allow you to submit titles to Microsoft to be certified for commercial release.

XNA Game Studio Express is the easiest DirectX framework yet, but it is not a drag-and-drop visual game development tool. It is code-oriented, and you are going to need graphical and mathematical skills to make the most of it.

Given the target market, some will find that disappointing, but there is nothing to stop third parties such as Garage Games from providing tools that use the underlying framework.

Getting started
XNA Game Studio Express is an add-in for Visual C# Express. It will not install into the non-Express versions of Visual Studio, although you can have the two products side by side. Once installed you can use XNA from the full Visual Studio. It is also advisable to install the latest DirectX SDK, which ensures your DirectX runtime is up to date and adds some important utilities, such as the Texture Tool.

XNA adds three project templates to the Express IDE – Windows Game, Windows Game Library and the Spacewar Starter Kit, which is a complete game, albeit fairly complex. A better approach to learning XNA is to start a new Windows Game project, which creates a skeleton game. If you run the game immediately, it displays a blank window. The Game1.cs file defines a class that inherits from Microsoft.XNA.Framework.Game, and here is where you start writing code.

You will notice two TODO comments. One is in the Update method, which gets called at frequent intervals while the game is running. This is where you process the logic of your game. The second is in the Draw method, which is where you paint the screen. Your game will run in a loop, with Update and Draw running alternately. Other key methods are OnStarting, where you write initialisation code, and LoadResources for loading multimedia resources from files into program variables.

The best way to learn XNA is by coding a game. What follows is the simplest game imaginable. A PCW logo flies back and forth across a window. You must hit the spacebar when it is at the central small arrow. The score depends on how close you get to the arrow. The game is based on the tutorial in the documentation – Your First XNA Game – with additional steps as described below.

Creating a sprite
One of the first things you need to do with many games is to control sprites. The starting point for a sprite is an image – in this case the PCW logo – modified to add a small arrow. To use this in XNA, it needs to be loaded into a Texture2D object, as such:

pcwLogo = Texture2D.FromFile(graphics.GraphicsDevice, “pcwlogo.bmp”);

By default this creates a rectangular image, whereas you probably want an image with a transparent background. This is where the DirectX SDK utilities come in handy. Create the image as a png file with transparency set as you want. Then run the DirectX Texture Tool. Create a new Texture file with the same size as the image, and then choose File/Open onto this surface to place the png onto the Texture. Save the file as pcwLogo.dds and add it to the project, setting the Build Action property to Content, and setting Copy to Output Directory to Copy Always. Load the logo like this:

pcwLogo = Texture2D.FromFile(graphics.GraphicsDevice, “pcwlogo.dds”);

Sprites are drawn on the DirectX surface using the SpriteBatch class, in the Draw method of the Game class. As the name suggests, the SpriteBatch class lets you specify the position of multiple sprites, and then draw in a batch for best performance. You call SpriteBatch.Begin to initiate sprite drawing, SpriteBatch.Draw to position each sprite, and SpriteBatch.End to have the batch drawn. You need to make a small change to the tutorial code to get transparency:

spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

The code to get the sprite moving right and left is shown in the tutorial, with vertical movement set to zero.

Reading the keyboard
When the user hits space, the game stops the logo and reads the position. This test goes in the Update method of the Game class. Reading the keyboard is easy:

KeyboardState kbs = Keyboard.GetState();
if (kbs.IsKeyDown(Keys.Space) & !wasDown) {…}

There is a small issue with the keyboard handling. In a Windows application, you would probably handle a keyboard event such as KeyPress, which fires once only when the user hits the key. Here, by contrast, you are polling the keyboard state, so you need to maintain your record of the keyboard state in order to detect changes. This is the purpose of the wasDown variable shown above.

An XNA limitation is the lack of any built-in support for writing text to the DirectX surface. Fortunately, Microsoft’s Gary Kacmarcik has plugged the gap by offering some sample code via his blog.

His BitmapFont class creates a graphic for each character. This is included in the game to write a prompt to the screen, and to report the score. It depends on several files for each font you want to use. For Times, there are two .png files and one xml file which must be deployed with the application. You must add these to the project, using the same Build Action and Copy to Output Directory properties as for DirectX Texture files.

The game is used via the Visual C# Express Publish wizard. This will handle the dependencies, provided you have set the Build Action properties as described above. The publish wizard creates a setup.exe which will install the game. Currently, XNA only officially supports Windows XP SP2, although Vista is to be added as soon as it is fully released.

The XNA revolution
XNA really needs to be bundled with some user-friendly multimedia editors to make it more attractive for beginners, and features such as writing text should be built in.

The revolutionary part is the possibility of targeting a next-generation console as well as Windows, and its attractive price of £0.

A community is already building up around XNA, which means plenty of tips and help will be available, so it is well worth a look if you have a few gaming ideas or, for that matter, any application that works better on top of DirectX rather than the staid old windowing API.

www.pcw.co.uk/2167413
This article was printed from the Personal Computer World web site
© Incisive Media Ltd. 2008
Incisive Media Limited, Haymarket House, 28-29 Haymarket, London SW1Y 4RX, is a company registered in the United Kingdom with company registration number 04038503
Close this window to return to the website