From 705afa9dfd3c1429c93ae64cb5495fc414e400f3 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 27 Mar 2021 13:03:46 +0100 Subject: [PATCH] Add pause on escape --- sources/js/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sources/js/index.js b/sources/js/index.js index 350c397..3b19ba7 100644 --- a/sources/js/index.js +++ b/sources/js/index.js @@ -17,6 +17,7 @@ const dieSound = new Audio("sources/sound/onDie.mp3"); const gameOverSound = new Audio("sources/sound/onGameOver.mp3"); const game = new Game(canvas); const scores = []; +let inGame = false; let gameOverModal; @@ -43,6 +44,7 @@ req.onload = () => { req.send(); game.onStart = () => { + inGame = true; updateLives(game.lives); updateScore(game.score); menu.classList.add("invisible"); @@ -52,6 +54,7 @@ game.onStart = () => { }; game.onStop = () => { + inGame = false; menu.classList.remove("invisible"); gameZone.classList.add("invisible"); }; @@ -133,11 +136,16 @@ function reloadScoreBoard() { } -document.addEventListener('DOMContentLoaded', function() { +document.addEventListener('DOMContentLoaded', () => { M.AutoInit(); gameOverModal = M.Modal.getInstance(gameOver); }); +document.addEventListener("keydown", ev => { + if (inGame && ev.key === "Escape") + alert("Game in pause..."); +}); + audio.addEventListener("canplaythrough", () => { audio.play(); });