A Snake Game Using Javascript
A Snake Game Using Javascript
Micro
projectOn
SinhgadInstitutes
Society’s
SOU.VENUTAI CHAVAN
POLYTECHNIC
PUNE-411041 ACADEMIC
YEAR2022-2023
Maharashtra State
Board of Technical
Education
Certificate
This is to certify that Mis. Pragati Salunke with Roll No. 27 of Semester V
ofDiplomainComputerTechnologyofInstituteSou.VenutaiChavanPolytechnic
(Code: 0040) has successfully completed the Micro-Project inClient Side
Scripting (22519) for the academic year 2021-2022 as prescribed
inthecurriculum.
Program Code:
CMCourseCode:CM/
5/I
SubjectTeacher HeadofDepartment
INDEX
2 Rationale 3
4 Literature Review 3
7 Source code 4
8 Output 8
9 Skills Developed 9
10 Applications of Micro-Project 10
11 Conclusion 10
CSS- 22519 BodyMassIndex(BMI)
Annexure –
I
Micro-Project Proposal
Annexure II
Micro-Project Report
1.0 Rationale:
JavaScript is a text-based programming language used both on the client-side and server- side
that allows you to make web pages interactive. Where as HTML and CSS are languages that
give structure and style to web pages, JavaScript gives web pages interactive elements that
engage a user. JavaScript allows you to create highly responsive interfaces that improve the
user experience and provide dynamic functionality, without having to wait for the server to
react and show another page.
You can bet that JavaScript is probably involved. It is the third layer of the layer cake of
standard web technologies, two of which (HTML and CSS) we have covered in much more
detail in other parts of the Learning Area. Snake Game is a simple project in HTML5, CSS,
and JavaScript. This is an amazing game made for the user conveniently.
food.js
import{onSnake,expandSnake}from'./snake.js'
import{randomGridPosition}from'./grid.js'
letfood=getRandomFoodPosition()
constEXPANSION_RATE=2
export function update()
{if(onSnake(food)) { expandSnake(EXPANSION_RATE)foo
d=getRandomFoodPosition()
}
}
exportfunctiondraw(gameBoard)
{ constfoodElement=document.createElement('div')foo
dElement.style.gridRowStart =
food.yfoodElement.style.gridColumnStart =
food.xfoodElement.classList.add('food')gameBoard.
appendChild(foodElement)
}
functiongetRandomFoodPosition(){
game.js
letnewFoodPosition
import{updateasupdateSnake,drawasdrawSnake,SNAKE_SPEED,
while(newFoodPosition==null||onSnake(newFoodPosition)){
getSnakeHead,snakeIntersection}from'./snake.js'
newFoodPosition=randomGridPosition()
import{updateasupdateFood,drawasdrawFood}from'./food.js'import{out sideGrid}from'./gr
}
returnnewFoodPosition
}
letlastRenderTime=0
letgameOver =false
constgameBoard=document.getElementById('game-board')
functionmain(currentTime){
if(gameOver){ if(confirm('Youlost.Pressoktorestart.')){window.
location ='/'
}
return
}
window.requestAnimationFrame(main)
constsecondsSinceLastRender=(currentTime- lastRenderTime)/1000if(secondsSinceLastRe
lastRenderTime=currentTimeup date()
draw()
}
window.requestAnimationFrame(main)
function update()
{updateSnake()upd ateFood()checkDea th()
}
functiondraw(){
gameBoard.innerHTML=''
drawSnake(gameBoard) drawFood(gameBoard)
}
functioncheckDeath(){
gameOver=outsideGrid(getSnakeHead())||snakeIntersection()
}
grid.js
constGRID_SIZE=21
Index.html
export function randomGridPosition()
{return{
x: Math.floor(Math.random() * GRID_SIZE)
<!DOCTYPEhtml>
+ 1,y:Math.floor(Math.random()*
<htmllang="en">
GRID_SIZE)+1
<head>
}
} <metacharset="UTF-8">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<metahttp-equiv="X-UA-Compatible"content="ie=edge">
export function outsideGrid(position)
<title>Snake</title>
{return(
<style
position.x < 1 || position.x > GRID_SIZE
>b
||position.y<1|| position.y> GRID_SIZE
) ody{
height:
100vh;width:
100vw;displa
y: flex;
margin: 0;
background-color:black;
input.js
}
letinputDirection= {x:0,y:0}
letlastInputDirection={x:0,y:0}
#game-board{
background-
window.addEventListener('keydown',e=>{
color:
switch (e.key)
#CCC;width:
{case'ArrowUp':
100vmin; height:
if (lastInputDirection.y !== 0) breakinputDirection={x: 0,y:-1}break
case'ArrowDown':
100vmin;display:
if grid;
(lastInputDirection.y !== 0) breakinputDirection={x:0,y: 1} break
case'ArrowLeft':
grid-template-rows:
if (lastInputDirection.x
repeat(21, 1fr);grid- !== 0) breakinputDirection={x:-1,y: 0}
template-
columns:repeat(21,1fr);
}
.snake{
background-color: hsl(200,
100%, 50%);border:
.25vminsolidblack;
}
.food{
background-color: hsl(50,
100%, 50%);border:
.25vminsolidblack;
}
</style>
break
case'ArrowRight':
if (lastInputDirection.x !== 0)
breakinputDirection={x:1,y: 0} break
}
})
exportfunctiongetInputDirection(){
lastInputDirection=inputDirectionre turninputDirection
}
snake.js
import{getInputDirection}from"./input.js"
exportconstSNAKE_SPEED=5
const snakeBody = [{ x: 11, y: 11
}]letnewSegments=0
constinputDirection=getInputDirection()
for (let i = snakeBody.length - 2; i >= 0; i--)
{snakeBody[i+1] ={...snakeBody[i]}
}
snakeBody[0].x +=
inputDirection.xsnakeBody[0].y+=
in putDirection.y
}
exportfunctiondraw(gameBoard){sna
keBody.forEach(segment=>{
const snakeElement =
document.createElement('div')snakeElement.style.gr
idRowStart =
segment.ysnakeElement.style.gridColumnStart =
segment.xsnakeElement.classList.add('snake')gameBo
ard.appendChild(snakeElement)
})
}
exportfunctionsnakeIntersection(){ returnonSnake(snakeBody[0],
{ignoreHead:true})
}
functionequalPositions(pos1,pos2){
returnpos1.x===pos2.x&&pos1.y===pos2.y
}
functionaddSegments(){
for(leti=0;i<newSegments;i++){
snakeBody.push({...snakeBody[snakeBody.length-1]})
}
newSegments =0
}
8.0 Output:
12.0 Conclusion:
We learned about developing a web page using CSS(JavaScript).