0% found this document useful (0 votes)
65 views28 pages

Document Snake Game

This document is a project report for a Snake Game developed by a team of students at Nazeer Hussain University. It includes sections on the introduction to the game, how to make and run the project, how to play the game, the proposed system requirements, requirement analysis, and system design. The project was developed in C++ and involves controlling a snake avatar on a bordered plane to collect dots while avoiding obstacles and the snake's own body.

Uploaded by

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

Document Snake Game

This document is a project report for a Snake Game developed by a team of students at Nazeer Hussain University. It includes sections on the introduction to the game, how to make and run the project, how to play the game, the proposed system requirements, requirement analysis, and system design. The project was developed in C++ and involves controlling a snake avatar on a bordered plane to collect dots while avoiding obstacles and the snake's own body.

Uploaded by

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

SNAKE GAME

A project report submitted


To:

NAZEER HUSSAIN UNIVERSITY


For partial fulfillment of the requirement for
the Award of the Degree

of

COMPUTER SCIENCE AND ENGINEERING


Submitted by - TEAM MEMBERS:
AFRAH
ALISHBA
IQRA
MANAHIL
SEHAR
Under the guidance of:
MISS SUBREENA AYUB KHAN
Lecturer Dept. of Computer Science (BSCS)

Date of the project submission: 26th June, 2023

SUBJECT: Programming Fundamentals


DECLARATION

We hereby declare that the details enclosed in the project proposal entitled “SNAKE
GAME” carried out by us in partial fulfilment of the requirements for the award for
degree in computer science & engineering are true and correct to the best of our
knowledge and belief is a record of an original work done by us under the guidance of
MISS SUBREENA AYUB KHAN, faculty of computer science and engineering
department and it is also declare that this report has been prepared for academic
purpose alone and has not been/will not be submitted elsewhere for any other purpose.

Place: Nazeer Hussain University

Branch-Computer Science & Engineering

Semester-1st

Team Names-AFRAH
ALISHBA
IQRA
MANAHIL
SEHAR
ACKNOWLEDGEMENT

We are thankful to the Department because of whom, we have gained confidence in


“Snake Game” Development and it has also enhanced our professional skills so as to
become competent in this field.

We take this opportunity to present my votes of thanks to all those guide


post who really acted as lightening pillars to enlighten our way throughout this project
that has led to successful and satisfactory completion of this study.

We are really grateful to our Ma’am Subreena for providing us with an


opportunity to undertake this project in this university and providing us with all the
facilities.

We are highly thankful for your active support, valuable time and advice,
whole-hearted guidance, sincere cooperation and painstaking involvement during the
study and in completing the assignment of preparing the said project within the time
stipulated.

Lastly, we are thankful to all those, particularly the various friends, who
have been instrumental in creating proper, healthy and conductive environment and
including new and fresh innovative ideas for us during the project, their help, it would
have been extremely difficult for us to prepare the project in a time bound framework.

We started our this project with excitement of embarking on a new and


long journey. Advancing with leaps and bounds every day, and exploring new
avenues that the field of Snake Game Development. The journey has not been a
smooth one though. Initially, it was very hard to cope up, learn and get acquainted
with this project “Snake Game”, it would not have been possible to sustain, strive and
get enriched in this field.
CONTENTS:

DECLARATION
ACKNOWLEDGEMENT
CONTENTS:
CHAPTER: 1 INTRODUCTION
CHAPTER: 2 MAKING OF THE GAME
CHAPTER: 3 HOW TO RUN THE PROJECT?
CHAPTER: 4 PLAYING THE GAME
CHAPTER: 5 PROPOSED SYSTEM
i) Description
ii) System Requirements
CHAPTER: 6 REQUIREMENT ANALYSIS
CHAPTER: 7 SYSTEM DESIGN
CHAPTER: 8 SOURCE CODE
CHAPTER: 9 EXPLANATION
CHAPTER: 10 BASIC SNAKE GAME CODE FEATURES
CHAPTER: 11 TESTING
CHAPTER: 12 FINAL OUTPUT
CHAPTER: 1

