Difference between revisions of "Drawing Textures"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "=Declare a Texture= =Load a Texture= =Draw a Texture=")
 
Line 1: Line 1:
 
=Declare a Texture=
 
=Declare a Texture=
 +
One of the built in MonoGame data types is Texture2D, this can be used to store a graphic or image. You will also need to declare a Vector2 to store the on screen position. Declare these within your Game1.cs:
 +
 +
<syntaxhighlight lang=csharp>
 +
// Animation representing the player
 +
Texture2D PlayerTexture;
 +
 +
// Position of the Player relative to the upper left side of the screen
 +
Vector2 Position;
  
 
=Load a Texture=
 
=Load a Texture=
 +
  
 
=Draw a Texture=
 
=Draw a Texture=
 +
Now the Initialize has setup the basic setting, we can use these to draw to the screen. Make the following alterations:
 +
 +
<syntaxhighlight lang=csharp>
 +
public void Draw(SpriteBatch spriteBatch)
 +
{
 +
    spriteBatch.Begin()
 +
    spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f,
 +
      SpriteEffects.None, 0f);
 +
    spriteBatch.End();
 +
}
 +
</syntaxhighlight>

Revision as of 14:54, 18 June 2018

Declare a Texture

One of the built in MonoGame data types is Texture2D, this can be used to store a graphic or image. You will also need to declare a Vector2 to store the on screen position. Declare these within your Game1.cs:

// Animation representing the player
Texture2D PlayerTexture;

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

=Load a Texture=


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

<syntaxhighlight lang=csharp>
public void Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Begin() 
    spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f,
       SpriteEffects.None, 0f);
    spriteBatch.End();
}