TOC

The community is working on translating this tutorial into Persian, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Creating a Game: SnakeWPF:

Introduction

A quick summary of this introductory article: We'll be implementing the classic Snake game in WPF, and the final result will look something like this:

Creating games is often the reason that a lot of, especially young, people are drawn towards learning programming. But the question usually is: How do I get started and what do I need? Well, you need a programming language, like C#, C++ or any of the other popular languages, and if you're fluent in your preferred language, you don't really need anything else: Just start at the bottom by adding pixels to the screen and at some point, you might have a working game.

However, most people would prefer a bit of help with the low-level stuff. Why add pixels to the screen manually if there's a library or framework that can do it for you, so you can focus on building an entertaining game? There are many frameworks out there which will help you do this, and actually, one of them is the WPF framework.

Now granted, WPF is not the most obvious choice when you want to create games - it's definitely a framework that focuses mostly on creating user interfaces for business-oriented applications. But still, there are many elements in the WPF framework that you can use to create a game, and perhaps equally important: You get all the mechanisms to paint and control a Window in Windows.

So, if you're looking to create a simple game, WPF might actually be a fine choice. At least it will be a great help for all the most basic aspects, like creating a Window, drawing a simple area for the game etc. If you want to add stuff like advanced 3D graphics and fast moving objects, might need more help from another library/framework, but it will do just fine for a simple game - for instance, a classic Snake game!

SnakeWPF

As a proof of concept, I have decided to create a WPF-based version of the extremely classical Snake game. It will use a regular WPF Window as its game area, as well as regular WPF controls/shapes to create the actual gameplay. The reason why I chose Snake is because it's fairly easy to implement (there's not that much logic to code) and because it can be implemented using simple geometric figures like squares and circles, which can be used very easily with the WPF framework. But also because it's still a really funny game, despite it's simplistic nature!

If you don't know the Snake game, I can only assume that you never owned a Nokia cellphone during the late 90's/early 2000's. The first version of Snake was written and demonstrated many years before that, but it became a major hit when Nokia decided to include their own version of it on all their cellphones.

The gameplay is as simple as it is entertaining: You move a virtual snake in one direction (left, right, up or down) in the hunt for food (sometimes an apple). When your snake hits the apple, it's consumed, your snake grows and a new apple appears on the screen. If you hit the walls or your own snake tail, the game ends and you have to start all over. The more apples you eat, the higher score you get but the more difficult it will get not to hit your own tail.

There are MANY variations to the gameplay - for instance, the speed with which your snake moves will often increase each time you eat an apple, making it harder and harder, but not all Snake implementations will do this. Another variation is the walls - some implementations will allow you to go through the wall and out on the opposite side, while other implementations will have the game end as soon as you hit the wall.

In our SnakeWPF, the walls are hard (the snake dies if it hits them), and the speed will increase exponentially for each apple you eat, up to a certain point.

Summary

Over the next several articles, we'll be implementing a nice version of the classic Snake game using the WPF framework. We'll start with the background in the next article, and in the end, we'll have our first, fully functional WPF-based game.

Please notice that while this IS a WPF tutorial, we will need a bit more C# code than normal, to implement the game logic etc. I will try to explain most of it as we move along, but in case you need a bit more knowledge about C#, don't forget that we have a nice, complete C# tutorial in our network!


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!
Table of Contents