Textures, Rectangle, Vector2 and comments
Still doing that static thing with images. Also my previous blog on a static image was missing a line of code, so I have a class of students trying to figure out what I did wrong. As to the rest of you, thanks for not telling me that I made a mistake.
Today, let’s see if I can do a really simple discussion of how to use Rectangles with Textures. It is a way to program, some designer do it differently.
First, we are going to use the fish images I found somewhere (and will put in a zip file, so if there isn’t a zip file let me know):
Here is the fish. It is also included in the zip file. As well as second one for you to experiment with.
Adding the images
Add the fish to your project, your project will look slightly different, right click on the Project that says (Content) to the right, then select Add and then Existing Item. This will add the image to the content project that is required in every XNA project. You can another fish or image or even use a copy of the same one, just make sure to rename it to Fish2.
Browse to an image you have collected from the web and select it. Change the filename to fish1, this way we don’t have to change the code. Normally you would change the asset name, but in this case to make it easy this should work. Make sure to add the second image or fish named fish2.
Repeat this for another image and name this image fish2
Add Class Members directly after the Class Definition
The Class Definition is:
- public class Game1 : Microsoft.Xna.Framework.Game
- Class Members are added after the first brace after the Class Definition.
Code Snippet
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- /************************************
- * Class Members, in this area of the
- * code includes the class members, that is
- * fields such as int, float, string
- * object variable declarations such as
- * Texture2D, and so forth
- ************************************/
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- /*********************************************
- * User Added Variables
- *
- * The use of Summary Comments means that the
- * intellisense will remind you of what the
- * variables do. Not important at the time
- * you program, but important when you come
- * back to troubleshoot later
- *********************************************/
- /// <summary>
- /// Texture for the Fish Image
- /// </summary>
- Texture2D t_Fish1, t_Fish2;
- /// <summary>
- /// Vectors for the fish images
- /// </summary>
- Vector2 v_Fish1, v_Fish2;
- /// <summary>
- /// Rectangle for the Fishies
- /// </summary>
- Rectangle r_Fish1;
- /***********************************************/
- /***********************************************/
Initialize variables
Now we need to initialize the object variables that we have declared:
Code Snippet
- protected override void Initialize()
- {
- /************************************************
- * Two different ways to do the vector locatoin
- * First way uses a separate X and Y
- * Second way uses a single number for both X and Y
- ************************************************/
- v_Fish1 = new Vector2(100, 100);
- v_Fish2 = new Vector2(300);
- /************************************************/
- /************************************************
- * Rectangle is initialized so that it can be used
- * to demonstrate the way to use a rectangle to
- * scale an image
- ************************************************/
- r_Fish1 = new Rectangle(100, 100, 140, 50);
- /************************************************/
- base.Initialize();
- }
LoadContent
In this case we are loading the Texture2D with the asset “Fish1”, which we loaded into the (Content) part of the project:
Code Snippet
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- /************************************************
- * Load the Image of the fish, the "Fish1"
- * uses the Asset name of the image, by default
- * it is the Filename, but you can change it.
- ***********************************************/
- t_Fish1 = Content.Load<Texture2D>("Fish1");
- t_Fish2 = Content.Load<Texture2D>("Fish2");
- /***********************************************/
- // TODO: use this.Content to load your game content here
- }
Draw
Now we need to draw the image, we use the rectangle, which has the location included in the r_Fish1 when we initialize the rectangle.
Code Snippet
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- // TODO: Add your drawing code here
- /************************************************
- * Draw the sprites, the spriteBatch could be one
- * or it could be many controlled by other classes
- * Must begin with Begin and stop with an End
- ************************************************/
- spriteBatch.Begin();
- spriteBatch.Draw(t_Fish1, r_Fish1, Color.White);
- spriteBatch.Draw(t_Fish2, v_Fish2, Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
This is fun!
What do we get?
Here is what you will see if you use two fishes, one of the images that I used had a background and the other didn’t. One of the fish (the smaller one) is inside of a rectangle, which is one of the ways to scale the image up or down. Rectangles also give you a way to test for collisions.