Difference between revisions of "Creating A Player"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(grammar)
(Draw method)
 
Line 102: Line 102:
 
public void Draw(SpriteBatch spriteBatch)
 
public void Draw(SpriteBatch spriteBatch)
 
{
 
{
     spriteBatch.begin() // Can pass matrix here
+
     spriteBatch.Begin() // Can pass matrix here
 
     spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f,
 
     spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f,
 
       SpriteEffects.None, 0f);
 
       SpriteEffects.None, 0f);
     spriteBatch.End()
+
     spriteBatch.End();
  
 
     /// Note spriteBatch can be initialized (Begun) in an external method
 
     /// Note spriteBatch can be initialized (Begun) in an external method

Latest revision as of 13:31, 1 November 2017

Creating the basic class

You need to create a new class, so from the main menu click File, and select New File.

New file class.png

You will have the following code:

 1 using System;
 2 namespace TestGame
 3 {
 4 	public class Player
 5 	{
 6 		public Player()
 7 		{
 8 		}
 9 	}
10 }

The public class Player line is the class definition, and public Player is the constructor for the class. You need to add the following lines within the using section:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

This will allow the class to access the MonoGame framework and SDK. Now to add the methods to setup, update and draw the player. So add the following methods into the player class:

public void Initialize()
{

}

public void Update()
{

}

public void Draw()
{

}

Adding variables to the class

Now still inside the player class add the following variables, these will be used by the game.

// Animation representing the player
public Texture2D PlayerTexture;

// Position of the Player relative to the upper left side of the screen
public Vector2 Position;

// State of the player
public bool Active;

// Amount of hit points that player has
public int Health;

// Get the width of the player ship
public int Width
{
     get { return PlayerTexture.Width; }
}

// Get the height of the player ship
public int Height
{
     get { return PlayerTexture.Height; }
}

Setup Initialize method

The Initialize method should be used to setup your player class. This can be used to assign a texture, position, and would be the best place to set the health or energy of the player. The position and texture already declared should be passed to the Initialize method. Adapt your Initialize method similar to below:

public void Initialize(Texture2D texture, Vector2 position)
{
    PlayerTexture = texture;

    // Set the starting position of the player around the middle of the screen and to the back
    Position = position;

    // Set the player to be active
    Active = true;

   // Set the player health
   Health = 100;
}

Draw method

Now the Initialize has setup the basic setting, we can use these to draw to the screen. Make the following alterations:

public void Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Begin() // Can pass matrix here
    spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f,
       SpriteEffects.None, 0f);
    spriteBatch.End();

    /// Note spriteBatch can be initialized (Begun) in an external method
    /// & hence it need only be begun once then any number of draw calls
    /// can be made, before finally ending the spriteBatch. Furthermore
    /// a spriteBatch can be begun more then once during a single iteration
    /// As long as it is not already been running.
}