CSS_Format
CSS_Format
Micro Project On
“Snake Game”
Submitted by
60. Sujal Subhash Patade
Under Guidance of
Mrs. J. C. Joshi
Sinhgad Institutes
Sinhgad Technical Education Society’s
SOU. VENUTAI CHAVAN POLYTECHNIC, PUNE - 411041
ACADEMIC YEAR 2024 – 2025
Maharashtra State Board of technical
Education Certificate
This is to certify that, Mast. Sujal Subhash Patade with Roll
No. 60 of Fifth Semester of Diploma in Information
Technology of Institute Sou. Venutai Chavan Polytechnic
(Code:0040) has successfully completed the Micro-Project in
Client-Side Scripting Language (22519) for the academic year
2024-2025.
BRIEF INTRODUCTION:
The Snake game is a classic arcade game where players control a growing snake that
moves around the screen, consuming food while avoiding collisions with itself and the
boundaries. Using JavaScript, this implementation captures real-time user input for direction
control, manages the snake's body and food positions with arrays, and utilizes functions to
handle game mechanics. Event listeners facilitate interaction, while a continuous game loop
updates the state and rendering of the game, creating an engaging and interactive experience.
This project serves as a fun introduction to key programming concepts and enhances
JavaScript skills in a practical way.
RESOURCES REQUIRED:
GROUP MEMBERS:
S. No Course Outcomes
a) Create interactive web pages using program flow control structure.
b) Implements array and function in JavaScript.
c) Create event-based web form using JavaScript.
Create interactive web page using regular expressions for validation.
d)
index.HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>replit</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="gameBoard">
<div class="head">
<span class="eye">a</span>
<span class="eye">a</span>
</div>
</div>
<div class="score-show">
<h1>Score : <span id="score">0</span></h1>
</div>
<script src="script.js"></script>
</body>
</html>
style.css
body{
display:flex;
height:100vh;
justify-content: space-evenly;
background-color: white;
}
.gameBoard{
display:grid;
margin-top:40px;
grid-template-columns: repeat(12, 1fr);
grid-template-rows: repeat(7, 1fr);
height: 80vh;
width: 60vw;
background-color:green;
border:2px solid black;
}
.eye{
height: 20px;
width: 20px;
background-color: black;
border-radius: 50%;
}
.snake{
background-color:red;
border: 2px solid black;
border-radius: 12px;
}
.food{
background-color:yellow;
border-radius:50%;
}
.head{
background-color:blue;
border-radius: 12px;
}
script.js
//variable declaration
let gameBoard = document.querySelector(".gameBoard");
let lastPaintTime = 0;
let speed = 5;
let snakeArr = [
{ x: 5, y: 2 }
]
let snakeDir = { x: 0, y: 0 }
let food = { x: 2, y: 2 }
let score = 0;
//Requird Function
function main(ctime) {
window.requestAnimationFrame(main);
gameEngine();
}
function isCollied(head, snakeArr) {
for (let i = 1; i < snakeArr.length; i++) {
if ((head.x === snakeArr[i].x && head.y === snakeArr[i].y)
|| (head.x > 12 || head.x < 1 || head.y > 7 || head.y < 1)) {
console.log("Game Over");
return true;
}
}
return false;
}
function gameEngine() {
} else {
snakeBody.classList.add("snake");
}
gameBoard.appendChild(snakeBody);
})
//displaying the food
let snakeFood = document.createElement('div');
snakeFood.classList.add("food");
snakeFood.style.gridRowStart = food.y;
snakeFood.style.gridColumnStart = food.x;
gameBoard.appendChild(snakeFood);
}
window.requestAnimationFrame(main);
window.addEventListener('keydown', e => {
snakeDir = { x: 0, y: 0 }
switch (e.key) {
case "ArrowUp":
snakeDir.x = 0;
snakeDir.y = -1;
break;
case "ArrowDown":
snakeDir.x = 0;
snakeDir.y = 1;
break;
case "ArrowLeft":
snakeDir.x = -1;
snakeDir.y = 0;
break;
case "ArrowRight":
snakeDir.x = 1;
snakeDir.y = 0;
break;
case "w":
snakeDir.x = 0;
snakeDir.y = -1;
break;
case "s":
snakeDir.x = 0;
snakeDir.y = 1;
break;
case "a":
snakeDir.x = -1;
snakeDir.y = 0;
break;
case "d":
snakeDir.x = 1;
snakeDir.y = 0;
break;
}
})
OUTPUT
Reference
https://www.geeksforgeeks.org/
https://www.freecodecamp.org/
https://www.thatsoftwaredude.com/content/6193/coding-the-snake-game-in-
javascript
Conclusion
Creating a Snake game using HTML, CSS, and JavaScript is a great way to apply
key programming concepts like arrays, functions, events, and loops. Arrays manage
the snake's body and game grid, while functions encapsulate game logic for
movement and collision detection. Event handling allows for player interaction, and
loops enable real-time updates, creating a dynamic gaming experience. This project
not only reinforces your coding skills but also introduces you to fundamental game
development principles, laying the groundwork for more advanced projects in the
future.