INTRODUCTION

The following is an example game written in C++ based on the game called
'snake game’ which has been around since the earliest days of home
computing (1 can remember writing a version of it for my ZX81), and has re-
emerged in recent years on mobile phones.

It isn't the world's greatest game, but it does give you an idea of what you
can achieve with a relatively simple C++ program, and perhaps the basis by
which to extend the principles and create more interesting games of your own.

What do you mean by the Snake game?


The Simple Snake Game is a single-player game and is one of the most
famous games available on all types of device and works on every operating
system. Here, the player has to control the black rectangular-shaped box
(termed as a snake) on a bordered plane. The game is a very popular and
interesting game. This whole system is in the C++ language. You just have to
press any ASWD keys to start moving the snake, then get eating those dots
(food) to score and grow your snake bigger.
CHAPTER: 2

MAKING OF THE GAME

This Snake game is simply designed in ‘C++’ language. The PC controls of


this game are also simple. You just have to use the ASWD keys of the
keyboard for moving the snake in different directions. The game is on until you
save the snake from the boundary line and snake itself. The game score is
shown at the top.  Also, this project is easy to operate and understood by the
users.
CHAPTER: 3

HOW TO RUN THE PROJECT?

In order to run the project, you must have installed Dev C++ or Code


Blocks on your PC. (we recommend these IDE). The Snake Game in C++ with
source code is use for educational purposes only, as the language of the
game is developed in C++. 
CHAPTER: 4

PLAYING OF THE GAME

The aim of the game is to collect the dots (food) and avoid the obstacles
(crosses, borders, and snake itself). As you collect foods, the snake gets
longer so, increasing your likelihood of crashing into yourself. When you have
collected enough foods, you progress onto the next level, where your snake
gets longer, and the amount of food to collect to progress through the level
gets larger. You get scored according to the length of the snake and the
number of ‘x’ obstacles on the screen. The speed increases every 5 levels.
You get a bonus when you complete the level of 1000, increasing by 1000
each level (e.g. complete level 5, you get a 5000 bonus). There is no concept
of lives. Once you hit an obstacle, that’s it, game over. Make sure you do not
have caps lock on, otherwise the keys will fail to respond. To move the snake,
use ‘w’ for up, ‘s’ for down, ‘a’ for left and ‘d’ for right. Again, there are no
constants you can change if you want to alter these settings. Press zero (0)
key to stop or ‘x’ to end the game.
CHAPTER: 5

PROPOSED SYSTEM
The following documentation is a project the “Name of the term paper
allotted”.it is a detailed summary of all the drawbacks of the old system and
how the new proposed system overcomes these shortcomings. The new
system takes into account the various factors while designing a new system. It
keeps into the account the Economical bandwidth available for the new
system, the foremost thing that is taken care of is the Need & Requirement of
the User.
DESCRIPTION:
Before developing software, we keep following things in mind that we can
develop powerful & quality software.
PROBLEM STATEMENT:

● Problem statement was to design a module.

● Which is user friendly.

● Which will restrict the user from accessing other user’s data.

● Which will help user in viewing his/her data and privileges.

● Which will help the administrator to handle all the changes.

FUNCTIONS TO BE PROVIDED:
The system will be user friendly and completely menu driven so that no
problem in using all options.

● The system will be efficient and fast in response.


● The system will be customized according to needs.

● It is a game which can play by anyone.

● It is easy to use.

SYSTEM REQUIREMENTS:
Operating systems: MS Windows XP or Windows Vista
Language: C++ language
RAM: 512 MB

CHAPTER: 6

REQUIREMENT ANALYSIS

This process is adopted when the management of the system development,


