Thursday, October 28, 2010

Write Windows Phone 7 game – for beginners (Part I)

Over the weekend there was a rainy weather, so I lighted up the fireplace in the living room, turned on the TV and occupied the couch with my laptop to check, what news we’ve got with Windows Phone 7 development tools.

Just to learn the WP7 programming news I started with a light version of blackjack game for WP7 – even I haven’t one yet, just using emulator. It was surprisingly easy and I had fun. So there is the description how I did it:

After developer tools succeeded just start Microsoft Visual Studio 2010 and create a new Windows Phone Game project:
image

Let’s call the project BJLight for Blackjack Light. We will learn the WP7 game programming building a light version of the old known card game – Blackjack.

Visual Studio 2010 creates new solution with all the required projects and files for a new WP7 game:

image

First rename Game1.cs to BJGame.cs as the only game contained in the project: just edit it in Solution Explorer.

You will be asked:

image

Confirm with “Yes”: you will see the file name changed in Solution Explorer:

image

… and source code will be updated as well:

image

Open “BJLite” project properties to set some common and initial configuration parameters of the game:

image

  • Ensure the “Game startup type” is “(Not set)” – this will enforce the only Game derived class to be launched on game start: in our project this is BJLite class. Note: if there are many Game derived classes in the project, you can specify, what class should be launched on game start.
  • The thumbnail of the game appears on WP7 display as the control to launch game. Let us fill this thumbnail with appropriate image to identify our game:

image

The do the same with Game.ico:

image

The project should be signed – this is a good programming style.

image

Select “New” key file and enter as follows:

image

A new key file will be created and added to project:

image

After initial project configuration is set, let us start with game design.

One of the key methods of the BJLite class instance is Draw(). This method is called each time the game requires the display to be redrawn:

/// <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);

   // TODO: Add your drawing code here

    base.Draw(gameTime);
}

Since we design a card game, let us emulate a casino gambling table and repaint it green:

/// <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.Green);

   // TODO: Add your drawing code here

    base.Draw(gameTime);
}

Start the solution with F5: the WP7 emulator appears and shows the newly created game screen - gambling table:

image

Click on the “Back” button and see the game thumbnail:

image

Clicking on the thumbnail you start the game.

The WP7 emulator controls allow rotate the emulator to get appropriate screen orientation:

image

We will design the game for landscape orientation.

(more to come)

No comments: