13,148,214 members (50,872 online)
alternative version

#### Stats

12.1K views
6 bookmarked
Posted 11 Sep 2013

# The Snake

, 15 Sep 2013
 Rate this:
Moving the snake by a timer and some labels

## Introduction

This article is about make a motion by a timer that changes the location of some labels that simulate a snake. The snake should get some targets (that placed random) and has a special time to do this. it couldn't go out of the field and become bigger and faster by spending the time.

the best part that i ask too see them:

• The idea that i used too make a smooth and flexible snake
• How did i solve the delay problem of key down event
• * How to code for multiple key down (for example Alt + f4) ->i explained at last part of Using the code

## Background

The labels are square and we can not use them to draw a smooth moving snake so i put some of them together And made a flexible rectangle. It is what I've done to design the snake. the labels follow each other with 1 unit distance, if we use more distance in the turns the snake doesn't look good enough so i tried to make it as i said by these codes:

here:

At first  i set the firstly location of head label of snake body  and use as first amount of x and y variable.

```int x = 50;
int y = 100;
```

a is the variable that shows the direction of snake moving that gets in a special key down event, and x,y are the variable that get or set the locations of the snake body. the codes work this way:
1. For example label6 is the head and labels 7,8,9... are the next body labels that follow the head(label6). At first i set first location of label6:

` <span style="font-size: small;">label6.Location = new Point(x, y);</span> `

2. and we suppose the snake is going to right direction (so the event should set a = "r"), the other thing that this event check is that if the user pressed a direction opposite to the moving direction? and if it was true it doesn't change the direction because a snake could not do this.

```protected override void OnKeyDown(KeyEventArgs keyEvent)
{
<span class="Apple-tab-span" style="white-space: pre;">	</span>string key;
<span style="font-size: 9pt;"><span class="Apple-tab-span" style="white-space: pre;">	</span>key = keyEvent.KeyCode.ToString();</span>```
```//checking the direction and set the new direction<span style="font-size: 9pt;">
</span><span class="Apple-tab-span" style="white-space: pre;">	</span>if (key == "Right")
{

if (a != "l")
a = "r";
}
if (key == "Left")
{

if (a != "r")
a = "l";
}
if (key == "Up")
{

if (a != "d")
a = "u";
}
if (key == "Down")
{

if (a != "u")
a = "d";
<span class="Apple-tab-span" style="white-space: pre;">	</span>    }
<span class="Apple-tab-span" style="white-space: pre;">	</span>}
```

3. get back to the labels: so the first label location set, so now we should set the next label location with 1px distance so we do it as the direction of moving like this:

```<span style="font-size: 14px;"> </span><span style="font-size: 14px;"> <span class="Apple-tab-span" style="white-space: pre;">	</span>    </span>
if (a == "r")
{
x = x + 1;
}
if (a == "l")
{
x = x - 1;
}
if (a == "u")
{
y = y - 1;
}
if (a == "d")
{
y = y + 1;
}

label5.Location = new Point(x, y);```

4. and the other labels 1px distance to previous one...

```if (a == "r")
{
x = x + 1;
}
if (a == "l")
{
x = x - 1;
}
if (a == "u")
{
y = y - 1;
}
if (a == "d")
{
y = y + 1;
}

label4.Location = new Point(x, y);
if (a == "r")
{
x = x + 1;
}
if (a == "l")
{
x = x - 1;
}
if (a == "u")
{
y = y - 1;
}
if (a == "d")
{
y = y + 1;
}
```

So the snake moving this way.

For database I just used three text files that keep the record, but I delete the type that user can't access them that much easy but i preferred to don't use sql data base for this project.

The random part is the targets location that have random places that should be inside of the snake field:

```Random fisrtX = new Random(DateTime.Now.Second);
Random firstY = new Random(DateTime.Now.Millisecond);```

When the snake catch the target we need new location so I used this.

```int secondX = fisrtX.Next(10, 390);
int secondY = firstY.Next(10, 370); ```

## Using the code

First of all install the Fonts that are beside the project to open it completely.

At the first form (form1) you can see a simple timer that change the back colors of some labels.

At the 2nd, 3rd, 4th forms you can see 3 timers that:

timer1 do the motion parts by getting the side button that user pressed and setting the location of the leader label. The leader label is the first label that moves and other labels will follow each other, this kind of programming make the visual part of snake flexible and real.

timer2 is for game time that when the snake catch the targets it will be added.

timer3 is for making the snake faster and make the game harder, by making the snake longer and adding more labels to follow that make it faster.

At last i wanna explain more about the On key down override, why i use it? and why i didn't use Key down event from the form? and explain the problems that i face when i writing this code and solutions:

1. If you see the game forms, you can see there is no text box, button and any thing that have tab index and i just used Labels instead of button. the reason was the key down event that works when your are selected the form page, but at first one of buttons had been selected and during the game, the user nay select one button that caused to disable key down code so for solution i used some labels.

2. When i my program completed i understand a big problem and that was the delay between key down and the snake moving that make the Hard part boring. so i tried this code and understood that it has very quick feed back in key down and moving

3.* The most big problem that i face in this project was the using of Alt+f4 by user to exit the game! what was wrong? The Timers and the game !!! if the timers didn't stop the game won't be stopped so used a code on the exit button to stop the timers, but what should i do with Alt+f4. you know what will  happened if the user exits by these button during the game. yes when he was at main page a message box shows that you lose!!! so i tried to code for the Alt+f4 by the code that i introduced... i set more codes in the source to check them and use (on key down override)

```string keyCode = "";

if (Alt == "Alt: True" && keyCode != "18")//check the alt is pressed or no
{
keyCode = keyCode + "18";//this is used to not to get the old alt pressed againg
}
if (KeyValue == "KeyValue: 115")//this is the key value of f4 button
{
keyCode = keyCode + "115";
}
if (keyCode == "18115")//chech if alt and f4 are pressed together
{
timer1.Stop();
timer2.Stop();
timer3.Stop();
}
```

*** i used more explanations and comments in the Form2 (easy part) so to check the codes use this form

## Points of Interest

The best part of this project that make me proud is the design of the snake that make if flexible in turns and it's moving and the limitation of getting targets that how much it should be close to catch the targets, I tried over and over to get the proper result.

And as i said other good parts of this project is key down void that i used instead of form key down it make the program faster to feedback the user key pressed. so use that instead of key down event in different  objects.

## Share

 Student Iran (Islamic Republic of)
I'm programming since 2009. i started it from school and c# language and then i learnt working sql and now i'm working on asp.net projects

## You may also be interested in...

 First Prev Next
 Re: My vote of 3 doostl16-Sep-13 21:17 doostl 16-Sep-13 21:17
 Re: My vote of 3 Mohammad Sharify18-Sep-13 5:02 Mohammad Sharify 18-Sep-13 5:02
 Re: My vote of 3 KarstenK21-Jan-14 0:27 KarstenK 21-Jan-14 0:27
 My vote of 2 Richard MacCutchan16-Sep-13 2:15 Richard MacCutchan 16-Sep-13 2:15
 My vote of 4 Shambhoo kumar15-Sep-13 19:47 Shambhoo kumar 15-Sep-13 19:47
 Re: My vote of 4 doostl16-Sep-13 1:41 doostl 16-Sep-13 1:41
 Re: My vote of 4 Shambhoo kumar16-Sep-13 18:22 Shambhoo kumar 16-Sep-13 18:22
 Last Visit: 31-Dec-99 18:00     Last Update: 24-Sep-17 15:25 Refresh 1