DEFCOM1  
 

XNA Level 2 Lesson 1

A New Project

  1. New project
  2. Import graphics
  3. Create tank variables
  4. Load and draw sprite sheet selection
 

New Project

This next mini game, a top down 2d tank game, will focus on the use of some basic maths in games. A lot of the maths involved in this level will also be used in level 3 when we create a simple First Person Shooter (FPS)

First of all you will need to create a new project.  

 

Import graphics

 

 

Create tank variables

 

We are going to need variables to store

  • The Sprite Sheet Texture
  • The Position
  • The Rotation

Add them just like you did before in the section for global variables

//Global Variables
protected Texture2D SpriteSheet;
        
protected Vector2 position;
protected float rotation;

 

We also need to initialise our variables

In the initialise function add

position = new Vector2();
rotation = 0;

 

We now need to load the sprite sheet image into our sprite sheet texture

Go to the LoadContent function

 

SpriteSheet = Content.Load<Texture2D>("TankSpriteSheet");

 

 

Sprite Sheet selection

 

Our draw function will be a little more complicated than last time as we will be taking into account the sprite and rotation.

 

Our new Draw command has 9 parameters

spriteBatch.Draw (

SpriteSheet ,        

position,           

new Rectangle(0, 0, 128, 128),

Color .White ,

rotation,           

new Vector2(64, 64),

1.0f,               

SpriteEffects .None ,

1                   

);

 

The Image

The position on screen (x and y)

which part of the texture are we using

full colour with no tinting.

which angle is it pointing

rotation handle, in the case the centre of the sprite

the scale

no effects

sprite layer order

 

GraphicsDevice.Clear(Color.SeaGreen);

spriteBatch.Begin();

spriteBatch.Draw(SpriteSheet, position, new Rectangle(0, 0, 128, 128), Color.White, 
rotation, new Vector2(64, 64), 1.0f, SpriteEffects.None, 1);

spriteBatch.End();

 

 

Press F5 The tank should now be in the top left corner

Feel free to play with the position

 

Complete Listings

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

    //Global Variables
    protected Texture2D SpriteSheet;
        
    protected Vector2 position;
    protected float rotation;



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


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

        position = new Vector2();
        rotation = 0;


        base.Initialize();

    }


    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

        SpriteSheet = Content.Load<Texture2D>("TankSpriteSheet");
    }


    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.SeaGreen);

        spriteBatch.Begin();

        spriteBatch.Draw(SpriteSheet, position, new Rectangle(0, 0, 128, 128), Color.White, 
		rotation, new Vector2(64, 64), 1.0f, SpriteEffects.None, 1);

        spriteBatch.End();


        base.Draw(gameTime);
    }
}