Geeks With Blogs


Google My Blog

Catch me at: The List!

My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Chris G. Williams Beware: I mix tech and personal interests here.

MonoGame - Putting a Sprite Onscreen

If you aren’t familiar with the term “sprite”, it refers to a two-dimensional (2D) graphical image that you can display onscreen. Sometimes they are individual images, and sometimes you can find them on what is called a SpriteSheet.

Here’s an example of a SpriteSheet:

We’ll come back to using SpriteSheets later, since that’s a little more complex than just putting a single image onscreen.

I’m going to use this image for now:

You can just right-click it and “Save As…” to your desktop or wherever, since we’ll be using it shortly.

If you haven’t already, create a new MonoGame Windows Project. (Take a look here for a refresher on the project types.)

Once you have the project created, you’ll want to add your sprite to the Content folder of your project. (right-click, Add,  Existing item…)

Mine is called red_mushroom.png so you should name yours that as well if you’re following along (or, you know, change it in the code… whatever.)


You’ll need to check the properties, and make sure Build Action is set to Content and Copy to Output is set to Copy if newer, like so:


Next, open up the Game1.cs file and add the following variable under the SpriteBatch definition, before the constructor:

        Texture2D redMushroom;

A Texture2D is how MonoGame (and XNA) refer to a two-dimension graphical asset.

Scroll down to the LoadContent() method and add this line:

        redMushroom = Content.Load<Texture2D>("red_mushroom");

The Content.Load<> is a generic that accepts a number of different types, including audio and 3D models. You can leave off the filename extension, or add it, it won’t make a difference, and it automatically looks in the Content folder by default!

Lastly, scroll down to the bottom of the file and add this to the Draw() method, before the call to base.Draw:

        spriteBatch.Draw(redMushroom, new Vector2(0, 0), Color.White);

I’m not going to go in much detail here about SpriteBatch, other than to say it provides a way to send multiple commands efficiently to your graphics card. We’ll dig into the SpriteBatch in a later post. The Draw() command has a lot of overloads, and the one we’re using takes the Texture2D, some coordinates, and a filter color. (Using white means we don’t change it at all. Try changing the color to Red or Blue and see what happens!!)

You’re done! Hit F5 and marvel in the glory of a giant red mushroom on a cornflower blue background.


Next up…

I’ll cover putting some text on screen. If you came directly to this page, you can find the complete list of articles here.

Posted on Thursday, February 16, 2017 1:48 PM Game Development , MonoGame | Back to top

Comments on this post: 6. MonoGame - Putting a Sprite Onscreen

# re: 6. MonoGame - Putting a Sprite Onscreen
Requesting Gravatar...
I was reading your article and wondered if you had considered creating an ebook on this subject. Your writing would sell it fast. You have a lot of writing talent.
madden mobile hack tool online
madden mobile hack coins
Left by dewsw on Mar 28, 2017 9:24 PM

# re: 6. MonoGame - Putting a Sprite Onscreen
Requesting Gravatar...
This article is very useful for all people. Free facebook video downloader, My knowledge is increase after reading article
Left by Alina on Apr 19, 2017 8:53 AM

Your comment:
 (will show your gravatar)

Copyright © Chris G. Williams | Powered by: