How to code games for the PC and Xbox 360

After that, there's a call to the Vector2.Clamp() method. 'Clamping' a number means to ensure it falls within a specific range – in this example, we want to ensure that a player's velocity falls within the range -10.0f to 10.0f, otherwise they would be able to reach stupidly fast speeds.

Once the velocity has been clamped to acceptable levels, we add the player's velocity to their current position, which has the effect of moving the player as requested.

Over to you

You can add various other features to the game: asteroids are created and moved in much the same way the player is moved, you can fire lasers, and those lasers can collide with asteroids.

Try your hand at adapting the game by implementing these challenges:

Fun: Make those asteroids collide dangerously with the player.
Tricky: Make asteroids that move off the screen re-appear on the other side as if they had wrapped around.
Taxing: Make the laser recharge rather than have infinite shots.
Mayhem: Load more than one asteroid image: create different types of asteroid rather than just one type.

All code required to implement these can be figured out from existing code in your download: steal code freely, copy and paste like you mean it, then fiddle to get what you want.

XNA takes all the hard work out of making games, so you can focus on coming up with an original idea then implementing it with awesome-looking assets. Have fun!

-------------------------------------------------------------------------------------------------------

First published in PC Format, Issue 223

Now read 14 arcade machines that led to a misspent youth

Sign up for the free weekly TechRadar newsletter
Get tech news delivered straight to your inbox. Register for the free TechRadar newsletter and stay on top of the week's biggest stories and product releases. Sign up at http://www.techradar.com/register

Follow TechRadar on Twitter