I have put together a really simple example of using XNA 4.0 in the Microsoft Visual Studio 2010 Express for Windows Phone CTP (MSV2010EWP? Nope, let’s use Phone CTP)

To get started, download the ABSOLUTELY no cost Phone CTP from:

You download some of the other excellent samples from MSDN, but you just need to the Phone CTP for this example.  This is a simple example:image

Open a new project:

Name the project: PinkGirlAppears, or similar

Click OK

You will see a game program appear, this is like XNA 3.1 if you have used the earlier versions of the XNA Game Studio.  However, you can’t utilize projects you created in XNA 3.1 because they won’t convert directly.  There are some differences in how content is handled, positive changes.  You will need to add an image named PinkGirl to make this work.

You do this by adding the image in the new Content Project, here are the two steps:

Right click and select Existing Item, navigate to a folder with some images

or use the one included at the bottom of blog:

image 

Your image filename will appear here, in place of Character Pink Girl.png

image 

Now, change the “asset name” properties of Pink Girl so that the code will pick it up (if you don’t do this, your code will not run):

image 

Open the Game1.cs (Game1 Class) delete all of the code in that file by using ctrl+A and then Delete.

Now copy the following and paste it into the empty Game1.cs class, press F5, wait a few seconds, and then a few more seconds, then a Phone should appear. This is the Windows Mobile Phone Emulator, and Pink Girl should appear!

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace PinkGirlAppears
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        /*************************************************/
        /// <summary>
        /// Store the texture or image for pink Girl
        /// </summary>
        Texture2D t_pinkGirl;
        /*************************************************/
        /// <summary>
        /// Store the location for Pink Girl
        /// </summary>
        Vector2 pinkGirlPosition;
        /*************************************************/
        /// <summary>
        /// Used for sizing the screen
        /// </summary>
        Rectangle rectWorld;
        /*************************************************/
        /// <summary>
        /// Matrix to store the screen size
        /// </summary>
        Matrix matrixWorldToScreen;
        /*************************************************/
        /// <summary>
        /// Random number
        /// </summary>
        Random rand;
        /*************************************************/
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);
            // Pre-autoscale settings.
            graphics.PreferredBackBufferWidth = 480;
            graphics.PreferredBackBufferHeight = 800;
            rand = new Random();
            TargetElapsedTime = TimeSpan.FromTicks(333333);
            // on the phone, we want to draw on top of the notification bar,
            // but running in a window on Windows is a better experience.
            graphics.IsFullScreen = true;
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            rectWorld = new Rectangle(0, 0, 272, 480);
            matrixWorldToScreen = Matrix.CreateScale(
                (float)GraphicsDevice.Viewport.Width / (float)rectWorld.Width,
                (float)GraphicsDevice.Viewport.Height / (float)rectWorld.Height, 1);
            base.Initialize();
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            /******************************************************
             * Load the pinkGirl Texture
             \****************************************************/
            t_pinkGirl = Content.Load<Texture2D>("pinkgirl");
            /******************************************************
             * Pink Girl position
             \****************************************************/
            pinkGirlPosition.X = rectWorld.Center.X;
            pinkGirlPosition.Y = rectWorld.Center.Y;
        }
        /********************************************************
         * Removed Unload Content
         \******************************************************/
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            base.Update(gameTime);
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(
                SpriteSortMode.Immediate,
                BlendState.NonPremultiplied,
                null,
                null,
                null,
                null,
                matrixWorldToScreen);
            // draw pink Girl
            spriteBatch.Draw(t_pinkGirl,
                pinkGirlPosition,
                null,
                Color.White,
                1,
                new Vector2(t_pinkGirl.Width / 2, t_pinkGirl.Height / 2),
                1.0f,
                SpriteEffects.None,
                0);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

 

 

***********************