Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ const right = new Audio();
const left = new Audio();
const cheer = new Audio();



const box = 32;
const cvs = document.getElementById("gamescreen");
const ctx = cvs.getContext("2d");
let message = "Your Score is: "
let snake = [];
let score = 0;
localStorage.getItem('topScores')?localStorage.getItem('topScores'):[0];
countWins(score);
let d;
let highestScore = localStorage.getItem('highestScore') ? localStorage.getItem('highestScore') : 0 ;

foodImg.src = "images/food.png";
background.src = "images/ground.png";
dead.src = "audio/dead.mp3";
Expand Down Expand Up @@ -111,10 +116,12 @@ function draw() {
else{
dead.play();
}
countWins(score);
setTimeout(() =>{
alert("Game Over! \n"+ message + score + "\nWanna Play Again ?");
// alert("Game Over! \n"+ message + score + "\nWanna Play Again ?");
window.location.reload()
},2500)
},500)

}

snake.unshift(newHead);
Expand Down Expand Up @@ -523,3 +530,33 @@ confetti.resize();
});
}

function countWins(score){
console.log("my score",score);
var topScores=localStorage.getItem('topScores')?localStorage.getItem('topScores'):[0];
topScores=JSON.parse(topScores);
console.log(topScores);
console.log(topScores.length);
if(topScores.length<5&&!topScores.includes(score)){
topScores.push(score);
}
else if(topScores.length>=5&&!topScores.includes(score)&&Math.min(...topScores)<score){
topScores[topScores.indexOf(Math.min(...topScores))]=score;

}
topScores.sort((score1,score2)=>{
return score2-score1;
})
var countWinBodyScore=document.querySelector('.countWinsBody-scores');
countWinBodyScore.innerHTML='';
topScores.forEach(score => {

var scoreEle=document.createElement('div');
scoreEle.className="allScores";
scoreEle.innerHTML=`Score is : ${score}`;
countWinBodyScore.appendChild(scoreEle);
});
topScores=JSON.stringify(topScores);
localStorage.setItem('topScores',topScores);
}


15 changes: 13 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@
<title>Snake Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<canvas id="confetti"></canvas>
<canvas id="gamescreen" width="608" height="608"></canvas>
<body>
<div class="countWinsBody">
<h2>Your Top Scores:</h2>
<div class="countWinsBody-scores">

</div>
</div>
<div>
<canvas id="confetti"></canvas>
<canvas id="gamescreen" width="608" height="608"></canvas>
</div>
</body>

<script src="game.js"></script>

</html>
32 changes: 29 additions & 3 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
body{
display: flex;
}
canvas {
display: block;
margin-left: auto;
/* display: block; */
margin-top: 75px;
margin-left: 75px;
margin-right: auto;
}
#confetti{
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
width: 50%;
}

.allScores{
align-content: center;
box-sizing: border-box;
margin: 0;
border-radius: 2px;
border: .8px solid rgb(250, 240, 240);
box-shadow: 0 10px 6px -6px #777;
width: 700px;
padding :40px;
/* padding-top: 10px; */
text-align: center;
background:rgb(226, 223, 223);
color:rgb(199, 35, 172);
margin-top: 10px;
font-size: large;
}

.countWinsBody h2{
color: rgb(36, 66, 196);
text-align: center;
}