loading

you may have played snake games in nokia mobiles.in this instructables, you will learn how to make a snake game in c++ programing language.

don't worry, if you have no programing skills and you dont know about c++.it is very simple and easy to make a snake game.you just have to follow my steps as i have done.

<>

my name is Hafiz Sayed Abdul Basit.i am 15 years old from Pakistan. I have some knowledge about programing and programing language (c++).

>>what we want in the game<<

we want to make a game in which there is a snake which has a head and a tail.the snake has to eat the food or any other thing to increase the score,,,as the score increases,,,the size of the snake should also increases.if it hits the boundary,,it should comes out from the opposite side of the boundary.if the snake hit its tail.the game will over.

===================================================================================

as you can see my name on the borders in the game screen.you may want to change it.i will show you in step 10 that how to change it.you can also change the speed of the snake.

Step 1: Video....

you can also watch the video to learn how to make this snake game.

Step 2: Install or Open "dev C++"

open dev c++ in your computer.if you don't have installed "dev C++". download it from the link below.

dev c++ : http://bloodshed-dev-c.en.softonic.com

now install and run it.

Step 3: New Project Button...

click on the new project button,in the left upper corner of your screen as marked (red) in picture.a new window will appear.

Step 4: Starting New Project...

this window will appear after clicking on the new project button.NOW you have to chose console application and click on the "Ok" button.

Step 5: Saving Project...

we should always save our project before we start.in this window,we have to write the name of the project and click on the save button.by default,it will save your project in the "document" folder on you PC.

Step 6: Deleting Extra Text...

delete the existing text in the dev c++

Step 7: Programing Code (c++)

:::copy the code below:::

#include

#include

#include

using namespace std;

bool gameOver;

const int width = 50;

const int height = 20;

int x, y, fruitX, fruitY, score;

int tailX[100], tailY[100];
int nTail;

enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN};

eDirecton dir;

void Setup()

{

gameOver = false;
dir = STOP;

x = width / 2;

y = height / 2; fruitX = rand() % width;

fruitY = rand() % height;

score = 0;

}

void Draw()
{

system("cls"); //system("clear");

for (int i = 0; i < width+10; i++)

cout << "#";

cout << endl;

for (int i = 0;i < height; i++)

{

for (int j = 0; j < width; j++)

{

if (j == 0)

cout << "#basit";

if (i == y && j == x)

cout << "O";

else if (i == fruitY && j == fruitX)

cout << "B";

else

{

bool print = false;

for (int k = 0; k < nTail; k++)

{

if (tailX[k] == j && tailY[k] == i)

{

cout << "o";

print = true;

}

}

if (!print)

cout << " ";

}

if (j == width - 1)

cout << "#basit";

}

cout << endl;

}

for (int i = 0; i < width+10; i++)

cout << "#";

cout << endl;

cout << "Score:" << score << endl;

cout << "made by hafiz sayed abdul basit";

cout << endl;

cout << "use w,a,s,d to control the snake";

}

void Input()
{

if (_kbhit())

{

switch (_getch())

{

case 'a':

dir = LEFT;

break;

case 'd':

dir = RIGHT;

break;

case 'w':

dir = UP;

break;

case 's':

dir = DOWN;

break;

case 'x':

gameOver = true;

break;

}

}

}

void Logic()
{

int prevX = tailX[0];

int prevY = tailY[0];

int prev2X, prev2Y;

tailX[0] = x;

tailY[0] = y;

for (int i = 1; i < nTail; i++)

{

prev2X = tailX[i];

prev2Y = tailY[i];

tailX[i] = prevX;

tailY[i] = prevY;

prevX = prev2X;

prevY = prev2Y;

}

switch (dir)

{

case LEFT:

x--;

break;

case RIGHT:

x++;

break;

case UP:

y--;

break;

case DOWN:

y++;

break;

default:

break;

}

//if (x > width || x < 0 || y > height || y < 0)

// gameOver = true;

if (x >= width) x = 0; else if (x < 0) x = width - 1;

if (y >= height) y = 0; else if (y < 0) y = height - 1;

for (int i = 0; i < nTail; i++)

if (tailX[i] == x && tailY[i] == y)

gameOver = true;

if (x == fruitX && y == fruitY)

{

score += 10;

fruitX = rand() % width;

fruitY = rand() % height;

nTail++;

}

}

int main()
{

Setup();

while (!gameOver)

{

Draw();

Input();

Logic();

Sleep(60); //sleep(60); //recomended speed is 40 to 80;

}

return 0;

}

Step 8: Paste the Copied Text...

now you have to paste the copied text in the dev c++.

Step 9: Compile and Run.....

please see that there is

after the #include in the start(from where program begins)

if there is not written that......please write those after every #include , #include , #include respectively.

then click on the "compile and run" button as you can see in the picture.

Step 10: Ready to Play.....

i am sure that there is no error n the program.but if there is any error please write your error in comment.

now you may want to remove my name from border.and in the last line.


lets do it.....

Step 11: Open Dev C++.

as you can see in the picture that there is...

cout << "#basit" ;

two times in your snake game program.you just have to write what you want instead of #basit in between the inverted comas(" ").

click on the "compile and run" button again.it will show the text you have written in the program on the borders of your game.

Step 12: Enjoy Your Game...share With Your Friends,,,

now you can play your game.

how to play??

use "w" for up, "a" for left, "s" for backward or stop, "d" for right. do not use upper case letters.

want to change the speed???

just go to the end of the program where you will see

sleep (60);

just write you required speed in the brackets instead of 60

my recommended speed is 30 to 90.

Step 13: Thanks,,,

please forgive me for my any mistake and vote for me...

Step 14:

<p>Where is the source code?????</p>
<p>Source code is available in next step</p><p>Click on &quot;view all steps&quot;</p><p>You will see the source code</p>
Voted! Computer programming is an excellent skill. So is English. Keep up the great work and you will be a success in whatever you do!
<p>thanks,,,</p>
Canio.h is not working
<p>same here</p>
<p>I got some errors in the above code ! worked on that but can't solved so i found anothe one: http://www.infoopoint.com/snake-game-using-c-complete-code/</p>
<p>I always knew that programming is about copy and paste...</p>
<p>if you want to learn the complete process.... i will also teach you in future instructables.<br><br>please vote for me.</p>
<p>the problem is i can not see what comes after the includes</p>
<p>sorry for that but i am unable to write that in the instrucble....</p><p>you can copy from here...</p><p>after evry #include , write the following respectivly</p><p>&lt;iostream&gt;</p><p>&lt;canio.h&gt;</p><p>&lt;windows.h&gt;</p>

About This Instructable

7,121views

14favorites

License:

More by sayed abdul basit khan:snake game...learn how to make a snake game in c++. how spider silk is harvested 
Add instructable to: