Snake Game...learn How to Make a Snake Game in C++.


Introduction: Snake Game...learn How to Make a Snake Game in C++.

you may have played snake games in nokia 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 is very simple and easy to make a snake 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 may want to change it.i will show you in step 10 that how to change 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++ :

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 this window,we have to write the name of the project and click on the save 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:::




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";



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;


case 'd':

dir = RIGHT;


case 'w':

dir = UP;


case 's':

dir = DOWN;


case 'x':

gameOver = true;





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:



case RIGHT:



case UP:



case DOWN:






//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;




int main()


while (!gameOver)





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 just have to write what you want instead of #basit in between the inverted comas(" ").

click on the "compile and run" button 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:



    • Oil Contest

      Oil Contest
    • Stick It! Contest

      Stick It! Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    12 Discussions

    Thanks a lot Abdul :)

    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!

    3 replies

    I got some errors in the above code ! worked on that but can't solved so i found anothe one:

    I always knew that programming is about copy and paste...

    3 replies

    if you want to learn the complete process.... i will also teach you in future instructables.

    please vote for me.

    sorry for that but i am unable to write that in the instrucble....

    you can copy from here...

    after evry #include , write the following respectivly