This post was originally published at How to make a Windows Store game with C# and XAML, part 2

This guest blog post is the second of a 3-part series written by Windows developer Jani Nevalainen. Please see the original 2nd post on Jani’s blog here, and refer to Part 1 here. Stay tuned for Part 3, coming soon. 

Creating the game field and player

In my previous post, we set up the project and created a nice start screen. But games need more than just the start screen. In this post we’ll create a game screen, enabling navigation to it from the start screen, and we’ll also add the player ship to it.

We create the game field by creating a new blank page called GamePage. We add it to the Shared –project (right click, Add, New Item, Blank Page). Because we support Windows and Windows Phone, we need to adjust our game page depending if we’re on a small phone screen or a larger screen such as tablet or desktop. Typically this is done by using VisualState manager, but for the sake of keeping things as simple as possible, on this tutorial we’ll just do the required adjustment in code.

Next step is to add the player ship to Shared project, so create a new user control and name it Ship.

Replace the Ship.xaml with the following:

and then the constructor content in Ship.xaml.cs with:

In that code, we’re using #if WINDOWS_PHONE_APP to define the code which we use to give size to the control when running on phone. And in #else we are defining the shape for Windows app. Currently they are just different size, and we could have handled that in XAML by just stretching the ship accordingly, but this leaves us the option to have a different ship shape in each platform.

Copy the following to the MySpaceInvanders.Shared -project’s GamePage.xaml, overwriting all the existing markup:

The code above divides the field in three different sectors, one for scores and ship controls, one for play field and for the fire button. Copy the following code under the code above:

The code above sets the score, highscore and player moving controls to the screen. Notice that we are using PointerReleased and PointerExited events for controlling the ship, those are the only events which will give you smooth control over the ship movement. Copy the following code under the code above:

The code above sets the playing field, and sets the ship on the screen. Copy the following code under the code above:

Code above draws the firing button on the screen. We’re using two ellipses to give impression of dimension for the button surface.

As mentioned earlier, we’re doing the differences in code to handle phone and desktop/tablet versions. For the GamePage we do it so that we open the GamePage.xaml.cs and edit the constructor public GamePage() and add the following just under the this.InitializeComponent:

Now that we have the UI set, we’ll add some functionality to enable the ship to move later on. Open the GamePage.xaml.cs and add these members to the class:

After that we need to add the event handlers under the constructor:

To enable navigation to game screen from the start page, we’ll open the StartPage.xaml.cs and add to the OnStart -method the following code:

Now we can test that launching the app, and pressing the Start-button should show us a screen with controls and ship, which is still not positioned correctly and not moving.

Stay tuned for Part 3, coming next week.

Originally posted here:  

This post was originally published at How to make a Windows Store game with C# and XAML, part 2