Getting Started with XNA

Install XNA

Step 1)

Download and install .NET framework 2.0 Redistributable (if you don’t already have it) :

32‐bit: http://www.microsoft.com/downloads/details.aspx?FamilyID=0856EACB‐4362‐4B0D‐8EDD‐ AAB15C5E04F5&displaylang=en

64‐bit: http://www.microsoft.com/downloads/details.aspx?FamilyID=B44A0000‐ACF8‐4FA1‐AFFB‐ 40E78D788B00&displaylang=en

Step 2) Download and install DirectX 9.0c (if you don’t already have it): https://www.microsoft.com/downloads/details.aspx?familyid=9226a611‐62fe‐4f61‐aba1‐ 914185249413&displaylang=en

Step 3)

Download and install Visual C# Express edition: http://msdn2.microsoft.com/en‐us/express/aa700756.aspx

You’ll need to “register” the application but it’s still completely free.

Step 4)

Download and install XNA Game Studio 1.0 Refresh: http://msdn2.microsoft.com/en‐us/xna/aa937795.aspx

Create Your First XNA Game Project!

Step 1)

Start C# Express edition

Step 2)

Click File‐>New‐>Project

Select Windows Game or 360 Game

Name your project whatever you like.

Windows Game Projects and Game projects contain exactly the same code. The difference is how you will run your application, whether you send it to the 360 to run it there (using Microsoft creator’s club) or you run it locally on your windows machine.

Step 3)

Run your very first XNA game!

Press f5 on your keyboard, click the little green triangle run button on the top bar of the program, or click Debug ‐> Start Debugging

Woohoo!!! Blue screen! That’s all there is to making a game in XNA, this gives us a solid base to make a full game.

Understanding the Base Code

This is the code we are given as a starter. We’ll analyze what we’re given in some depth. Don’t be scared off by the fact that the code is in C#. If you know Java, you can pretty much get away with just writing Java code most of the time since the syntax is nearly identical.

‐‐‐‐‐‐‐‐‐‐‐‐‐ Program Class ‐‐‐‐‐‐‐‐‐‐‐‐‐ using System; namespace FirstGame { static class Program { ///

/// The main entry point for the application. /// static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } } } }

‐‐‐‐‐‐‐‐‐‐‐‐‐ Program Class ‐‐‐‐‐‐‐‐‐‐‐‐‐

‐‐‐‐‐‐‐‐‐‐‐‐‐ Game1 Class ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

#region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; #endregion namespace FirstGame { ///

/// This is the main type for your game /// public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content;

public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); }

///

/// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// protected override void Initialize() { // TODO: Add your initialization logic here

base.Initialize(); }

///

/// Load your graphics content. If loadAllContent is true, you should /// load content from both ResourceManagementMode pools. Otherwise, just /// load ResourceManagementMode.Manual content. /// /// Which type of content to load. protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { // TODO: Load any ResourceManagementMode.Automatic content }

// TODO: Load any ResourceManagementMode.Manual content }

///

/// Unload your graphics content. If unloadAllContent is true, you should /// unload content from both ResourceManagementMode pools. Otherwise, just /// unload ResourceManagementMode.Manual content. Manual content will get /// Disposed by the GraphicsDevice during a Reset. /// /// Which type of content to unload. protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent) { // TODO: Unload any ResourceManagementMode.Automatic content content.Unload(); }

// TODO: Unload any ResourceManagementMode.Manual content }

///

/// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// /// Provides a snapshot of timing values. 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

base.Update(gameTime); }

///

/// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here

base.Draw(gameTime); } } }

‐‐‐‐‐‐‐‐‐‐‐‐‐ Game1 Class ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐

Program.cs provides a main method which starts running our game. We won’t go into it in detail.

///

/// The main entry point for the application. /// static void Main(string[] args) { using (Game1 game = new Game1()) { game.Run(); } }

It’s quite straight forward; it just creates a new instance of our game1 class and runs the game. You may need to edit this at some point if you want to handle command line parameters and act accordingly, but for now we won’t worry about it.

Let’s move onto the Game1 Class.

GraphicsDeviceManager graphics; ContentManager content;

These two objects are quite important. The GraphicsDeviceManager is the link between your code and the graphics device (video card) of your computer. To put anything on the screen you need to go through the graphics object.

The ContentManager is an object which keeps track of all of the content for your game (ie. Images, textures, models, sounds), so that you don’t need to worry about file formats, importing files etc. Once an asset is added to the Content Pipeline, (I’ll explain how that is done a little later) it is accessible through the ContentManager. protected override void Initialize()

Add any initialization code you need for your game goes into this method. This could be initializing variables, loading a level from a file etc. protected override void LoadGraphicsContent(bool loadAllContent) protected override void UnloadGraphicsContent(bool unloadAllContent)

You can ignore these methods if you are using the XNA content manager, since it will handle loading and unloading graphics content for you, you only need to worry about these methods if you want to load some content outside of the content manager. protected override void Update(GameTime gameTime) protected override void Draw(GameTime gameTime)

These two methods are the two methods you’ll be modifying the most. XNA has a fixed frame rate (defaults to 60 fps), and calls each of these methods for you. The Update method is called once every 60th of a second and the draw method is called as often as possible.

All of your code to draw things on the screen goes in the Draw method, and all code to update the state of your game goes in the Update method.

You can see in the Draw method the line of code that makes our beautiful blue screen:

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

You can change the color to make any solid colour game you like!

Lets draw something else!

Add these lines of code to your draw method:

SpriteBatch batch = new SpriteBatch(graphics.GraphicsDevice); batch.Begin();

Vector2 location = new Vector2(0, 0); Color tint = Color.White; Texture2D image = content.Load("GameThumbnail"); batch.Draw(image, location, tint); batch.End();

There’s quite a bit going on here and it warrants some explanation.

First we need to create a SpriteBatch.

SpriteBatch logo = new SpriteBatch(graphics.GraphicsDevice);

This object will draw our sprites onto the screen. It is more efficient to send a bunch of images to a video card batched together as opposed to one at a time, so XNA sprites need to be drawn in groups.

We start the batch with the line: batch.Begin();

And end it with: batch.End();

We call the draw method on our SpriteBatch to draw sprites, but we need to specify a bit of information so that they can be drawn.

We need:

Vector2 location = new Vector2(0, 0);

The location of the sprite as a two‐dimensional vector (, y).

Color tint = Color.White;

The tint of the sprite (you can tint images in code to modify their colours, if you wanted to make an enemy flash red when he’s dying you could change the tint between Color.White and Color.Red).

Texture2D image = content.Load("GameThumbnail");

The image to be displayed. You can see that we are loading it from the content pipeline.

Once we have all the information we need, we can draw the sprite to the screen. batch.Draw(image, location, tint);

If you were to try to just add this code to the XNA starter code and run it, you would get an error because GameThumbnail is not included in the Content pipeline. Let’s fix that right now.

We can see that the GameThumbnail.png file exists in our solution, so why can’t XNA find it? At the bottom left of the screen we can see a boolean flag on the file called XNA Framework Content. All we need to do to add that file to the Content pipeline so that we can use it in code is change False to True.

Now when we run our game, we should see something like this:

And there’s our picture on the screen! To be continued...