TOC

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

Creating a Game: SnakeWPF:

Introduction

Egy gyors összefoglaló erről a bevezető cikkről: létrehozzuk a klasszikus Snake játékot WPF-ben , miután a végső eredmény így fog megjelenni:

A legfőbb motiváció amiért sokan - leginkább fiatalok - elkezdenek programozást tanulni az a játékok készítése. Általában mindeközben a legfőbb kérdés, ami felmerül : Hogy kezdjünk neki vagy mire is van ehhez szükség? Nos, elsősorban szükség van egy programozási nyelvre, mint például : C#, C++ vagy bármi egyéb közismert nyelv. Amennyiben megfelelően tapasztalt vagy a választott nyelvben, akkor igazából semmi másra nincs is szükség: csak kezdj neki az elején , hozz létre pixeleket a képernyőn és egy idő után lesz egy működő játékod.

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!

Ha nem ismered a Snake játékot , minden okom megvan arra feltételezésre, hogy sosem volt Nokia mobiltelefonod a késői 90-es , korai 2000-es években. Az első Snake verzió már jópár évvel korábban elkészült és bemutatták, de csak akkor lett igazi sláger, amikor Nokia úgy döntött, hogy a saját verzióját a az összes telefonjukban elérhetővé teszik.

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!