TOC

This article is currently in the process of being translated into Polish (~19% done).

Creating a Game: SnakeWPF:

Introduction

Krótkie o tym artykule wprowadzającym: Będziemy implementować klasyczną grę Snake w WPF, a końcowy rezultat będzie wyglądał mniej więcej tak:

Tworzenie gier jest często powodem, dla którego wiele, szczególnie młodych osób ciągnie do programowania. Na początku pojawia się pytanie: jak zacząć, i czego do tego startu potrzebuję? Na pewno niezbędny będzie język programowania, jak C#, C++, lub jeden z wielu popularnych języków - jeśli jesteś biegły w swoim preferowanym języku, tak naprawdę nie potrzebujesz niczego innego - zacznij od początku, od dodawania pikseli na ekranie w konkretnych miejscach, a stąd już niedaleko do działającej gry.

Jednakże, większość osób twierdzi, że przydałaby jej się odrobina pomocy z tą niskopoziomowością. Ktoś słusznie zapyta: Dlaczego ręcznie rysować piksele na ekranie, skoro jest już biblioteka/framework który może zrobić to za nas, a my moglibyśmy w międzyczasie skupić się na tworzeniu fajnej gry? Dostępnych jest wiele frameworków mogących w tym pomóc i jednym z nich jest WPF.

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!