Posts Tagged ‘BasicStarter’

BasicStarter – something to get you up and running with XNA :)

Thursday, December 17th, 2009

basicstarterscreendumpBased on the input I received from my talk at Aalborg .Net User Group yesterday, I cleaned & commented my code and created a little XNA 3.1 Windows project called BasicStarter to download.

It is a simple start-out class for use in games for beginning XNA coders, has basic keyboard movement in four directions, fullscreen setup, black background.
It is possible to pause the game with "P"
It is possible to exit the game with "Esc"
The project also shows how a class called GameSprite which subclasses DrawableGameComponent is used to store image, position and speed of an object in a game.

Here is the class diagram. I like to get the gist of other people’s code this way Smiley


Some notable pieces of code for the beginner are:

How to get a direction from multiple keypresses
//create a variable to hold which direction to move
Vector2 movement = Vector2.Zero;

//alter the direction according to the keys pressed:
if (keyState.IsKeyDown(Keys.Down)) { movement += Directions.Down; }
if (keyState.IsKeyDown(Keys.Up)) { movement += Directions.Up; }
if (keyState.IsKeyDown(Keys.Left)) { movement += Directions.Left; }
if (keyState.IsKeyDown(Keys.Right)) { movement += Directions.Right; }

//use the combined movement from the keys
//to update the position of the XNA logo
//we multiply by gamespeed to get a reasonable speed onscreen
xnaLogo.Velocity = movement * gameSpeed;
Hope you like it - and send feedback if you can think of improvements :)