Exciting times! Starting today, Windows 10 is available for everyone: During the next twelve months, people can upgrade for free if they’re running Windows 7 or Windows 8 on their machine. What’s in it for us?
Well, we can see that Microsoft’s efforts of rewriting the Windows kernel over the past few years have really paid off: With Windows 10, universal apps enable each and every one of us to develop a single app and run it on phones, desktops and consoles alike!
I found that this is the perfect time to start a new big blog series about developing multi-platform online games. In this series, we’ll learn how to build a classical strategy game on Windows 10 using a (free!) Microsoft Azure backend. Azure enables us to create a backend that scales to arbitrary user counts, is easy to connect to various client platforms (not just Windows) and comes with full tool support for maximum productivity. Azure is a huge platform, and this can really be a little overwhelming – it was for me, and I think it will be even for more experienced developers. So we’re going to go through this step by step.
We’ll talk about building and deploying client and server, authenticating users, implementing the game logic itself on both sides, and sending push notifications, among others. Whenever you’ve got any questions, or are aching to see a specific feature, just add your thoughts to the comments below!
Okay – let’s do this!
Setting Up The Backend
Many of today’s games are online. This is for several reasons: First, we love to play with others. For thousands of years, we have been playing games with our friends and family, and interestingly enough, single-player games are a phenomenon that has just been introduced a few tens of years ago with computer games.
However, we’re leaning more and more towards connected online experiences again today. We love to play massive online multiplayer games such as World of Warcraft, team-based games like League of Legends or Counter-Strike and highly competitive 1v1 games such as StarCraft II. But also, we love to socially engage with our peers in social games such as Farmville, or compete with our friends in mobile games such as Candy Crush Saga.
Clearly, in order to be able to interact with other people that are not present in the same room, being connected online helps a lot. But there’s more: Many games embrace the online component to ensure a connected experience across different universes, or to prevent players from cheating in competitive environments, just to name a few.
In our small strategy game, let’s call it Fantasy Kingdom, we’re going to use Microsoft Azure as gaming backend in the cloud. Azure comes with a lot of features that, while not being originally designed and tailored towards games, will help a lot with building a clean architecture and providing a great experience for the player.
First, we’re going to set up the Azure backend for our game. If you haven’t got an Azure account yet, you can start with the free trial – no strings attached.
- Log in to your Azure Portal at https://portal.azure.com/ Here we can manage and monitor all of our apps, create new ones, check our current billing status or ask for help if anything’s missing.
- At the top-left of the page, click +NEW.
- Azure features a whole lot of different services, including web applications, databases, analytics, authentication, and many more. For now, select Web + Mobile > Mobile App.
- Enter a name for your new game.
- We’re going to need a new database for our game. In this database, we’re going to store all of our game data, such as the costs of buildings and the time required for upgrading them, as well as the user-specific data such as their current level or resource amount. Click Package settings > USERDATABASE > Create a new database.
- Enter a name for your database, select the lowest pricing tier and configure the required settings of your new server as desired.
- Create a new App Service Plan. An App Service plan represents a set of features and capacity that you can share across your apps: If you want to save money on your testing environment, you can share a plan across multiple apps. On the other hand, you can also maximize throughput for your production environment by scaling it across multiple regions and plans later.
- Click Pricing Tier > View All and select Free.
- Click Create.
Azure will now set up your new mobile app and notify you when it’s finished.
That way easy, right?
Setting Up Visual Studio
Next, we need to create the client and server projects for Visual Studio. As with most online games, we’re going to separate the game client from the server logic, both in code and physically.
- If you haven’t already done so, download and install Visual Studio 2015 Community Free from https://www.visualstudio.com/downloads/download-visual-studio-vs.
- In your Azure Portal, click Add Client > Windows (C#) > Create A New App.
- Azure provides two Visual Studio solutions so we don’t have to start from scratch every time again. Download and unzip both the server and the client project.
First, we’re going to build and run the server project. The game server will be responsible for handling all client requests, such as building a new construction or attacking another player. It is also the server who’s always in charge: If any player would be tempted to cheat and send a forged request to gain some additional resources, it’s up to the server to validate and reject that request. Now, we want to make sure that the server can handle an arbitrary amount of concurrent players, so we’ll better think of some clever way of ensuring that everything’s running smoothly.
- Open the server solution in Visual Studio (ending with _Runtime).
- Build and run the solution. This will cause Visual Studio to download all additional required packages with the NuGet Package Manager.
- If everything goes well, your default browser will connect to your local server, telling you “This mobile app is up and running”.
Now, we’ll build and run the client project. The game client will allow the player to interact with our game world: It will provide different views for checking their current status, producing resources, building new structures, upgrading existing ones, and interacting with others players. Clearly, the player doesn’t want to know how exactly all of this works, so the client should basically hide all of the implementation details such as the data model or the server communication from the player.
With the new Universal Windows Platform, we’ll be able share the client views across many different devices, such as mobile phones or desktop PCs, while being able to explicitly tailor the experience to match the respective platform the best possible way.
- Open the client solution in Visual Studio (ending with _Windows_CS).
- In your shared solution, open App.xaml.cs (below App.xaml), comment out the client settings for connecting to the Azure backend, and uncomment the settings for connecting to the local server.
// This MobileServiceClient has been configured to communicate with the Azure Mobile Service and
// Azure Gateway using the application key. You're all set to start working with your Mobile Service!
//public static MobileServiceClient MobileService = new MobileServiceClient(
// Uncomment this code for configuring the MobileServiceClient to communicate with your local
// test project for debugging purposes.
public static MobileServiceClient MobileService = new MobileServiceClient(
- Ensure the server is still running in your other Visual Studio instance (i.e. the browser was not closed).
- Build and run the client solution. This will cause Visual Studio to download all additional required packages with the NuGet Package Manager.
- The Windows client will show and connect to your local server to fetch the two default items of the Todo example app.
Wow, we did it! We have successfully built a client-server application with an architecture that can easily be extended to multiple target platforms and the cloud.
That’s more than enough for today! Next time, we’re going to port the app to the new Universal Windows Platform, take a closer look at the current project structure and the existing code files, learn how to deploy and connect to our Azure backend, and build the phone client before we’ll finally transform our Todo list app into a cool game!
A few parting words: Today, Microsoft is investing a whole $10 million in global nonprofit organizations. They have already picked nine of them, and are asking you to pick the 10th. I really appreciate what they’re doing here, and I cannot help but say: Vote now, and help us all upgrade our world!
Exciting times. Right?