Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

DELHI TECHNOLOGICAL UNIVERSITY

(Formerly Delhi College of Engineering)


Shahbad Daulatpur, Bawana Road, Delhi 110042

COE
PROJECT REPORT
ON
Snake Game
SUBMITTED BY
Tanisha Singhal (2k20/A5/14)
Akkshita Swain (2k20/A5/23)
SUBMITTED TO: Ms Priya Singh
CANDIDATE DECLARATION

We , (Shakshi and Sachi Giri ) students of


B.Tech(Computer Engineering ) hereby declare that
the project Dissertation titled “Snake Game “ which
is submitted by us to the Department of Computer
Fundamental(CO) Delhi Technological University ,
Delhi in partial fulfillment of the need for the award
of the degree of Bachelor of Technology, is original
and not copied from any source without proper
citation. This work has not previously formed the
idea for the award of any Degree, Diploma
Associations, Fellowship or similar title or
recognition.

PLACE: DELHI
DATE : 18 JULY 2021
CERTIFICATE

We at this moment certify that the project titled


"Snake Game”, which is submitted by Tanisha
Singhal and Akkshita Swain (2K20/A5/14,
2K20/A5/23) student of Delhi Technological
University, Delhi, incomplete fulfilment of the
requirement for the award of Term Evaluation of the
second semester of Bachelor Of Technology, maybe
a record of the project work allotted by the scholars
under my supervision.

Place: Delhi Date : 18July2021

Ms Priya Singh
(Subject Teacher)
Objective :
The purpose of our project is to write a snake game
using the C programming language. Our goal is to
move the snake and eat as many "fruits" blocks as
possible. There is only one fruit at any given time.
When the fruit is eaten, the snake grows in length. If
we hit the snake itself, the game is over.

Language: C

Libraries:

1. stdio.h
2. stdlib.h
3. conio.h
ACKNOWLEDGEMENT

In performing our project-1, we had to require the


assistance and guideline of some respected persons,
who deserve our most incredible gratitude.
Completing of this assignment gives us so much
pleasure. We might prefer to show our gratitude to
Ms Priya Singh for giving us a decent guideline for
report throughout numerous consultations. We might
also wish to extend our deepest gratitude to all
people who have directly and indirectly guided us in
penning this assignment.
Many folks, especially Ms Priya Singh, our
classmates and team members, have made valuable
comment suggestions on this proposal, giving us a
concept to boost our assignment. We thank all the
people for his or her help directly and indirectly to
complete our assignment.Additionally, we would
prefer to thank the Department of Computer Science,
Delhi Technological University, for giving us the
chance to figure on this subject.
CONTENTS

• Title Page
• Candidate Declaration
• Certificate
• Proposal
• Acknowledgement
• Contents
• Header file Used
• Function Used
• Flowchart
• Working Code
• Sample Output
HEADER FILES USED

• #include<stdio.h>
stdio. h is a header file for input and output. This is
useful for getting the input from the
user(Keyboard) and output result text to the
monitor(Screen).
• #include<conio.h>
This header file is used to use kbhit() function in
our program. kbhit() function return true only when
user press a key. getch() is also a library function of
conio.h
• #include<stdlib.h>
This header file is used to use the random() function
in our program. random() function generates a
random number system() is a library function of
stdlib.h. This function is used to run system or
command prompt commands, and here cls is a
command to clear the output screen.
FUNCTION USED

• Setup() function
setup() function is used to set the intial values of all
the variables

• draw() function
draw() function is used to set the boundary and the
position of snake and fruits.

• input() function
input() function that is used to take input from user.

• logic() function
logic () function explains the working of the game.
FLOWCHART
SNAKE GAME CODE

/* ******************SNAKE GAME ***********************


BY - TANISHA SINGHAL(2K20/A5/14)
AKKSHITA SWAIN (2K20/A5/23)
*/

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

int width=20 ;
int height=20; // global variable as we are using in different functions for the
same variables value
int x,y ;
int fruitX,fruitY;
int score;
int gameover;
int flag ;
int tailX[100];
int tailY[100];
int countTail=0;

void set_up() // to set the initial value of every variable


{

gameover=0;
x=width/2; // dimensions of the box
y=height/2;

label1:

fruitX=rand()%20 ; // rand function is used for getting fruit at random posit


ions wihtin those boundaries
if(fruitX==0)
goto label1 ; // goto function is used to jump to a particular function
(label)

label2:
fruitY=rand()%20 ;
if(fruitY==0)
goto label2 ;

score=0;

void draw() // fucntion to make boundaries


{

int i,j,k;

system("cls"); // cls is used so that to clear the previous code(screen) and


we get a clear boundaries

for(i=0;i<width;i++)
{

for(j=0;j<height;j++)
{
if(i==0||i==height-1||j==0||j==width-1)
{
printf("#");
}
else
{
if(i==x && j==y)
{
printf("o");
}
else if(i==fruitX && j==fruitY)
{
printf("F");
}
else

{ int ch=0 ;

for (k=0;k<countTail;k++)
{
if(i==tailX[k] && j==tailY[k])
{
printf("o");
ch=1 ;
}
}
if(ch==0)
printf(" ");
}

}
printf("\n");
}

printf("score=%d",score) ;

void input(){

if(kbhit())
{
switch(getch())
{
case 'a' :
flag=1;
break;
case 's':
flag=2;
break ;
case 'w':
flag =3;
break;
case 'z':
flag=4;
break;

case 'L':
gameover=1 ;

}
}
}
void Make_logic()

{
int i ;
int prevX=tailX[0];
int prevY=tailY[0];

int prev2X ;
int prev2Y ;

tailX[0]=x ;
tailY[0]=y ;

for(i=1;i<countTail;i++)
{

prev2X=tailX[i];
prev2Y=tailY[i];
tailX[i]=prevX;
tailY[i]=prevY;
prevX=prev2X;
prevY=prev2Y;

switch(flag)
{
case 1:
y-- ;
break;
case 2:
y++ ;
break;
case 3:
x-- ;
break ;
case 4:
x++ ;
break ;

default :
break ;
}
if (x<0||x>width||y<0||y>height)
{
gameover=1;
for(i=0;i<countTail;i++)
{
if(x==tailX[i] && y==tailY[i])
gameover=1;
}
}

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


{
label3:

fruitX=rand()%20 ;
if(fruitX==0)
goto label3 ;

label4:

fruitY=rand()%20 ;
if(fruitY==0)
goto label4 ;

score+=10 ;

countTail++ ;

int main()
{
char c ;
label5 :

set_up();

while(!gameover)

{
draw();
input();
Make_logic();

for(int m=0;m<1000;m++)
{
for(int n=0;n<10000;n++)
{

}
}

for(int m=0;m<1000;m++)
{
for(int n=0;n<10000;n++)
{

}
}
for(int m=0;m<1000;m++)
{
for(int n=0;n<10000;n++)
{

}
}
for(int m=0;m<1000;m++)
{
for(int n=0;n<10000;n++)
{

}
}

printf("\n press Y to continue again:");


scanf("%c" , &c);

if(c=='y'||c=='Y')
goto label5 ;

return 0 ;
}
FINAL OUTPUT SAMPLE
THANK YOU

You might also like