How to code games for the PC and Xbox 360

Of course, that raises another question: just what is a tick? Think about it this way: your computer is doing all sorts of things behind your back.

In the case of the Xbox 360, it's managing RAM, it's checking whether your friends are online and it's busy calculating when would be the most annoying time to show you its Red Ring of Death. Your game has to play nicely with all the other things the computer is doing, which means it only has control for part of the time.

What happens is that your PC says: 'OK, Bang – go ahead and update yourself. Now wait a nanosecond while I finish downloading this page in Firefox. OK, I'm done; go ahead and update yourself again.'

Those 'update yourself' parts are ticks: the bits where you have control. They can happen once a second, a hundred times a second or even more, depending on how much work you do in your Update() and Draw() methods.

As soon as your game starts, XNA starts calling Update() then Draw(), Update() then Draw(), and will continue calling those two methods until your game finishes: either when players click the Close button in Windows, chooses the Return to Dashboard option on the 360, or you write some code to tell the game to quit.

Alternatively, your game will also quit if it crashes, but that only happens if you write bad code. And you don't write bad code, do you?

Asset management

What do you do if you want to load a texture into your game? What about something trickier, like a font? Or, trickier still, a 3D model?

All of this used to require lots of programming – you had to write code to read JPEGs, code to read PNGs, code to load animations, and much more. But all this is taken away from you with XNA: to load a game asset, there are now just three steps:

1. Add the asset to your solution. If you look on the right of the VC# window you'll see the Solution Explorer, which contains things like Properties, References, Content, and some files ending with .cs (that's the file extension used for XNA code). To add some game content to your solution, right-click on Content then choose Add > Existing Item then choose what you want to add. The file will automatically be copied to the Content directory for your game.

2. Tell XNA that you want to load that asset in your game. You also need to tell it what type of asset it is. If you look near the top of Game1.cs you'll see lines like 'Texture2D sfcAsteroid' – that means 'give me some space in memory that will hold a texture, and let me refer to that in my code as sfcAsteroid.' If you were wondering, 2D textures are the most common type: it's a picture with a width and a height, but no depth.

3. In the LoadContent() method, ask XNA to load the asset into the memory you requested. For example,'sfcAsteroid=Content.Load("asteroid");' means 'load the asset named 'asteroid' into the texture memory reserved for me at sfcAsteroid'. XNA names assets by removing filenames' extensions then looking in the Content directory. That's it. All the work of loading JPEGs, PNGs and TGAs, as well as all the work of handling memory compression techniques such as DXTC, is ultimately boiled down to a single line of code:

Content.Load().

Drawing the world

For a 2D game, you may think we can just draw pixels to the screen with the bitmapping method. But we' don't: DirectX doesn't really 2D much; if you've got an uber-fast 3D graphics card, why not use it? So, XNA does some magic for us: whenever we say, 'Draw this sprite in 2D, please,' XNA actually creates two triangles and maps our texture onto them in 3D space. This has several obvious advantages: