01-18-2011, 01:13 PM
Lesson 01 Required Resources
Microsoft XNA Installation PackageLesson 01 Vocab
Methods are the C# name for functions. Everything you do will be in the methods. You can launch methods from inside other methods.Classes are C# objects. They hold methods along with variables.
Framework Classes are special classes that are located inside the XNA Framework. Just use them, don't try editing them.
Lesson 01: The Anatomy of a Project
It's been a while since I've done any C# programming, let's see if i still "got my groove". In this lesson we're gonna take it slow and try to understand exactly what everything does.Starting the Project
After you install the IDE (as shown in the Required Resources section) you will start a new project with File>New>Project. Choose a "XNA Game Studio 4.0", and then "Windows Game (4.0)". Name your game whatever you want, but for this lesson I'm choosing "tSR Tutorial 01". Click "OK" and your new project will be created.
The Project's Code
Before we program let's take a closer look at our XNA project:
Code:
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.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace tSR_Tutorial_01
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <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
base.Initialize();
}
/// <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
}
/// <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
}
/// <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)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
"Using" Statements
The first thing you'll likely notice is the list of "using" statements.
Code:
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.Input.Touch;
using Microsoft.Xna.Framework.Media;
Namespace
The next thing we see is the namespace:
Code:
namespace tSR_Tutorial_01
{
...
}
Class Declaration
Code:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
...
}
We also see a colon and some kind of XNA object. When you put a colon and a class name after the new class' name you are telling the compiler that the new class inherits variables and methods from the other class. For example, a Sprite class has a Draw method and a Location variable. Now you want a Player class and type "public class Player : Sprite". Now the Player has a Draw method and Location variable.
And then there are these two special variables, the GraphicsDeviceManager and the SpriteBatch. The GraphicsDeviceManager is a framework class that talks to the graphics device on your computer. The SpriteBatch is a framework class that draws all your sprites. If you want to add variables to any class you would put them between the class' two curly braces but before the constructor, like GraphicsDeviceManager and SpriteBatch are.
The Constructor
Code:
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
Game1's Initialization
Code:
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
LoadContent
Code:
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
}
Unload Content
Code:
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
Update
Code:
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);
}
Drawing
Code:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
Wrap up
I hope you now understand how the project works, so that when we start coding nobody will be lost. I'll try to write tutorials weekly, with next week's being sprite drawing and movement ("hello world" is a bit harder with XNA). I also hope I did a good job with writing