Professional Documents
Culture Documents
Delhi Technological University (Formerly Delhi College of Engineering) Shahbad Daulatpur, Bawana Road, Delhi 110042
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
PLACE: DELHI
DATE : 18 JULY 2021
CERTIFICATE
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
• 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
#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;
gameover=0;
x=width/2; // dimensions of the box
y=height/2;
label1:
label2:
fruitY=rand()%20 ;
if(fruitY==0)
goto label2 ;
score=0;
int i,j,k;
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;
}
}
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++)
{
}
}
if(c=='y'||c=='Y')
goto label5 ;
return 0 ;
}
FINAL OUTPUT SAMPLE
THANK YOU