Quantcast
Channel: CodesMesh » Syed Wajahat Ali Shah
Viewing all articles
Browse latest Browse all 11

Snake Game In C#

$
0
0

Introduction to snake game:

Snake game is a computer video game and a classic one in which a player moves a snake whose food is displayed at various points on screen and if snake hits boundary wall of screen game overs or even if snake hits itself making a closed loop.
Our snake game will look something like this

C# Snake Game:

Now to make a snake game in C# we need to know following things.

1). How to draw on form in C#

2). How to move a drawn object in C#

3). How to detect,access and process what key user hits.

4). Game logic

 Step 1: How to Draw on form in C#:

You can draw on form in C# very easily first you need to declare a graphics object in form designer under  public class partial form: form1 as shown below


Here we defined a graphic object named paper. Now this paper is our canvas basically on which we are going to draw our snake and its food.

Drawing Snake:

Snake can be thought of as a rectangle shaped object with some width and height. Its width changes but its height remains same now to do it we draw on our paper named graphic object or canvas a rectangle giving it some width and some height. We draw our snake and its food by calling some functions of drawing also made by us in paint event of a form as shown below.


Now their are two methods that we made and gave our paper as their arguments real drawing of objects is done in these methods. We have two classes one for each(food and snake). In food class we draw food using a random number generator and calling fillrectangle() function.


Similarly snake is drawn using drawSnake()  function we made in snake class.


This was all about drawing now we turn to Step 2.

 Step 2: How to move a drawn object in C#

For moving we need a timer which can be found in the inventory or toolbox on left side drag it and place it on form to get a timer. Now we want to move our snake at each timer's tick we do this as shown below.


Here we made a timer tick event and set its time and redrawn snake to its new location by just adjusting and processing coordinates.

Step 3: How to detect,access and process what key user hits

To detect and access what user press for moving snake like up key down key left or right key we go to our form and go to its events window on right side and double click on "key down event" and we entered in to event coding as shown below and code following code.

Step 4: Game Logic

Now we move to game logic. So far we have discussed the drawing of snake and its moving now we need to set this that if snake hits any of the four walls of form its should say "game over". To do this we made a collision function as shown below.


That's it! So you made a snake game of your own make it play it and enjoy it. For any further queries feel free to comment below it will be appreciated. To download a complete working snake game code click here


Viewing all articles
Browse latest Browse all 11

Trending Articles