The anatomy of an XNA 4.0 game
Oct
18
Written by:
10/18/2011 8:37 AM
With Microsoft’s XNA 4.0 game development framework, writing exciting games for Windows/Xbox/Phone 7/Zune has never been easier. Never before has access to software development kits targeting next-gen consoles or the latest handheld media devices been so readily available. I am sure you are ready to get started building that next great game, so let’s first take a look at the general anatomy of an XNA 4.0 game.
You will need Visual Studio 2010 to follow along as we explore the anatomy of our first XNA game. Once you have started Visual Studio 2010 select File > New > Project. From the new dialog choose 'XNA Game studio 4.0' from the template categories on the left-hand side of the screen. From the right-hand side of the screen select ‘Windows Game 4.0’. Create the project.
Create A New Project
Select XNA Game Studio 4.0 from Template Categories
Open the Game1.cs file and let us take a look at the major sections/elements that will make up your game.
Game1.cs file
GraphicsDeviceManager graphics;
The first class-level variable is of the type GraphicsDeviceManager. This is a very important object because it provides you, as a developer, with a way to access the graphics device on your PC, Xbox 360, Phone 7 device or Zune. The GraphicsDeviceManager object has a property called GraphicsDevice that represents the actual graphics device on your machine. Because that graphics device object acts as a conduit between your XNA game and the graphics card on your machine everything you do on the screen in your XNA games will run through this object.
SpriteBatch spriteBatch;
The second variable is an instance of the SpriteBatch class. This is the core object you’ll be using to draw sprites. In computer graphics terms, a sprite is defined as a 2D or 3D image that is integrated into a larger scene. 2D games are made by drawing multiple sprites in a scene (player sprites, enemy sprites, background sprites, etc.).
protected override void Initialize(){}
The Initialize method is used to initialize variables and other objects associated with your Game1 object. Your graphics device object will be instantiated at this point and can be used in the Initialize method to help you initialize other objects that depend on its settings. You’ll use this method to initialize score values and other such items.
LoadContent method
protected override void LoadContent(){}
The LoadContent method is called after the Initialize method, as well as any time the graphics content of the game needs to be reloaded. The LoadContent method is where you will load all graphics and other content required by your game, including images, models, sounds, fonts and so on.
Draw and Update methods
The Game Loop
After the LoadContent method finishes, the Game1 object will enter into something known as a game loop. Almost all games use some form of game loop, whether or not they are written in XNA. Essentially, a game loop consists of a series of methods that are called over and over until the game ends. In XNA, the game loop consists of only two methods: Update and Draw. For now, you can think of the game loop in these terms: all logic that affects the actual game play will be done in the Update or the Draw method. The Draw method is typically used, to draw things. You should try to do as little as possible in the Draw method other than draw to your scene. Everything else needed to run your game (which eventually will involve moving objects, checking for collisions, updating scores etc.) should be done in the Update method.
Next Time
Next time we will modify our game project to draw some text on the screen as well as an image, so stay tuned!