Book Image

Building your First Mobile Game using XNA 4.0

By : Brecht Kets, Thomas Goussaert
Book Image

Building your First Mobile Game using XNA 4.0

By: Brecht Kets, Thomas Goussaert

Overview of this book

With the dawn of the Windows Phone 7 platform, Microsoft has offered us an easy way to create 3D mobile games. In this book, we will build a 3D game for Windows Phone 7 together, taking full advantage of the graphics and touch capabilities, along with the sensors of the platform."Building your First Mobile Game using XNA 4.0" is the book for starting game development on the Windows Phone 7 platform. This book will go over the technical aspects of building games along with designing your own framework. Finally we'll build an actual game together from the ground up! This book will set future mobile game developers in the right direction.The XNA framework empowers us to build 2D and 3D games for PC, Xbox 360 and Windows Phone 7. We will use those capabilities to create stunning 3D games for the Windows Phone 7 platform. We will start by covering the basics like drawing graphics, followed by building a custom framework and end with building a game together!In this book, we will cover drawing 2D and 3D graphics, both static and animations. We will also cover the various ways of handling user input and help set the mood of our game playing both 2D and 3D sound, and accessing the user's media library. The only thing left before building a game is covering several techniques created for making our life easier while building the game, whilst building a framework to do just that. Finally, we'll build a 3D game together that will run on the Windows Phone 7 platform."Building your First Mobile Game using XNA 4.0" is the book you need to get started with mobile game development for Windows Phone 7. Its hands on approach will set you on your way in no time. Let's build some games!
Table of Contents (16 chapters)

The Game class


Let's go over the Game class that XNA Game Studio created for us. This class inherits from Microsoft.Xna.Framework.Game. This is our main game class. This class has some fields and methods that have been created for us.

Fields

Our game class has two fields, a GraphicsDeviceManager field and a SpriteBatch field. The GraphicsDeviceManager class will handle the configuration and management of our graphics card, so we don't have to. The SpriteBatch class we can use to draw 2D graphics.

Constructor

The constructor is called once at the start of the game. It creates a new GraphicsDeviceManager instance and sets the root directory for our content manager. This property will determine where the content is loaded from when calling Content.Load<>(""). We will use the content manager later on to load content. The constructor also sets the default framerate, being 30 frames per second for Windows Phone.

public MainGame()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
    // Frame rate is 30 fps by default for Windows Phone.
    TargetElapsedTime = TimeSpan.FromTicks(333333);
}

Initialize

We can use the Initialize method to initialize all non-graphics related content and query for services. By default, the Initialize method of the base class gets called. This makes sure all components are initialized as well. Make sure you create your objects before calling Initialize on the base class.

protected override void Initialize()
{
    // TODO: Add your initialization logic here
            
    base.Initialize();
}

LoadContent

We can use the LoadContent method to load our content. This will also be called once per game. In this method, we can use the content manager to load a variety of content—textures, models, and sounds for instance. By default, the spriteBatch field gets instantiated.

protected override void LoadContent()
{
    // Create a new SpriteBatch
    spriteBatch = new SpriteBatch(GraphicsDevice);
}

UnloadContent

We can use the UnloadContent method to unload any content. As most of the content we will use, uses the content pipeline, we won't have much use for this method. The UnloadContent method gets called once per game.

protected override void UnloadContent()
{
    // TODO: Unload any non ContentManager content here
}

Update

The Update method will run multiple times, and to be more specific, it will do its best to run 30 times per second (depending on the TargetElapsedTime property). In this method we will perform all calculations we need, such as collision detection, updating positions, playing sound, and gathering input. The method has one argument, an object of type GameTime. This object contains the total amount of time since the game started, as well as the time passed since the last update was called. We will use this object to make sure our game logic is frame rate independent. This means our object will move at the same speed, regardless the current frame rate—for example, if the Update method fails to be executed 30 times per second, we want our hero to move just as fast as if it were executing 30 times per second.

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==ButtonState.Pressed)
        this.Exit();
    // TODO: Add your update logic here
    base.Update(gameTime);
}

Also note that in the Update method, we exit the game when the back button is pressed on the gamepad with index one. The interesting thing here is that the back button of the phone registers as the back button of the gamepad.

Draw

As the name suggests, we will use the Draw method to render our objects to the screen. By default, XNA will try to call this method 30 times per second on Windows Phone. Depending on how much you are drawing, this could be less. The method also has a GameTime object as argument, which provides a snapshot of the timing values. The first thing that happens in the method is that the graphics device gets cleared. Each time Draw is called, we render to a render target. A render target can be seen as a 2D image, or a whiteboard. Each time we want to draw a new frame, we need to clear the render target, or wipe the board. The Clear method takes a color as argument, by default cornflower blue. This means our background will be cornflower blue. After clearing our render target, we can draw all our graphics and call Draw on our base class. The default render target is the screen, but we don't have to worry about that just now.

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    // TODO: Add your drawing code here

    base.Draw(gameTime);
}