Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented score and MaxScore logic. #37

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<body>
<div class="body">
<div id="score">Score: 0</div>
<div id="maxScoreCont">Max Score: 0</div>
<div id="maxScoreCont">Max Score:</div>
<div id="board">
</div>
</div>
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const gameOverSound = new Audio('music/gameOver.mp3');
const moveSound = new Audio('music/move.mp3');
const musicSound = new Audio('music/music.mp3');
let speed = 5;
let score=0;
let maxScore = localStorage.getItem('maxScore') || 0;
let lastPaintTime = 0;
let snakeArr = [
{ x: 13, y: 15 }
Expand Down Expand Up @@ -44,14 +46,23 @@ function gameEngine() {
musicSound.pause();
inputDir = { x: 0, y: 0 };
alert("Game over. Press any key to play again");
if (score > maxScore) {
maxScore = score;
localStorage.setItem('maxScore', maxScore);
maxScoreCont.textContent = `Max Score: ${maxScore}`;
}
snakeArr = [{ x: 13, y: 15 }];
score = 0; // Reset score
scoreCont.textContent = `Score: ${score}`;
// musicSound.play();
}

//IF you have eaten the food, increment the score and regenerate the food
if (snakeArr[0].y === food.y && snakeArr[0].x === food.x) {
// console.log("food")
foodSound.play();
score++;
scoreCont.textContent = `Score: ${score}`;

snakeArr.unshift({ x: snakeArr[0].x + inputDir.x, y: snakeArr[0].y + inputDir.y });
// console.log(snakeArr)
Expand Down Expand Up @@ -147,6 +158,7 @@ function gameEngine() {

//Main logic starts here
window.requestAnimationFrame(main);
maxScoreCont.innerHTML=`Max Score ${maxScore}`
window.addEventListener('keydown', e => {
// inputDir = { x: 0, y: 1 } //start the game
moveSound.play();
Expand Down