DEFCOM1  
 

XNA Level 1 Lesson 3

Adding Graphics Sprites

In the Solution Explorer, Right Click on the Adding Graphics Content Menu and select Add- Existing Item option.

Browse for the ufo image. (you can get this from the sprites page)

The image will then be copied into your project folder

 

Just underneath the Public Class game 1,

Add the Comment //GLOBAL VARIABLES just before GraphicsDeviceManager graphics;

Variables are where we store game related data, location, health, score etc...

 

Under the SpriteBatch line add the Texture2D and Vector2 code (marked new line)

 

public class Game1 : Microsoft.Xna.Framework.Game
{
        
    //GLOBAL VARIABLES
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Texture2D ufoTex;// This is a texture we can render. //NEW Line
    Vector2 ufoPos = Vector2.Zero;// Set the coordinates to draw the sprite at. //NEW Line

    public Game1()
    {

 

These variables store the position of the ufo and the image (texture) of the ufo

Note: we will be adding more variables into the this GLOBAL VARIABLE section as we go along

 

Inside the LoadContent function add the loading code

Add the line: ufoTex = Content.Load<Texture2D>("ufo");

so it should now look like this

protected override void LoadContent()
{
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);
            
    //load our graphic
    ufoTex = Content.Load<Texture2D>("ufo"); //New Line

}

This line loads the ufo image into our ufoTex. The "ufo" must match the file name, but we do not add the file extension, i.e .bmp .jpeg

 

Inside the Draw function we need to add the code that will draw the game

after the line where you changed the colour of the background add the Begin, Draw and End lines

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.Black);

    // TODO: Add your drawing code here

    spriteBatch.Begin(); //NEW LINES
    spriteBatch.Draw(ufoTex, ufoPos, Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
}
 

Press F5

Tadaa, Now on to Movement :)

 

Complete Listings

public class Game1 : Microsoft.Xna.Framework.Game
{
        
    //GLOBAL VARIABLES
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;


    Texture2D ufoTex;// This is a texture we can render.
    Vector2 ufoPos = Vector2.Zero;// Set the coordinates to draw the sprite at.

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

 
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
            
        //load our graphic
        ufoTex = Content.Load<Texture2D>("ufo");

    }


    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    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);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        // TODO: Add your drawing code here

        spriteBatch.Begin();
        spriteBatch.Draw(ufoTex, ufoPos, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}