TOC

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

Création d'un jeu : SnakeWPF:

Introduction

Un résumé rapide de cet article d’introduction : nous allons mettre en œuvre le jeu classique Snake dans WPF, et le résultat final ressemblera à ceci :

Créer des jeux est souvent la raison pour laquelle beaucoup de gens, en particulier les jeunes, sont attirés par l’apprentissage de la programmation. Mais la question est généralement : comment puis-je commencer et de quoi ai-je besoin ? Eh bien, vous avez besoin d’un langage de programmation, comme le C, le C ou l’un des autres langages populaires, et si vous parlez couramment votre langage de programmation préféré, vous n’avez pas vraiment besoin d’autre chose : il suffit de commencer par le bas en ajoutant des pixels à l’écran et à un moment donné, vous pourriez avoir un jeu opérationnel.

Cependant, la plupart des gens préféreraient un peu d'aide avec le langage de bas niveau. Pourquoi ajouter des pixels à l'écran manuellement s'il existe une bibliothèque ou un framework qui peut le faire pour vous, afin que vous puissiez vous concentrer sur la construction d'un jeu divertissant ? Il existe de nombreux framework qui vous aideront à le faire, et en fait, l'un d'eux est le framework 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!