# Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This snake game has no graphics in it and it is considered as mini project for the students who are beginners and wants to learn and understand the C language efficiently.

The basic idea to implement and play this game is using W, A, S and D keys drive the snake within the console window and there is fruit which we need to make the snake. The fruit is placed randomly in the console window. The game is played only within the console window.

The game instructions are as follows:

• The snake in the game is represented with the symbol 0.
• The snake can be driven in the console window in any directions by using the keys W, A, S and D. only within the console window.
• The fruit for the snake is represented by *(asterisk) symbol.
• Whenever the snake eats the fruit, the scoreboard increases by 10 points every time.
• The fruit for the snake is placed randomly within the boundaries of the wall.
• The game comes to an end whenever the snake touches the wall of any side.

Some technical aspects for the snake program are:

• User defined functions and Built-in functions.
• Building a boundary for the game.
• Generating randomly placed fruit for the snake.
• Increasing score board after eating fruit.

### User defined functions

We require mainly 4 different types of user defined functions and they are as follows:

• Setup ()
• Draw ()
• Input ()
• Logic ()

Setup ()

This function is used to arrange the fruit for the snake in the random position within the boundary console window.

Draw ()

This function is implemented and deployed for creating the boundary for the required snake game in which the game is played.

Input ()

This function plays the main role in the snake program because this function helps in taking the input values from the user through keyboard.

Logic()

This function is used to allocate and set the movement positions of the snake in the console window.

### Built-in functions

We require two built in C functions and they are as follows:

• Kbhit ()
• rand ()

Kbhit ()

This function in C programming language is used to crosscheck and determine if a key is pressed or not. To implement this function, we need to include the header file conio.h because whenever a key is pressed, it returns a non-zero value or else zero.

Rand ()

This is declared in the header file stdlib.h because whenever the rand () function is declared it returns a random integer value.

We require 3 header files and some variables to declare in the pre processor section and declaration section.

``````#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
``````

And the variable declaration follows as:

``````inti, j ;
int height = 20, width = 20, gameover, score;
int x, y;
int fruitx, fruiyty, flag ;
``````

Let’s deploy the code for the building of the boundaries of the console window:

Code:

``````// C program to deploy and construct the console boundary window for the snake game.
#include <stdio.h>
#include <stdlib.h>
inti, j, height = 10;
intwidth = 10, gameover, score;

// Function to construct the boundary walls for the game
voiddraw ()
{
// system("cls");
for(i = 0; i < height; i++) {
for(j = 0; j < width; j++) {
if(i == 0 || i == width - 1 || j == 0
|| j == height - 1) {
printf ("#");
}
else{
printf (" ");
}
}
printf("\n");
}
}

// Driver Code
intmain ()
{
// Function Call
draw ();

return0;
}
``````

Output:

``````# # # # # # # # # #
##
#                #
#                #
#		 #
#		 #
#		 #
#		 #
#		 #
# # # # # # # # # #
``````

By using all the functions and declaring all the required aspects the whole code for deploying the snake game is:

``````// C program to deploy the complete snake game
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

inti, j, height = 10, width = 10;
intgameover, score;
intx, y, fruitx, fruity, flag;

// Function to generate the fruit inside the boundary walls
voidsetup ()
{
gameover = 0;

// Storing height and width of the window
x = height / 2;
y = width / 2;
label1:
fruitx = rand () % 20;
if(fruitx == 0)
gotolabel1;
label2:
fruity = rand () % 20;
if(fruity == 0)
gotolabel2;
score = 0;
}

// Function to draw the boundaries of the snake game
voiddraw ()
{
system("cls");
for(i = 0; i < height; i++) {
for(j = 0; j < width; j++) {
if(i == 0 || i == width - 1
|| j == 0
|| j == height - 1) {
printf ("#");
}
else{
if(i == x && j == y)
printf("0");
elseif(i == fruitx
&& j == fruity)
printf("*");
else
printf(" ");
}
}
printf("\n");
}

//displaying the score after the game ends
printf ("score = %d", score);
printf("\n");
printf("press X to quit the game");
}

// taking input
voidinput()
{
if(kbhit ()) {
switch(getch ()) {
case'a':
flag = 1;
break;
case's':
flag = 2;
break;
case'd':
flag = 3;
break;
case'w':
flag = 4;
break;
case'x':
gameover = 1;
break;
}
}
}

//logic behind movement
voidlogic ()
{
sleep(0.01);
switch(flag) {
case1:
y--;
break;
case2:
x++;
break;
case3:
y++;
break;
case4:
x--;
break;
default:
break;
}

// game over consideration
if(x < 0 || x > height
|| y < 0 || y > width)
gameover = 1;

//updation of score if snake reaches fruit
if(x == fruitx && y == fruity) {
label3:
fruitx = rand() % 20;
if(fruitx == 0)
gotolabel3;

// after eating the given fruit generate another new fruit
label4:
fruity = rand() % 20;
if(fruity == 0)
gotolabel4;
score += 10;
}
}

// Driver Code
voidmain()
{
intm, n;

// Generate boundary
setup();

// Until the game is over
while(!gameover) {

// Function Call
draw();
input();
logic();
}
}
``````

Output:

``````# # # # # # # # # #
#      *           #
#                 #
#                 #
#		  #
#	0	  #
#		  #
#		  #
#		  #
# # # # # # # # # #
``````
``````# # # # # # # # # #
#           0      #
#   *              #
#                 #
#		  #
#		  #
#		  #
#		  #
#		  #
# # # # # # # # # #
``````