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

Added Celebratory Popup for High Score Achievements #70

Closed
wants to merge 4 commits 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
43 changes: 42 additions & 1 deletion index.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,45 @@
border-radius: 50%;
margin: auto;
/* border-radius: 50%; */
}
}

/* index.css */

.game-over-popup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(255, 255, 255, 0.9);
padding: 20px;
border-radius: 10px;
box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.2);
text-align: center;
}

.game-over-popup h2 {
margin-bottom: 20px;
font-size: 28px;
color: #333;
}

.game-over-popup p {
margin-bottom: 15px;
font-size: 20px;
color: #555;
}

.game-over-popup button {
padding: 10px 30px;
background-color: #ff5f5f;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 18px;
transition: background-color 0.3s;
}

.game-over-popup button:hover {
background-color: #ff3f3f;
}
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
<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>

<script src="index.js"></script>
</body>

Expand Down
34 changes: 33 additions & 1 deletion 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 @@ -43,15 +45,44 @@ function gameEngine() {
gameOverSound.play();
musicSound.pause();
inputDir = { x: 0, y: 0 };
alert("Game over. Press any key to play again");
const popup = document.createElement('div');
popup.classList.add('game-over-popup');
if(score>maxScore){
popup.innerHTML = `
<h2>Congratulations! High Score</h2>
<p>Your Score: ${score}</p>
<p>Old High Score: ${maxScore}</p>
<button id="play-again-btn">Play Again</button>
`;
}else{
alert("Game over. Press any key to play again");
}

document.body.appendChild(popup);
if (score > maxScore) {
maxScore = score;
localStorage.setItem('maxScore', maxScore);
maxScoreCont.textContent = `Max Score: ${maxScore}`;
}
snakeArr = [{ x: 13, y: 15 }];
score = 0; // Reset score
// speed = 5;
scoreCont.textContent = `Score: ${score}`;
// musicSound.play();
const playAgainBtn = document.getElementById('play-again-btn');
playAgainBtn.addEventListener('click', () => {
document.body.removeChild(popup);
window.requestAnimationFrame(main);
});
}

//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++;
// speed=speed+0.1;
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 +178,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