Getting Started with XNA Game Development Studio 3.0
You can download it here, and it can even be used in conjunction with the Microsoft C# Express edition.
I'm going to illustrate how easy it is to get started, by displaying a 2D Sprite, which will be hardware accelerated, and subsequently moving it around the screen.
Here's the sprite we'll be using:
I know, I'm so artistic.
Once you've got XNA installed, load up Visual Studio and create a new XNA Game Studio project, selecting the one labelled Windows Game (3.0).
Open up the solution explorer if it's not already visible, right click content, then Add Exisiting, and choose the location that you saved the above sprite to. You should then see Sprite1.png appear under Content in the solution explorer.
Now, open up Game1.cs and add the following two declarations inside the Game1 class:
Texture2D playerTexture;
Vector2 playerPosition;
These will hold the sprite texture, and position respectively. Now we need to load our sprite into memory. Find the LoadContent method, and add to it so it looks like the following:
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
playerTexture = Content.Load<Texture2D>("Sprite1");
}
Likewise, do the same for the Initialise method, where we'll set the initial position of the player:
protected override void Initialize()
{
// TODO: Add your initialization logic here
playerPosition = new Vector2(20, 20);
base.Initialize();
}
Done that? Good. Finally, we need to draw the sprite on the screen. Find the Draw method, and add code so it looks as follows:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(playerTexture, playerPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
Now press F5, and all being well you'll see your sprite on the screen:

Now let's add the ability to move our sprite up and down. Find the Update method and add the following code to allow our player to be moved up and down with the arrow keys:
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down))
{
float y = playerPosition.Y;
float x = playerPosition.X;
playerPosition = new Vector2(x, y + 2);
}
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up))
{
float y = playerPosition.Y;
float x = playerPosition.X;
playerPosition = new Vector2(x, y - 2);
}
base.Update(gameTime);
}
Hit F5 again, and congratulations! You successfully displayed a 2D hardware accelerated sprite on the screen and enabled it to be moved up and down. This is a simple example, but hopefully you get an idea of how rapidly you can put together a simple game using XNA Game Develpopment Studio.
Labels: Gaming, Tutorials, Visual Studio, XNA





