The VG Resource

Full Version: tSR GameDev Tutorials: C# & XNA - Lesson 01
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Lesson 01 Required Resources
Microsoft XNA Installation Package

Lesson 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);
        }
    }
}
Don't let the wall of code discourage you, it's actually pretty easy to understand if you break it down.

"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;
These lines of code tell the compiler which XNA libraries this class will use. For example, if we want to use the graphics-based variables and methods we need to use "Microsoft.Xna.Framework.Graphics".

Namespace
The next thing we see is the namespace:
Code:
namespace tSR_Tutorial_01
{
...
}
This lets the compiler know that everything within these curly braces are for the specified game, in this case "tSR_Tutorial_01".

Class Declaration
Code:
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    ...
}
This is a public class, which means that the whole game can access it. Player classes and enemy classes are examples of classes you would want public.

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";
}
This method is run only when Game1 is created. A constructor for a class is just the class' name followed by a set of parentheses. With the exception of Game1, you put all of your initialization code in a class' constructor. Initialization code is just setting the variables you declared in the previous step.

Game1's Initialization
Code:
protected override void Initialize()
{
    // TODO: Add your initialization logic here

    base.Initialize();
}
While most classes use constructors, Game1 uses its own Initialization method. It works the same as the constructor.

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
}
This is where all the content for Game1 is loaded, right after we init SpriteBatch. I'll go into detail on how to load in the next lesson.

Unload Content
Code:
protected override void UnloadContent()
{
    // TODO: Unload any non ContentManager content here
}
eh, I never really use this tbh, I think it's mainly for platform programming or something.

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);
        }
This method is called every frame, and is where you will call other classes' update methods. You can even use gameTime (which tells you how many seconds have elapsed since the last frame (generally less than 1) to make your animations less choppy! That if statement is really just for Xbox debugging, so ignore it for now.

Drawing
Code:
protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    // TODO: Add your drawing code here

    base.Draw(gameTime);
}
This is where you will draw sprites every frame, but before you draw anything you gotta clear the display of the previous frame's sprites (shown here with Cornflower Blue). The next lesson will have you working with sprites here.

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 Sick