Mini Project
Mini Project
Submitted
In Partial Fulfillment of the Requirements
For the Degree of
Bachelor of Technology (B. Tech)
By
Priyanshu Sagar
2002150100033
Under the Supervision of
Neetu Mam
Of
All the work put into creating this game is dedicated to my family,
particularly my dear dad and mom, who stood by me in every
situation, providing unwavering support even during times of
financial need. Their encouragement has been my driving force, and
I am grateful for their constant presence. Lastly, I extend my sincere
thanks to all the faculties of SGIT for their assistance and support
throughout this endeavor.
Student:
Priyanshu Sagar
[Professor's Signature]
Index
Introduction
Objective
Implementation
Code Implementation
Dependencies
Workflow
Challenges faced
Results
Future Enhancements
Conclusion
References
INTRODUCTION
The game called "Snake" or "Snake Game" typically involve the player controlling a line
or snake, there is no official version of the game, so gameplay varies. The most
common version of the game involves the snake or line eating items which make it
longer, with the objective being to avoid running into a border or the snake itself for as
long as possible. The player loses when the snake either runs into a border or its own
body. Because of this, the game becomes more difficult as it goes on, due to the
growth of the snake.
OBJECTIVE
This Project in C language of Snake Game is a simple console application with very simple
graphics. In this project, you can play the popular “Snake Game” just like you played it
elsewhere.
• Hard Disk : 32 GB
• RAM : 128 MB
• Processor : Any Pentium Version
SNAPSHOT
STEPS TO CREATE THIS GAME
• Draw(): This function creates the boundary in which the game will be played.
• Setup(): This function will set the position of the fruit within the boundary.
• Input(): This function will take the input from the keyboard.
• MakeLogic(): This function will set the movement of the snake.
BUILT-IN FUNCTIONS USED
• kbhit(): This function in C is used to determine if a key has been pressed or not. To
use this function in a program include the header file conio.h. If a key has been
pressed, then it returns a non-zero value otherwise it returns zero.
• rand(): The rand() function is declared in stdlib.h. It returns a random integer value
every time it is called.
SOURCE CODE
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int width = 20, height = 20,gameOver;
int x,y,fruitX,fruitY,score,flag;
int tailX[100],tailY[100];
int CountTail=0;
void setup()
{
gameOver=0;
x=width/2;
y=height/2;
label1:
fruitX=rand()%20;
if(fruitX==0)
goto label1;
label2:
fruitY=rand()%20;
if(fruitY==0)
goto label2;
score=0;
}
void draw()
{
int i,j,k;
system("cls");
for(i=0;i<width;i++)
{
for(j=0;j<width;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': //aswz
flag=1;
break;
case 'd':
flag=2;
break;
case 'w':
flag=3;
break;
case 's':
flag=4;
break;
case 'x':
gameOver =1;
break;
}
}
}
void MakeLogic()
{
int i;
int prevX=tailX[0];
int prevY=tailY[0];
int prev2X,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()
{
int m,n;
char c;
label5:
setup();
while(!gameOver)
{
draw();
input();
MakeLogic();
for(m=0;m<1000;m++){
for(n=0;n<10000;n++){
}
}
for(m=0;m<10000;m++){
for(n=0;n<10000;n++){
}
}
}
printf("\nPress Y to continue again: ");
scanf("%c",&c);
if(c=='y' || c=='Y')
goto label5;
return 0;
}
REFERENCE
https://www.youtube.com/watch?v=L23H0lJ_eXM
https://www.youtube.com/watch?v=cBvw4-a6VlA
https://www.youtube.com/watch?v=PZZwnV8m0oo
https://www.youtube.com/watch?v=zcoEDCe4WZc
https://www.youtube.com/watch?v=ON5ODiAAQE&list=TLPQMjUwMTIwMjLY5Pxv8T0wK
A&index=2
https://www.geeksforgeeks.org/snake-game-in-c/
https://www.slideshare.net/ashutoshkumarkumar/project-on-snake-game-in-c-
language