A Snake Game Program Using C

Abstract: It is a game in which a snake is controlled by a person to eat foods(dots). As the foods are eaten the score is increased.

 

Introduction: In this game the person controls the snake with the arrow keys to eat foods. There are borders at the end of the screen at four sides.As the snake hits any of the borders,the snake dies.

 

Explaination :

 

The project(Snake Game) is written in C language.

First we added “#include<graphics.h>”header file to “CODEBLOCKS”  using this link and then we started coding.

 

DWORD scwidth = GetSystemMetrics(SM_CXSCREEN);

DWORD scheight = GetSystemMetrics(SM_CYSCREEN);

initwindow(scwidth, scheight, “The Snake Game”);

 

DWORD means “double word”.It is defined in windows.h header file.It contains integer value in the range 0 through 4,294,967,295.

 

GetSystemMetrics() and initwindowfunctions are written to create a window fit to the screen on which the game will be  played.

GetSystemMetrics(SM_CXSCREEN) and GetSystemMetrics(SM_CYSCREEN) functions return integer type value which indicate the pixels of the screen width and height respectively.

Initwindow()

This function takes 7 arguments,2 arguments are necessary other 5 arguments are optional.The first and second arguments create the window.We set first and second arguments as the returned values of GetSystemMetrics(SM_CXSCREEN) and GetSystemMetrics(SM_CYSCREEN) functions.We set the third argument as the name of the window.

 

 

x[0]= scwidth/2;

y[0]= scheight/2;

 

Writing x[0]= scwidth/2 and  y[0]= scheight/2 line of code we set the initial position of the snake as the  middle of the screen .This two lines make the head of the snake.

 

l=5;

Writing l=5,we set the initial length of  snake as 5 blocks.

 

Food coordinates :

 

rx=rand()%scwidth;

ry=rand()%scheight;

    if(rx<=(scwidth/2)) rx+=50;

    else rx-=50;

    if(ry<=(scheight/2)) ry+=50;

    else ry-=50;

 

 

 

rand()

It is a function that generates random number.

 

If the random numbers is greater than the screen,then after getting the modulus value of the function and screen width and screen height we set the food coordinates in the screen.For preventing the food generation on border we increased or decreased the value of the coordinates with 50.

 

Graphics Functions:

 

setcolor()

This function takes 1 argument as parameter.The colors are assigned in numbers.We can pass the color as the name of the color(in capital letters)or the assigned numbers.It sets the current drawing color.

 

settextstyle()

This function takes 3 arguments as parameters.First,second and third arguments are text font,text direction text size respectively.They are integer type.The arguments can be passed as assigned numbers or like this format :

 

settextstyle(BOLD_FONT, HORIZ_DIR, 5);

 

outtextxy()

This function takes 3 arguments.First 2 arguments is the coordinates of the text which  is to be appeared on the screen and the third argument is the text itself or the text can be stored into a string and then the string can be passed.

 

sprintf()

This function takes two arguments and it passes the second argument into the first argument.If we take this line to be clear,

sprintf(ins, “WELCOME TO SNAKE GAME”);

the second part is passed into first argument as string(ins named string should be declared first).

 

setfillstyle()

This function takes two arguments.First argument is pattern and second argument is color.There are 13 values which are allowed as pattern.We can even pass the value.

 

bar()

This function takes 4 integer type arguments.The first and second arguments are the starting point of X and Y axes.The third and fourth arguments are the ending point of X and Y axes.

 

getpixel()

This function returns the color of pixel at point (x,y).

 

 

GetAsyncKeyState()

This function is used for detecting key presses.

 

VK_RIGHT means right arrow key.

VK_LEFT means left arrow key.

VK_UP means up arrow key.

VK_DOWN means down arrow key.

VK_RETURN means enter key.

 

These are the parameters of this function.

 

 

 

 

Double Buffering (Graphics function):

 

setactivepage()

This function makes the page as active graphic page.An active graphic page is a page in which the graphics are drawn off screen.It is not visible page.

 

setvisualpage()

This function makes the page as visual graphics page.It is visible on screen.

 

These two functions prevent flickering.

 

 

How to play Snake Game :

 

One has to press the arrow keys to move the snake. If the position of snake head is overlapped with the food,then the food will be eaten and score and the length of the snake will be increased. But if the snake touches the border the snake dies.

 

 

Starting playing ,at first a window will pop up like this

 

https://drive.google.com/file/d/1eNmfd_i5rdnb1zl4JWNcmsu-9g2YVJFn/view?usp=sharing

 

After pressing any key ,the game will start.

It will look like this

https://drive.google.com/open?id=1TyW8fNNqKGWQVyYpzgojEw_Ubh-r_nEi

 

If the snake hits the border,then it will look like this,

 

https://drive.google.com/open?id=1qua-RDs6aeyMDIb8OpmpB7_gRbRO5QmJ

 

DOWNLOAD: Download our source code from https://github.com/PronabSarker/Snake_Game/blob/master/main.cpp

 

 

 

Authors:

Ruhit Arman (Student ID: 180222)

Md. FizulHaque (Student ID: 180230)

Pronab Sarker (Student ID: 180236)

Md. Tarif Hasan(Student ID :180238)

 

Computer Science and Engineering Discipline, Khulna University, Khulna.