Personnel decide that the particular system needs improvement. The system
development life cycle is the set of activities, carried out by the analyst,
designers and users to develop and implement a system. The systems that
are present in the nature follow common life cycle pattern. For Example,
consider the raining system. Initially the rain falls into the river, river flows into
the sea, the sea water evaporates to form vapors, the vapors form clouds
which again bring rain. Similarly consider a man-made system initially a
system is analyzed, designed and made operational by the efforts of system
analysis. After successful operation or a number of users, the system
becomes less and less effective by change in the environment. So these
changes have to be incorporated in to the system by minor modifications. So
the general activities from the life cycle of the system are given below:

● Select ion and identification of the system to be studied.

● Preliminary study.

● Defining the system.


● Design & development of the system.

● Implementation of the system.

CHAPTER: 7

SYSTEM DESIGN

Then we began with the design phase of the system. System design is a
solution, a “HOW TO” approach to the creation of a new system. It translates
system requirements into ways by which they can be made operational. It is a
translational from a user oriented document to a document oriented
programmers. For that, it provides the understanding and procedural details
necessary for the implementation. Here we use flowchart to supplement the
working of the new system. The system thus made should be reliable, durable
and above all should have least possible maintenance costs. It should
overcome all the drawbacks of the Old existing system and most important of
all meet the user’s requirements.
CHAPTER: 8

SOURCE CODE
Let's take the source code of the snake game in C++.

#include<iostream>
#include<conio.h>
#include<windows.h>

using namespace std;


// Enum for the snake's direction
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dira; // Current direction of the snake

bool gameOver; // Flag to indicate if the game is over

// Function prototypes
void setup();
void Draw();
void Input();
void Logic();
void Logic2();

const int width = 20; // Width of the game board


const int height = 20; // Height of the game board

int x, y, fruitX, fruitY, score; // Variables for snake position, fruit position, and
score
int tailx[100], taily[100]; // Arrays to store the tail coordinates
int ntail; // Length of the snake's tail

int main()
{
char ch;

// Game mode selection


cout << "\n\t---------------------------------------\n";
cout << "\t\t:Snake game in C++:\n";
cout << "\t---------------------------------------\n\n";
cout << "Please Enter Game Mode you want to play:\n\n";
cout << "Play Hard Mode by pressing: H\n\n";
cout << "Play Easy Mode by pressing: S\n\n";
cin >> ch;

switch (ch)
{
case 'S':
setup();
while (!gameOver)
{
Draw();
Input();
Logic();
_sleep(30);
}
break;
case 'H':
setup();
while (!gameOver)
{
Draw();
Input();
Logic2();
_sleep(30);
}
break;
default:
cout << "Invalid!! Please Select the Given Options.";
break;
}

getch();
return 0;
}

// Function to initialize the game


void setup()
{
gameOver = false;
dira = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
}

// Function to draw the game board and snake


void Draw()
{
system("cls");
cout << "\n\t\t :SNAKE GAME:\n";
cout << "\t\t";
for (int i = 0; i < width - 8; i++)
{
cout << "||";
}
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
{
cout << "\t\t||";
}
if (i == y && j == x)
cout << "O";
else if (i == fruitY && j == fruitX)
cout << "*";
else
{
bool print = false;
for (int k = 0; k < ntail; k++)
{
if (tailx[k] == j && taily[k] == i)
{
cout << "o";
print = true;
}
}
if (!print)
{
cout << " ";
}
}
if (j == width - 1)
cout << "||";
}
cout << endl;
}
cout << "\t\t";
for (int i = 0; i < width - 8; i++)
{
cout << "||";
}
cout << endl;

