Get started writing games for Windows 10 - Part 1
If you've been writing iOS or Android games and apps, you might have glanced at Windows and though "Hmm, well, Windows is all C# and XAML, so unless my game is 'Database Simulator V' that's not going to be much fun". And you might have a point: it's true XAML isn't suited to apps like games, but on the other hand, there now more frameworks and tools available on Windows than ever before. I'd go so far and say there's never been a better time to start writing games for Windows. If anything, with so many different choices, it can be rather confusing to know how to get started.
So, let's get down to some basics and ask the obvious question: "What should you use to write a game that you want to publish in the Windows Store?"
The answer you get is going to depend on who you ask, and what kind of game you want to write. 2D? 3D? It will also depend on if you want to go cross-platform, and it will depend on your choice of programming language.
But since I'm writing this, you're going to get my opinion. My favorite type of game to write is 2D and Sprite based. I like the retro classics, and I've writing them since before they were retro. So, on iOS I am a big fan of the SpriteKit framework. I've been looking around for the closest equivalent, and the answer seems to be MonoGame. MonoGame, if you didn't know, is what happened to Microsoft's XNA game framework. MonoGame is now open source and cross-platform. You can even use Xamarin to write MonoGame apps and run them on everything from iPhones to Xboxes.
So let's start here. I'm assuming you have Windows 10 running, and Visual Studio 2015 installed.
Part 1 - Getting the tools installed
1. Install MonoGame 3.5 for Visual Studio from MonoGame.net
2. Launch Visual Studio 2015, and go to File / New / Project
3. Under the Visual C# project templates, select MonoGame and MonoGame Windows 10 Universal Project
4. Pick a name for your app, and click on OK.
5. Have a little cry because the template is broken and you get an error. :-(
6. Have a cup of tea. Resolve to fix this.
7. Find the MonoGame templates. They have (perhaps unexpectedly) been installed into: C:\Users\USER\Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C#\Visual C#\MonoGame
8. Unzip the template WindowsUniversal.zip, and explore its contents.
9. Find the file called WindowsUniversal.vstemplate and open it in a XML editor.
10. Locate the XML that looks like this, and delete it:
<wizardextension>`` <assembly>Microsoft.VisualStudio.WinRT.TemplateWizards, Version=14.0.0.0, Culture=neutral, PublicKeyToken = b03f5f7f11d50a3a</assembly> <fullclassname> Microsoft.VisualStudio.WinRT.TemplateWizards.ApplicationInsights.Wizard\</fullclassname></wizardextension>
11. Save the file. Delete the original template file WindowsUniversal.zip.
12. Restart Visual Studio.
13. Go to File / New / Project
14. Under the Visual C# project templates, select MonoGame and MonoGame Windows 10 Universal Project
15. Pick a name for your project (different from Step 4), and click OK.
16. Your project will be created. It will look as though there are errors, until you build it for the first time as this triggers the installation of some nuget packages.
17. Make sure x86 and Local Machine are set as a target platform, and build and run the empty project: if it's working, you will see a beautiful, blue window.
Part 2 - Getting something onto the screen
1. Starting with your empty MonoGame project, drag in a PNG image to act as a sprite. Place the image in the Content directory for now.
Note: MonoGame supports a "content pipeline", which is a separate tool that processes images and sounds and other files into a resource that your app can use. For larger projects, it makes sense to use the pipeline. For small apps like this, directly loading the image from file is fine.
2. Open the file Game1.cs, which the template has created for you. This file contains the main functions that are called when are app is running, including Initialize() , Update() and Draw() .
3. Let's create a texture - the property that stores an image. Under
SpriteBatch spriteBatch;
add:
Texture2D spaceshipTexture;
4. In the LoadContent() method, add the following code to load the PNG file and assign it to a Texture2D object:
using (var stream = TitleContainer.OpenStream("Assets/SpaceShip.png")) { spaceshipTexture = Texture2D.FromStream(this.GraphicsDevice, stream); }
5. Now we can draw the texture by updating the Draw() method to include the following:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Vector2 spritePosition = new Vector2(100, 100);
Color tintColor = Color.White;
spriteBatch.Draw(spaceshipTexture, spritePosition, tintColor);
spriteBatch.End();
base.Draw(gameTime);
}
That's how easy it is to create a sprite and draw it, using MonoGame. What next? Let's see how to move and control things. Stay tuned!