How to code games for the PC and Xbox 360

If you've got high-end hardware capable of DX10 or later, don't get carried away – XNA is designed to be compatible with both Windows and X360, which means the most you should aim for is the Xbox's curious superset of shader model 3.0.

Once you have the right software (that will take a little time) and the right hardware (only antediluvian PCs are ruled out here), you're all set to code.

Wait… How do you code?

We get asked this question a lot: "What's the best way to start programming?" Some people like to buy books. Some people like to get a computer games programming degree. But they are – and we don't want to put too fine a point on this – stupid.

You don't read books if you want to learn to ride a bike. You don't need a degree to play GTA4 (although it might take one to get the damn thing working). So if you think programming is different, you're wrong: the best way to learn to code is to just to dive in. Steal code from other people. Copy and paste stuff, then edit it a little to get what you want. Try things out, because if they don't work you've lost nothing.

So, in this tutorial we're not going to get bogged down talking about theory. And we're not going to explain to you what every line of code does, because, quite frankly, it's irrelevant. Go with the flow; you'll pick up the theory later. For now, dive in: we're going to produce a simple little top-down shooter called Bang.

The player will control a spaceship, and asteroids will fly around the screen begging to be shot. All the source code for this project can be downloaded from the PC Format website. To get started, go into the Bang folder and double-click on the file Bang.sln.

Visual C# 2008 Express Edition (VC# from on) will start and load the project for editing. If you get a Windows message box saying something like 'Windows cannot open the file Bang.sln', then you must have skipped over the 'install Visual C#' step – go back and try again.

VC# is an incredibly powerful development environment. In this one program you can write all your code, debug it and deploy it to your Xbox. Don't worry about debugging for now – a wise man once said that debugging is harder than coding, so if you write your code as cleverly as you possibly can, then you are – by definition – not smart enough to debug it.

Once VC# has loaded, you'll see all the source code for our game. It totals 300 lines, but that includes empty and almost-empty lines. For now, go to the Debug menu and click Start Debugging: this will start the game up and you should be able to play it.

The keys we've used are WSAD to move the player, with the cursor keys to aim lasers. OK, now stop playing. No, really, stop – it's time to take a look at how the program works.

Behind the curtain

When you create a blank XNA project using the File > New Project menu, VC# writes a little bit of code for you – just enough to get you a blank window with a pale blue background.

It creates five basic pieces of code for you: Initialize(), LoadContent(), UnloadContent(), Update() and Draw(). These are known as methods, which is a neat little piece of coder jargon designed to fool people into thinking programming is hard.

These five methods are magic because they get called for you by XNA:

Initialize() gets called when your game starts up.
LoadContent() is called when DirectX is ready to start loading game data (pictures, sound, etc).
UnloadContent()
is called when your game is ending and you need to clean up after yourself. You can ignore this.
Update() is called every tick so you can make changes to your game world.
Draw() is called every tick so you can draw your game world to the screen.