0% found this document useful (0 votes)
19 views

Project Report On Snake in C by Js

Uploaded by

CSC WALA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Project Report On Snake in C by Js

Uploaded by

CSC WALA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

PROJECT REPORT

On
Snake Game On C
Submitted to
DEPARTMENT OF COMPUTER SCIENCE

Session: 2022-2023
Jasvinder
BCA 1st Year (2nd SEM)
Roll no: 1221803011014
UNDER THE SUPERVSION OF: Ms. Nisha Choudhary
AGGARWAL COLLEGE BALLABGARH
(Affiliated to MDU University, Rohtak)
ACKNOWLEDGEMENT

Practical works is incomplete without the guidance, assistance, inspiration And corporation
form various persons. This study also bears the imprints of many persons. I take this
opportunity to express my deep gratitude to all those who helped int the perusal of this
study and make it possible for this Report to be in our hand today.

Firstly, I want to express my gratitude to DR.SANJEEV GUPTA (INCHARGE AGGARWAL


COLLEGE BALLABGARH WING-3) who provided me with an oppurnity to do practical report.

I am grateful to my worthy supervisors MS. NISHA CHOUDHARY their guidance had


Privileged and encourage me throughout this study. They look pains to going through every
line and also give me creative ideas for further improvement.

I am obliged to DR. SACHIN GARG (H.O.D (COMPUTER DEPT.)) who helped me in My


training tenure and also guide me from time to time about various aspects Of my projects
by their valuable assistance.

Finally, I wish to express my special thanks to my family and friends to their Constant
support and encouragement during the creation of this project.

JASVINDER
BCA 1st YEAR
PREFACE

Today's modern age the importance has been acknowledgement by all specially by the
optimum utilization of time and money is the urging need of the day and there is rapid
computerization of the organization and professional bodies, as a result the knowledge of
computer is absolutely necessary .

Therefore for developing our basic technique In computer knowledge wear making a mini
projects, sometimes it is very hard to observe time in analogue watches so here we are
making project using c programming language name.

SNAKE GAME ON C
INDEX

TABLE OF CONTENTS[T.O.C]

SER NO. DESCRIPTION PAGE NUMBER.

01 Acknowledgement 02

02 Preface 03

03 Introduction 05

04 Source code 07

05 Output 20

06 Bibliography 23
INTROCUCTION

C Language Introduction

C is a procedural programming language initially developed by Dennis Ritchie


in the year 1972 at Bell Laboratories of AT&T Labs. It was mainly developed as
a system programming language to write the UNIX operating system.

The main Features of the C language include:

 General Purpose and Portable


 Low-level Memory Access
 Fast Speed
 Clean Syntax
 These features make the C language suitable for system programming
like an operating system or compiler development.

Why Should We Learn C?

Many later languages have borrowed syntax/features directly or indirectly


from the C language. Like syntax of Java, PHP, JavaScript, and many other
languages are mainly based on the C language. C++ is nearly a superset of C
language (Only a few programs may compile in C, but not in C++).

So, if a person learns C programming first, it will help him to learn any modern
programming language as well. As learning C help to understand a lot of the
underlying architecture of the operating system. Like pointers, working with
memory locations, etc.
PROJECT
CODE
Snake Game in C
In this article, the task is to implement a basic Snake Game. Below given
some functionalities of this game:
 The snake is represented with a 0(zero) symbol.
 The fruit is represented with an *(asterisk) symbol.
 The snake can move in any direction according to the user with the help
of the keyboard (W, A, S, D keys).
 When the snake eats a fruit the score will increase by 10 points.
 The fruit will generate automatically within the boundaries.
 Whenever the snake will touch the boundary the game is over.
Steps to create this game:
 There will be four user-defined functions.
 Build a boundary within which the game will be played.
 The fruits are generated randomly.
 Then increase the score whenever the snake eats a fruit.
The user-defined functions created in this program are given below:
 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.
 Logic(): 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.
Header files and variables:
 The header files and variables used in this program are:

 Here include the <unistd.h> header file for the sleep() function.
Draw(): This function is responsible to build the boundary within which the game
will be played.
Below is the C program to build the outline boundary using draw():

 C

// C program to build the outline

// boundary using draw()

#include <stdio.h>

#include <stdlib.h>

int i, j, height = 30;

int width = 30, gameover, score;

// Function to draw a boundary

void draw()

{
// 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

int main()

// Function Call
draw();

return 0;

Output:
##############################

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #
# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

# #

##############################

setup():nThisbfunction is used to write the code to generate the fruit within the
boundary using rand() function.
 Using rand()%20 because the size of the boundary is length = 20 and width =
20 so the fruit will generate within the boundary.
Input(): In this function, the programmer writes the code to take the input from the
keyboard (W, A, S, D, X keys).
logic(): Here, write all the logic for this program like for the movement of the snake,
for increasing the score, when the snake will touch the boundary the game will be
over, to exit the game and the random generation of the fruit once the snake will
eat the fruit.
sleep(): This function in C is a function that delays the program execution for the
given number of seconds. In this code sleep() is used to slow down the movement
of the snake so it will be easy for the user to play.
main(): From the main() function the execution of the program starts. It calls all the
functions.
Below is the C program to build the complete snake game.

INPUT

// C program to build the complete


// snake game
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int i, j, height = 20, width = 20;
int gameover, score;
int x, y, fruitx, fruity, flag;
// Function to generate the fruit
// within the boundary
void setup()
{
gameover = 0;
// Stores height and width
x = height / 2;
y = width / 2;
label1:
fruitx = rand() % 20;
if (fruitx == 0)
goto label1;
label2:
fruity = rand() % 20;
if (fruity == 0)
goto label2;
score = 0;
}
// Function to draw the boundaries
void draw()
{
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");
else if (i == fruitx
&& j == fruity)
printf("*");
else
printf(" ");
}
}
printf("\n");
}
// Print the score after the
// game ends
printf("score = %d", score);
printf("\n");
printf("press X to quit the game");
}
// Function to take the input
void input()
{
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;
}
}
}
// Function for the logic behind
// each movement
void logic()
{
sleep(0.01);
switch (flag) {
case 1:
y--;
break;
case 2:
x++;
break;
case 3:
y++;
break;
case 4:
x--;
break;
default:
break;
}
// If the game is over
if (x < 0 || x > height
|| y < 0 || y > width)
gameover = 1;

// If snake reaches the fruit


// then update the score
if (x == fruitx && y == fruity) {
label3:
fruitx = rand() % 20;
if (fruitx == 0)
goto label3;

// After eating the above fruit


// generate new fruit
label4:
fruity = rand() % 20;
if (fruity == 0)
goto label4;
score += 10;
}
}
// Driver Code
void main()
{
int m, n;
// Generate boundary
setup();
// Until the game is over
while (!gameover) {

// Function Call
draw();
input();
logic();
}
}
OUTPUT
CONCLUSION

The objective of the game is to make the snake eat the said digit, so that it is
added to the score. When a digit is eaten, the size of the snake increases by
the number of characters equal to the value of the digit. The most important
life lesson the classic snake game teaches its players is control in their day-to-
day activities. In addition to this, players also learn about the duality of life and
how important it is to focus on a set goal.
BIBLIOGRAPHY

WWW.w3school.com

WWW.gIthub.com

WWW.geeks for geeks.com

You might also like