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.

Latest in Gaming
Indiana Jones talking to a friend in a university setting with a jaunty smile on his face
New leak claims Indiana Jones and the Great Circle PS5 release will come in April
The main character from Intergalactic: The Heretic Prophet performing a jump attack on a robot enemy.
Neil Druckmann reveals new details about Naughty Dog's Intergalactic: The Heretic Prophet, says it's 'a game about faith and religion' and wants players to be 'lost' and 'confused'
Doom: The Dark Ages
Doom: The Dark Ages' director confirms DLC is in the works and says the game won't end the way 2016's Doom begins: 'If we took it all the way to that point, then that would mean that we couldn't tell any more medieval stories'
Nintendo Switch 2
A Nintendo Switch 2 FCC filing confirms Wi-Fi 6 and NFC support for the upcoming console
A close-up of the PS5 Pro
PS5 Pro games will soon get something 'very similar' to FSR 4 for what Sony is calling 'the next evolution of PSSR'
Xbox Series X
Xbox is reportedly teaming up with a mystery manufacturer to launch a PC gaming handheld this year
Latest in News
An Nvidia GeForce RTX 4060 on a table with its retail packaging
Nvidia RTX 5060 GPU spotted in Acer gaming PC, suggesting rumors of imminent launch are correct – and that it’ll run with only 8GB of video RAM
Indiana Jones talking to a friend in a university setting with a jaunty smile on his face
New leak claims Indiana Jones and the Great Circle PS5 release will come in April
A close up of the limited edition vinyl turntable wrist watch from AndoAndoAndo
This limited-edition timepiece turns the iconic Technics SL-1200 turntable into a watch, and I want one
A close up of Gemma sitting down in Severance season 2 episode 7
'I'm like Gemma, I'm in the dark': Severance star Dichen Lachman shares disappointing filming update for the popular Apple TV+ show's third season
OpenAI
OpenAI wants to help your business build its next generation of AI agents
The main character from Intergalactic: The Heretic Prophet performing a jump attack on a robot enemy.
Neil Druckmann reveals new details about Naughty Dog's Intergalactic: The Heretic Prophet, says it's 'a game about faith and religion' and wants players to be 'lost' and 'confused'