cout << "\t\t\tScore: " << score << endl;


}
// Function to handle user input
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 'a':
case 75: // Left arrow key
dira = LEFT;
break;
case 'd':
case 77: // Right arrow key
dira = RIGHT;
break;
case 'w':
case 72: // Up arrow key
dira = UP;
break;
case 's':
case 80: // Down arrow key
dira = DOWN;
break;
case 'x':
gameOver = true;
break;
}
}
}
// Logic for the simple game mode
void Logic()
{
int prevX = tailx[0];
int prevY = taily[0];
int prev2X, prev2Y;

tailx[0] = x;
taily[0] = y;

// Update the tail coordinates


for (int i = 1; i < ntail; i++)
{
prev2X = tailx[i];
prev2Y = taily[i];
tailx[i] = prevX;
taily[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}

// Update the snake's head position based on the direction


switch (dira)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}

// Wrap the snake around the screen


if (x >= width)
x = 0;
else if (x < 0)
x = width - 1;
if (y >= height)
y = 0;
else if (y < 0)
y = height - 1;

// Check if the snake collides with itself


for (int i = 0; i < ntail; i++)
{
if (tailx[i] == x && taily[i] == y)
{
gameOver = true;
}
}

// Check if the snake eats the fruit


if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
ntail++;
}
}

// Logic for the hard game mode


void Logic2()
{
int prevX = tailx[0];
int prevY = taily[0];
int prev2X, prev2Y;

tailx[0] = x;
taily[0] = y;

// Update the tail coordinates


for (int i = 1; i < ntail; i++)
{
prev2X = tailx[i];
prev2Y = taily[i];
tailx[i] = prevX;
taily[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}

// Update the snake's head position based on the direction


switch (dira)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}

// Check if the snake hits the wall


if (x >= width || x < 0 || y >= height || y < 0)
{
gameOver = true;
}

// Wrap the snake around the screen


if (x >= width)
x = 0;
else if (x < 0)
x = width - 1;
if (y >= height)
y = 0;
else if (y < 0)
y = height - 1;

// Check if the snake collides with itself


for (int i = 0; i < ntail; i++)
{
if (tailx[i] == x && taily[i] == y)
{
gameOver = true;
}
}

// Check if the snake eats the fruit


if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
ntail++;
}
}

CHAPTER: 9

EXPLANATION
About the Application:

In the above code example, we have implemented a basic Snake Game.


The Basic Snake Game was developed in a simple windowed console
application, that display the gameplay as a console log gameplay. 
The basic functionalities of this game are given below.
o The Snake is created with the help of a draw() function.
o Different modes of the game is also created that of which mode type of
game player wants to play (Easy or Hard).
o The fruit of the Snake is generated by the rand() function of computer
graphics.
o  The players can play the game via keyboard bindings (Press “a” to
Move Left, “w” to Move Above, “d” to Move Right, “s” to Move Below). 
o When the Snake eats a fruit, the score will increase by 10 points.
o Each time you wait food you will gain a score and it will increase the
length of your snake. 
o  The game will be over if your snake hits its own body. 

In this game, we can create basic controls:

o H control is used to select the hard level mode of the game.


o S control is used to select the easy or simple level mode of the
game.
o x control is used to exit from the game. We can click
an x character from the keyword to exit from or stop the game.

This application is for educational purpose only, you are free to modify it as


you fit your needs.

CHAPTER: 10

Basic Snake Game Code Features:

▪ Basic GUI
▪ The project contains basic GUI that display the actual gameplay
of the game.
▪ User-friendly Interface
▪ This project was design with a simple user-friendly interface.

▪ Basic Controls
▪ This project uses a basic control function so that player can easily
be playable.

CHAPTER: 11

TESTING

Testing is the major control measure used during software development. It is


basic function is to detect errors in the software. During requirement analysis
and design, the output is a document that is usually textual and no
executable. After the coding phase, computer programs are available that can
be executed for testing purpose. This implies that testing not only, has to
uncover errors introduced during coding, but also errors introduced during
previous phase. Thus the goal of testing is to uncover the requirements,
design and coding errors in the programs. So after testing the outputs of our
project are as follows:

CHAPTER: 12

FINAL OUTPUT
Following is the output of the above source code:

You might also like