Hey everyone, I have made a cute pixel player sprite and wrote some code to go with it, and I’m posting it here! Here is the texture: (Add it to your Content.mgcb
file and make sure it’s named tPlayer.png
)
And here is the code! (Copy and paste it into an empty C# file)
public class Player
{
public const int SPEED = 5; // Adjust to your liking.
Game1 game;
Vector2 position;
public Vector2 Position
{
get { return position; }
set { position = value; }
}
Vector2 velocity;
public Vector2 Velocity
{
get { return velocity; }
set { velocity = value; }
}
Texture2D texture;
public Texture2D Texture
{
get { return texture; }
set { texture = value; }
}
/// <summary>
/// Used to construct a Player!
/// </summary>
/// <param name="startX"></param> The X coord the Player is started on.
/// <param name="startY"></param> The Y coord the Player is started on.
/// <param name="game"></param> A player takes a reference to a Game1.
public Player(float startX, float startY, Game1 game)
{
this.game = game;
position = new Vector2(startX, startY);
velocity = Vector2.Zero;
texture = game.Content.Load<Texture2D>("tPlayer"); // t stands for Texture2D!
}
/// <summary>
/// Called when the Player needs to handle input from the keyboard and mouse. Adjust the controlls to your liking.
/// </summary>
/// <param name="keyboard"></param> The keyboard's current state
/// <param name="mouse"></param> The mouse's current state
public void HandleInput(KeyboardState keyboard, MouseState mouse)
{
if (keyboard.IsKeyDown(Keys.W) || keyboard.IsKeyDown(Keys.Up))
velocity += new Vector2(0, -SPEED); // Move up!
if (keyboard.IsKeyDown(Keys.S) || keyboard.IsKeyDown(Keys.Down))
velocity += new Vector2(0, SPEED); // Move down!
if (keyboard.IsKeyDown(Keys.A) || keyboard.IsKeyDown(Keys.Left))
velocity += new Vector2(-SPEED, 0); // Move left!
if (keyboard.IsKeyDown(Keys.D) || keyboard.IsKeyDown(Keys.Right))
velocity += new Vector2(SPEED, 0); // Move right!
}
/// <summary>
/// Called once a frame.
/// </summary>
/// <param name="gameTime"></param> Provides a snapshot of timing values.
public void Update(GameTime gameTime)
{
// Add the velocity to the position.
position += velocity;
// Reset velocity!
velocity = Vector2.Zero;
}
/// <summary>
/// Called when the Player needs to draw itself.
/// </summary>
/// <param name="spriteBatch"></param> The SpriteBatch used to draw the Player.
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture: texture, position: position, scale: new Vector2(2, 2));
}
}
and you may use it in a game class like this:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Player player;
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);
player = new YourNamespace.Player(100, 100, this);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific 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)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
player.HandleInput(Keyboard.GetState(), Mouse.GetState());
player.Update(gameTime);
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);
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
player.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}`
I hope you like my code and art and leave a comment if you have any feedback! (Remember to add the “using” directives in, by the way).
- Surge Crafter
EDIT: Fixed a grammar mistake I made!