Any time you start an XNA project it generates a whole lot of code for you. Let's start by taking a look at that code.
Below, I've included just the bare basics code that's generated by Visual Studio 2010 for an XNA project. If you haven't done any XNA before, you should really go through this code - line by line - and get familiar with it. This code is going to be in every XNA program you create. So you need to know what it's doing.
In the version I've included below, I've added a lot of my own comments to try and explain what's going on in the bare basics code. Reading through my comments may help you get a little more familiar with this code if you are not already familiar with it.
You may want to copy this file into Visual Studio in order to read it more easily.
If you've written XNA programs before, and are pretty familiar with XNA, you may just want to skip this lesson.
//These are calls to various libraries that XNA will need to function. //Although some of them could be excluded, if you don't call any of their objects //in your code. I've marked the one's you absolutely have to have, but having //these calls here really doesn't hurt anything. So, I might remove Linq until //I need it... which I'm still imagining will be never. Otherwise, I would just //leave the using statements as they are because you will probably use them eventually. using System; //The core of C#. You're going nowhere without this. using System.Collections.Generic; //May need at some point for data structures like Lists. using System.Linq; //You may never need this one in your life. using Microsoft.Xna.Framework; //The core of XNA. Ya gotta have this. using Microsoft.Xna.Framework.Audio; //Only if you've got sound. using Microsoft.Xna.Framework.Content; //Not absolutly necessary if your game has no content, but it almost certainly will. using Microsoft.Xna.Framework.GamerServices; //Basic code will build without this. using Microsoft.Xna.Framework.Graphics; //Needed for drawing sprites. Code will build without this if you remove all the sprite code. using Microsoft.Xna.Framework.Input; //Needed to read the Xbox controller and probably for the keyboard as well. using Microsoft.Xna.Framework.Media; //Basic code will build without this. namespace BareBones //Your namespace here has to match what's in Program.cs. { /// <summary> /// This is the main type for your game /// </summary> //This is where the core of your game is declared, by "inheriting" the Framework.Game object from XNA. //You can rename Game1, but I would suggest leaving it alone for the first few games you create, at least. public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; //This object manages your graphics card and the process of drawing graphics. SpriteBatch spriteBatch; //This is for drawing sprites and you can remove it if you're not going to draw sprites. //This is the "constructor" for your Game1 class that was just declared above. //This method will be basically the first code called upon at startup. As a //beginner in XNA, you're probably not going to mess with it much, but it's here //for more advanced manipulation of what's happening in the code. public Game1() { graphics = new GraphicsDeviceManager(this); //This basically puts the GraphicsDeviceManager in memory and gets it going. Content.RootDirectory = "Content"; //This defines the folder on your harddrive where Content is stored such as sounds and texture files. } /// <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> //Initialze will run after the constructor above. This is where you put code that will //run every time the game starts up that is not related to art work, such as sounds and //3D models. It will probably only run when the game starts up. protected override void Initialize() { // TODO: Add your initialization logic here //Never mess with this. It should ALWAYS be in your code. Initialize is an "overide" method, which means there's //another Initialize method at a higher level, that you don't have access to other than what's done in this section. //Still, it's absolutely necessary to call that Initialize method that's deeper in the code, so that it gets to //run. Basically, just make sure you always have this next statement and you can forget about it. base.Initialize(); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> // This is the place to load your artwork into the computer's memory from disk at startup. protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); //You can remove this if you don't ever draw any sprites. // TODO: use this.Content to load your game content here } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> //I'm still trying to figure out what UnloadContent is for. I've never seen as example where someone actually //unloaded content. I assume it runs when you exit the game. protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <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> //This is where the action takes place. Pretty much all your game code goes here other than //loading artwork into memory, initial setup, and drawing on the screen. It runs all the time //at a rate that is "probably" 60 frames per second. Just think of this as running constantly //over and over as long as the game is running. protected override void Update(GameTime gameTime) { // Allows the game to exit //This is code to allow you to close the game by pressing the "Back" button on your XBox 360 controller //that's attached to this game. You DO have an XBox 360 controller attached to your PC don't you... //This serves partially as an example of how you can setup code to allow your keyboard or controller to //control the game. Alt+F4 always closes the game, by default. Otherwise, the "Back" button is currently //the only way to get out of the game. One of the first things I do is hookup the "Escape" button to close //the game. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here //Right here is where pretty much all your code for the game goes. //Again, Update is an "override" of an Update method "deeper" in the system (an override of the Game class's //Update method). That still needs to be called every time this section runs because there's important stuff //done when it runs. Just make sure it's always here and forget about it. base.Update(gameTime); //You NEVER want to remove this. } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> //Your Draw method is where you put code that actually draws things on the screen. Basically, you //should never have any code in here unless it's all about putting things on the screen. None of //your game logic should be in here. It's ONLY for putting things on the screen. Since video games //are all about putting things on the screen, you're going to have a lot of code here even without //any game logic in this section. Your game logic needs to be in your Update method. protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); //Basically, you want to clear the screen every time you draw. Pick your color. // TODO: Add your drawing code here //This is where all the code for drawing on the screen in your game goes. //Again, Draw is inherited from the Game class. There is some functionality that's at that higher level that //needs to run every time your Draw method is called. So you always want base.Draw() to be called after all your own //Draw code has run. It's pretty much always going to be the very last line of your Draw code. base.Draw(gameTime); //You should NEVER remove this line. } } }
In this first lesson, we're just getting familiar with the code that XNA generates for every program you make. You can build this program and get the well known CornFlowerBlue screen. That's all it does. But this code will be used in every XNA program you ever create. So, getting familiar with it is important.