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:

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'