Miniproject Merged
Miniproject Merged
Mini-Project Reported On
“SNAKE GAME”
Project carried out
By
Pragyesh Kumar Seth 2158071
Anand Raj Bind 2158017
Hritik Sonkar 2158052
Alok Kumar Maurya 2158011
Date: / /2024
Head of Department
(Prof. Noopur Goel)
Internal Examiner External Examiner
i
ACKNOWLEDGEMENT
The Viva-voce examination of PRAGYESH KUMAR SETH (BCA) student has been
held on.............................
iii
PROJECT REPORT
ON
SNAKE GAME
TABLE OF CONTENTS
1
INTRODUCTION
2
concept of lives. Once you hit an obstacle, that’s it, game
over.
3
4
LITERATURE SURVEY
The history of
the Snake game goes back to the 1970's, the concept
originated in the 1976 arcade game Blockade, and its
simplicity has led to many implementations. However, it was
the1980'swhen the game took on the look that we will be
using. It was sold under numerous names and many
platforms but probably gained widespread recognition when
it was shipped as standard on Nokia mobile phones in the
late 1990's. The first published Nokia, for monochrome
phones. It was programmed in1997 by Taneli Armanto of
Nokia and introduced on the Nokia 6110.The game involves
controlling a single block or snakehead by turning only left or
right by ninety degrees until you manage to eat an block.
When you get the block, the Snake grows an extra block or
body segment. If, or rather when, he snake bumps into the
edge of the screen or accidentally eats himself the game is
over. The more blocks the snake eats the higher the score.
5
SYSTEM DESIGN
6
SYSTEM
AND
HARDWARE REQUIREMENTS
SYSTEM REQUIREMENTS
7
HARDWARE REQUIREMENTS
8
FUNCTIONAL REQUIREMENTS
9
NON-FUNCTIONAL REQUIREMENTS
I. Robustness:
Robustness is nothing but its ability that it can
tolerate the affects of system functional body. And it
can also be defined as its systems ability that it can
withstand to change without transforming its initial
stable configuration.
II. Reliability:
The system is trustworthy and it is consistently
good in performance. It can also be stated as the
system performs the function without any failure under
certain conditions and specified period of time.
10
III. Availability:
The system is available 24*7. Availability and
Reliability are directly proportional as reliability
increase availability also increases. The user can have
access to the system all the time.
IV. Reusability:
The system can be used any number of times by
the specific user. And the reusability is consistent,
adaptable and stable.
V. Effectiveness:
The algorithm is capable of producing desired
result or it has the ability to provide better results.
11
WORKING ALGORITHM
12
SAMPLE OUTPUT
13
CONCLUSION
14
FUTURE SCOPE
15
REFERENCES
• Class Lectures
• Project Supervisor
• https://youtu.be/2ZDnw6ifdSI?si=lsxmseaj8YT6UV
af
• https://www.scribd.com/document/631803562/sn
ake-game-project-report
16
SOURCE CODE
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bhaag Snake Bhaag</title>
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<div class="body">
<div id="scoreBox">Score: 0</div>
<div id="hiscoreBox">Hi score: 0</div>
<div id="board"></div>
</div>
17
</body>
<script src="./js/script.js"></script>
</html>
18
style.css:
/**
* FILEPATH: /D:/Snake-game-project-miniproject/css/style.css
*
* This CSS file contains the styling for a snake game project.
* It imports a Google Fonts stylesheet and sets some global styles.
* The body element is styled to have a background image and be centered on
the page.
* The scoreBox and hiscoreBox elements are positioned and styled for
displaying scores.
* The board element represents the game board and is styled with a gradient
background and grid layout.
* The head, snake, and food elements are styled for the game components.
*/
@import
url('https://fonts.googleapis.com/css2?family=New+Tegomin&display=swap');
*{
padding: 0;
margin: 0;
}
.body {
background: url("../img/bg.jpg");
min-height: 100vh;
background-size: 100vw 100vh;
background-repeat: no-repeat;
19
display: flex;
justify-content: center;
align-items: center;
}
#scoreBox {
position: absolute;
top: 9px;
right: 200px;
font-size: 39px;
font-weight: bold;
font-family: 'New Tegomin', serif;
}
#hiscoreBox{
position: absolute;
top: 59px;
right: 140px;
font-size: 39px;
font-weight: bold;
font-family: 'New Tegomin', serif;
}
#board {
background: linear-gradient(#2a9c2a, #d8d840);
width: 90vmin;
20
height: 92vmin;
border: 2px solid #ff0000;
display: grid;
grid-template-rows: repeat(18, 1fr);
grid-template-columns: repeat(18, 1fr);
}
.head {
background: linear-gradient(#c43b3b, #c7c748);
border: 2px solid purple;
transform: scale(1.02);
border-radius: 19px;
}
.snake {
background-color: #800080;
border: .25vmin solid white;
border-radius: 120px;
}
.food {
background: linear-gradient(#ff0000, #800080);
border: .25vmin solid black;
border-radius: 8px;
21
script.js:
/**
* FILEPATH: /D:/Snake-game-project-miniproject/js/script.js
*
* This JavaScript contains all the logic for snake game project.
* It also contains the music that is used in game.
*/
// Game Functions
22
function main(ctime) {
window.requestAnimationFrame(main);
if ((ctime - lastPaintTime) / 1000 < 1 / speed) {
return;
}
lastPaintTime = ctime;
gameEngine();
}
function isCollide(snake) {
// If you bump into yourself
for (let i = 1; i < snakeArr.length; i++) {
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) {
return true;
}
}
// If you bump into the wall
if (snake[0].x >= 19 || snake[0].x <= 0 || snake[0].y >= 19 || snake[0].y <= 0) {
return true;
}
}
/**
* Executes the game engine logic.
*/
function gameEngine() {
// Part 1: Updating the snake array
if (isCollide(snakeArr)) {
23
gameOverSound.play();
musicSound.pause();
inputDir = { x: 0, y: 0 }
alert("Game Over. Press any key to play again!");
snakeArr = [{ x: 13, y: 15 }];
musicSound.play();
score = 0;
}
// If you have eaten the food, increment the score and regenerate the food
if (snakeArr[0].y === food.y && snakeArr[0].x === food.x) {
foodSound.play();
score += 1;
if (score > hiscoreval) {
hiscoreval = score;
localStorage.setItem("hiscore", JSON.stringify(hiscoreval));
hiscoreBox.innerHTML = "HiScore: " + hiscoreval;
}
scoreBox.innerHTML = "Score: " + score;
snakeArr.unshift({ x: snakeArr[0].x + inputDir.x, y: snakeArr[0].y + inputDir.y
});
let a = 2;
let b = 16;
food = { x: 2 + Math.round(a + (b - a) * Math.random()), y: 2 +
Math.round(a + (b - a) * Math.random()) }
}
24
// Moving the snake
for (let i = snakeArr.length - 2; i >= 0; i--) {
snakeArr[i + 1] = { ...snakeArr[i] };
}
snakeArr[0].x += inputDir.x;
snakeArr[0].y += inputDir.y;
board.appendChild(snakeElement);
})
// Displaying the food
25
foodElement = document.createElement('div');
foodElement.style.gridRowStart = food.y;
foodElement.style.gridColumnStart = food.x;
foodElement.classList.add('food');
board.appendChild(foodElement);
26
case "ArrowUp":
console.log("ArrowUp");
inputDir.x = 0;
inputDir.y = -1;
break;
case "ArrowDown":
console.log("ArrowDown");
inputDir.x = 0;
inputDir.y = 1;
break;
case "ArrowLeft":
console.log("ArrowLeft");
inputDir.x = -1;
inputDir.y = 0;
break;
case "ArrowRight":
console.log("ArrowRight");
inputDir.x = 1;
inputDir.y = 0;
break;
default:
break;
27
}
});
28
OUTPUT
29