Windows Phone 7 Game Design: Placing the image in a specific point

Windows Phone 7 Game Design: Placing the image in a specific point

  • Comments 2

Now you have an image with no background or alpha channel, well that’s nice.  What are you going to do with it?  If it sits in your picture folder, not doing much.

All right, let’s take a look at placing a specific image in a point certain location.  No software telling the image to move, and so forth.  This is a blog with many images which requires that you scroll down. The code is attached in zip file at the bottom of blog.

Super exciting goal:

image

Starting

First open your version of Visual Studio, in my case I am using Visual Studio Express for Windows Phone (VSESP), normally I use Visual Studio 2010 Ultimate, but to keep this simple, I will make use of the VSEWP.  Open a new project, and create a Windows Phone 4.0 project, name the project whatever you want.

image

Once the new project opens you will see the Visual Studio IDE:

image

Pick Windows Phone 7.1 and then OK

Make sure Solution Explorer is open:

image

Adding an Image

Add an image to the, use Existing Item after right clicking

image

Browse to the image you want to use, mine is the fish from the previous blog:

image

Variables

You will need to add the following variables, find the top of the class and add the two new lines via cut and paste, the t_ and v_ in front of the variables are something that I do to help me remember that one is for texture and one for vectors.  The better practice would be to not use the underscore, I used to the underscore in this scenario.

Code Snippet
  1. public class Game1 : Microsoft.Xna.Framework.Game
  2. {
  3.     GraphicsDeviceManager graphics;
  4.     SpriteBatch spriteBatch;
  5.     Texture2D t_Fish;
  6.     Vector2 v_Fish;

Vectors

The vector needs to be initialized, in the case I selected the “override” as shown in the image:

image

Here is my code:

Code Snippet
  1. protected override void Initialize()
  2.         {
  3.             // TODO: Add your initialization logic here
  4.             v_Fish = new Vector2(100, 100);
  5.  
  6.  
  7.             base.Initialize();
  8.         }

Drawing the Fish

Now we need to draw the fish, since you won’t move the fish, you can simply place it into a spriteBatch cycle as shown in the code, for the spriteBatch. Draw, I used the override as shown in the image:

image

Here is the code:

Code Snippet
  1. protected override void Draw(GameTime gameTime)
  2.         {
  3.             GraphicsDevice.Clear(Color.CornflowerBlue);
  4.  
  5.             // TODO: Add your drawing code here
  6.             spriteBatch.Begin();
  7.             spriteBatch.Draw(t_Fish,v_Fish,Color.White);
  8.             spriteBatch.End();
  9.  
  10.             base.Draw(gameTime);
  11.         }

Finally

If both of us got everything right then you should see, and if you are part of the theological practice that uses fish to indicate that you are member this might be pretty exciting, otherwise, for the others reading this, it might be boring.  However you should be proud that you have seen how to draw an image in XNA.

image

Leave a Comment
  • Please add 8 and 7 and type the answer here:
  • Post
  • Following this tutorial, I noticed that t_Fish wasn't initialized and so I always get an error.

    Also, I don't see the link to the code.

  • I did not find the error. The only thing I didn't understand from the code was the color. I don't get why the fish is "colored"  white in the code but it is not white on the screenshot.

Page 1 of 1 (2 items)
Generic Content

Legal Note:

  1. The author, Sam Stokes, is an employee of Microsoft
  2. There is no guarantee that anything I write is correct, I do try to make sure that what I write is correct.
  3. Use anything written in this blog at your own risk
  4. Test what I write about before using the information
  • Sometimes posts will disappear because I realized that they really don't fit with the total chaos of what I write about
  • Links are tested and available on date of publication, what others do with their links are out of my control

Restrictions:

  • Whatever I write on this blog is unedited by management or anyone, which should be obvious from the randomness of the blog.  I simply write about things I like.
  • Microsoft does not restrict me in anyway, so I generallyblather about whatever I want to.
  • For personal reasons I will not write about:
    • Company confidential stuff, mainly because Microsoft is more transparent than any company I have ever worked for.
    • Other people unless they give me permission to write about them