Drawing a Sprite
Demonstrates how to draw a sprite by using the SpriteBatch class
Overview
In any game, drawing basic textures to the screen is essential knowledge, whether it is for menus and background images, to 2D game textures and characters. In this sample we will walk though the steps needed to load a texture and then render it on the screen.
End result
Requirements
The following texture will be used to render to the screen.
Save it to your content project and name it "Character" (this name will used to reference it in the project).
Note
The tutorial assumes you have already created a new MonoGame project using one of the standard templates.
To draw a sprite on screen
Add the texture to your Content project, as detailed in HowTo Add Content.
Load the textures that will be used for drawing sprites in the Game.LoadContent method. In this case, the example uses the Content member to load a texture from the MonoGame Framework Content Pipeline.
Important
The texture must be in the project, with the same Name passed to ContentManager.Load. In this case the texture should be called "Character"!!
// The reference to the loaded sprite private Texture2D spriteTexture; // The position to draw the sprite private Vector2 spritePosition; protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. _spriteBatch = new SpriteBatch(GraphicsDevice); spriteTexture = Content.Load<Texture2D>("Character"); spritePosition = Vector2.Zero; }
In the Game.Draw method, you will see a call to GraphicsDevice.Clear, which clears out the screen for the next frame.
After GraphicsDevice.Clear, call SpriteBatch.Begin on your SpriteBatch object to ready the next batch of textures to draw.
Call SpriteBatch.Draw on your SpriteBatch object, passing the texture to draw, the screen position, and the color to apply.
Tip
Color.White is used to draw the texture without any color effects.
When all the sprites have been drawn, call SpriteBatch.End on your SpriteBatch object.
protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here _spriteBatch.Begin(); _spriteBatch.Draw(spriteTexture, spritePosition, Color.White); _spriteBatch.End(); base.Draw(gameTime); }
When run, you should now see the character drawn in the top-left of the screen.
Note
Actually, what you should see is the character drawn from the top-left of the screen. This is because (by default) the Top-Left corner of the sprite is the origin and the sprite is drawn from that position. The Animated Sprite example shows a simple way of offsetting the drawing of a sprite so that its middle is used as its origin instead.
Moving the sprite
To move the sprite, simply update the spritePosition
to a new location, a quick and easy way to do this would be to detect when the arrow keys are pressed and change the position, like so:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Keys.Right))
{
spritePosition.X += 1;
}
if (state.IsKeyDown(Keys.Left))
{
spritePosition.X -= 1;
}
if (state.IsKeyDown(Keys.Up))
{
spritePosition.Y -= 1;
}
if (state.IsKeyDown(Keys.Down))
{
spritePosition.Y += 1;
}
base.Update(gameTime);
}
See How to detect input for more information on working with the various inputs that MonoGame supports.
See Also
Concepts
Reference
(Character by upklyak
from FreePik)