Geeks With Blogs

News

Google My Blog

Catch me at: The List!


My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer


Archives
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.)

image

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:

image

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.Begin();
        spriteBatch.Draw(redMushroom, new Vector2(0, 0), Color.White);
        spriteBatch.End();

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.

image

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

# play
Requesting Gravatar...
Play the most popular free online titans game it is the interesting on the online i sure you create the best score to play these hard many players excited to connect this free games mahjongg titans thanks for the visit the online play this i wish that the best experience for your friends.
Left by game on Nov 19, 2017 5:42 AM

# re: 6. MonoGame - Putting a Sprite Onscreen
Requesting Gravatar...
Howdy I am so empowered I uncovered your website web page, I truly uncovered you by bad up, while Iwas considering on Yahoo for something interesting,
Indian Super League 2017 live streaming
Bengaluru FC 2017 Schedule
Left by pritampyare on Dec 06, 2017 5:41 AM

# re: 6. MonoGame - Putting a Sprite Onscreen
Requesting Gravatar...
This is the first time I came across such a program. From this blog I came to know more about the MonoGame and the requirements for installing the program. However there are no clear indications for the uses of the program here. snoring mouth guard
Left by Taylor Shaw on Jan 04, 2018 4:14 AM

# re: 6. MonoGame - Putting a Sprite Onscreen
Requesting Gravatar...
I don't know about the MonoGame which are offering game app, but I know about australian academic writing sites sites who are offering the writing services in everywhere.
Left by Steven on Feb 02, 2018 11:37 AM

# re: 6. MonoGame - Putting a Sprite Onscreen
Requesting Gravatar...
Aah! I still remember those days. Those were the best days. Thanks for reminding me those beautiful days.
Left by Alice on Mar 27, 2018 6:03 AM

# re: 6. MonoGame - Putting a Sprite Onscreen
Requesting Gravatar...
Is there anyone who knows how to do this? I needsome practical help. Thanks in advance. luxury hotels near me
Left by Alice on Mar 27, 2018 6:04 AM

Your comment:
 (will show your gravatar)


Copyright © Chris G. Williams | Powered by: GeeksWithBlogs.net