Continuing with our Game Development Theme Week, we revisit one of our long term favorite game development platforms with this post from Dean Ellis.
Game development is what got many developers into programming. But how many of us actually ever learned how to create games? Creating games can be challenging, but it doesn’t have to be that way! MonoGame is a cross-platform gaming framework based on Microsoft’s XNA framework that’s extremely easy to learn. Best of all, games you build with MonoGame will run on iOS, Android, Mac OS X, tvOS, Windows, Linux, PlayStation 4, and more—write once, play anywhere.
In this blog post, we’re going to walk through each step required to create your first game from start to finish, creating a user interface to adding game logic. The object of the game is to tap the monkeys as they appear to keep them from running off with your bananas. If you leave any monkey untapped for more than five seconds, the monkey takes your bananas, and the game is over! Think of it as Whack-a-Mole, but with monkeys!
File -> New Game
To get started, we first need to make sure we have both Xamarin and the latest development build of MonoGame installed. For this tutorial, we will be using Xamarin Studio, though the exact same steps apply when building games with MonoGame in Visual Studio as well. [GD: Emphasis Added]
First, we need to create a shared project where our game logic will reside that can also be shared with all target platforms we want our game to run on. Within Xamarin Studio, choose File -> New Solution -> MonoGame -> Library -> MonoGame Shared Library and name the project MonkeyTap. We must add projects for all of the platforms we wish to target.
Add a “MonoGame for Android Application” project to the solution and name it MonkeyTap.Android. If our game needed to take advantage of native Android features, such as NFC, the platform-specific project is the place to do so. Because our game doesn’t utilize any of these components, we can delete the
Game1class created in the Android project and add a reference to the shared project we just created earlier.
Hit F5 or Cmd+Enter to run the app on your selected emulator or the Xamarin Android Player. You should see a lovely blue screen. Now that our solution is properly configured, it’s time to get started writing our game!
Anatomy of a Game
Many different components work together to create a game. The
Game1class contains the main logic for our game, and is made up five main methods. Each serves its own purpose in making sure a game functions properly, from displaying art and playing sound effects to responding to user input and executing game logic.
Game1is made up of five main methods:
Adding Game Assets
No game is complete without textures and sound effects, known in game development as “content” or “assets”. Most gaming frameworks have a content pipeline, which is just used to take raw assets and turn them into an optimized format for your game. The
Content.mgcbfile in the Content folder is MonoGame’s content pipeline. Anything added to this file will be optimized and included in your final application package.
Creating MonkeyTap User Interface
Loading game assets is done through a
ContentManager, which is exposed by default via the “Content” property of the
Gameclass. First, let’s import some necessary namespaces and declare fields to store our game assets:
Run the app, and you should see a grid full of monkeys!