Making the Player Class

 The Player Class:



What does it contain?

It contains all the functionality of the player. This includes(but not limited to) movement, animation, powerups, health, etc.

The Player class:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended.Sprites;
using System;
using System.Collections.Generic;
using System.Text;
namespace RPG_1
{
public class Player
{
public Vector2 pos;
private AnimatedSprite[] playerSprite;
private SpriteSheet sheet;
private float moveSpeed = 1.5f;
public Rectangle playerBounds;//For the collisions
public bool isIdle=false;
public Player()
{
playerSprite = new AnimatedSprite[10];
pos = new Vector2(100, 50);
playerBounds = new Rectangle((int)pos.X-8/*centered at centre*/,(int)pos.Y-8,16,17);
}
public void Load(SpriteSheet[] spriteSheets)
{
for (int i =0; i<spriteSheets.Length;i++)
{
sheet = spriteSheets[i];
playerSprite[i] = new AnimatedSprite(sheet);
}
}
public void Update(GameTime gameTime)
{
isIdle = true;
playerSprite[0].Play("idleDown");
string animation = "";
var keyboardstate = Keyboard.GetState();
if(keyboardstate.IsKeyDown(Keys.D))//Move right
{
animation = "walkRight";
pos.X += moveSpeed;
isIdle = false;
}
if (keyboardstate.IsKeyDown(Keys.A))//Move right
{
animation = "walkLeft";
pos.X -= moveSpeed;
isIdle = false;
}
if (keyboardstate.IsKeyDown(Keys.W))//Move right
{
animation = "walkUp";
pos.Y -= moveSpeed;
isIdle = false;
}
if (keyboardstate.IsKeyDown(Keys.S))//Move right
{
animation = "walkDown";
pos.Y += moveSpeed;
isIdle = false;
}
if (!isIdle)
{
playerSprite[1].Play(animation);
playerSprite[1].Update(gameTime);
}
playerSprite[0].Update(gameTime);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
if (isIdle)
spriteBatch.Draw(playerSprite[0], pos);
else
spriteBatch.Draw(playerSprite[1], pos);
spriteBatch.End();
}
}
}
view raw Player.cs hosted with ❤ by GitHub


After this you got to call the Load function of your player class from the load function of your game1 class.
The update function of your player class from the update function of your game1 class
The draw function of your player class from the draw function of your game1 class.




Comments

Popular posts from this blog

Tile map collisions

Introduction