Learning the (XNA) basics – part 2

XNA codeContinuing from Learning the (XNA) basics lets take a quick look at what all that XNA framework code actually does.

We write XNA code in C# (C sharp) so you should at least be familiar with the basics of C# before starting in on XNA (there are a lot of introductory tutorials for both C# and XNA on the internet).

Taking it section by section;

Using statements reference other namespaces which are typically linked libraries or assemblies within .NET. They basically enable us to use methods from within those namespaces without having to specify the namespace every time, so instead of typing Microsoft.Xna.Framework.Graphics.Effect we can just type Effect and the compiler will select the correct namespace for us because we have a using reference to Microsoft.Xna.Framework.Graphics.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

Here XNA has specified a namespace for our game, WindowsGame1. You may end up using multiple namespaces within your game to help seperate out functional components and to provide another level of encapsulation. You should see that code will only be able to access code within another namespace by fully qualifying the namespace or via the using method.

namespace WindowsGame1

This is the class definition and shows that our Game1 class inherits from the Microsoft.Xna.Framework.Game class, which provides all of the base functionality for our game. Within the definition XNA has provided member variables for the GraphicsDeviceManager which is our graphics device and a SpriteBatch which may be used for drawing sprites.

/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

The class constructor initialises the GraphicsDeviceManager member, graphics, and defines a root directory for the game to find it’s Content (content is also known as the game assets, or the models, textures, sounds, etc that your game will use).

public Game1()
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

The Initialize method can be used to perform other initialisation actions. It is also important to call base.Initialize(); as this causes the correct creation of any other GameComponents.

/// <summary>
/// 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.
/// </summary>
protected override void Initialize()
// TODO: Add your initialization logic here


LoadContent can be used to, load your content 🙂 You will see that XNA has initialised the SpriteBatch member, spriteBatch, here.

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
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

UnloadContent, unsurprisingly, unloads your content 🙂

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
// TODO: Unload any non ContentManager content here

This is a key method, Update. Upate and Draw are (usually) called in sequence over and over providing your game loop. The Update step is usually where you would gather user input, and update your game logic. You will see that Update also takes a parameter, gameTime, which allows us to perform time-related calculations. XNA has provided minimal code here, it checks to see if a controller button has been pressed and if so exits the game loop.

Like, Initialize, the call to base.Update(); causes any other GameComponents to update as well.

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

// TODO: Add your update logic here


The other key method within your game loop, Draw. Draw, like Update, takes a gameTime parameter. Here XNA clears the window to a pretty blue, and calls base.Draw(); so that any other DrawableGameComponents can also draw themselves.

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)

// TODO: Add your drawing code here


As you can see XNA has provided a simple framework for a game / game loop. There is a lot more going on under the hood, but we don’t actually need to concern ourselves with how XNA does what it does.

Now its time to think about how to use that framework to start building your game.

, , , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: