Skip to content

Commit 618354f

Browse files
author
jjteoh
committed
feat: add pause/resume mechanism
1 parent 003f48a commit 618354f

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ <h2>Classic Snake</h2>
4141
<button id="startButton">Start Game</button>
4242
</div>
4343
</div>
44+
45+
<div id="pausedMessage" class="overlay hidden">
46+
<div class="message-box">
47+
<h2>PAUSED</h2>
48+
</div>
49+
</div>
4450
</div>
4551

4652
<div class="controls-container">

script.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const startButton = document.getElementById("startButton");
1010
const gameContainer = document.querySelector(".game-container");
1111
const canvasContainer = document.querySelector(".canvas-container");
1212
const easterEggVideo = document.getElementById("easterEggVideo");
13+
const pausedMessageElement = document.getElementById("pausedMessage");
1314

1415
// --- Audio Elements ---
1516
const eatSound = new Audio('media/eat.mp3');
@@ -39,6 +40,7 @@ let changingDirection = false;
3940
let gameLoopInterval = null;
4041
let gameActive = false;
4142
let currentSpeed = INITIAL_SPEED_MS;
43+
let isPaused = false;
4244

4345
// --- Touch Controls ---
4446
const btnUp = document.getElementById("btnUp");
@@ -127,7 +129,7 @@ function drawFood() {
127129
}
128130

129131
function moveSnake() {
130-
if (!gameActive) return;
132+
if (!gameActive || isPaused) return;
131133

132134
// snake[0] is the head
133135
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
@@ -194,7 +196,7 @@ function handleKeyDown(event) {
194196
return;
195197
}
196198

197-
if (!gameActive || changingDirection) return;
199+
if (!gameActive || changingDirection || isPaused) return;
198200

199201
const keyPressed = event.key;
200202

@@ -231,7 +233,7 @@ function handleKeyDown(event) {
231233
// handle touch control for mobile players
232234
function handleTouchControl(newDx, newDy) {
233235
// make sure the previous direction is executed before a new direction is set
234-
if (changingDirection || !gameActive) return;
236+
if (changingDirection || !gameActive || isPaused) return;
235237

236238
// just translate condition to booleans for better readability
237239
const goingUp = dy === -1;
@@ -272,6 +274,7 @@ function checkGameOver() {
272274
}
273275

274276
function gameLoop() {
277+
if (isPaused) return;
275278
if (checkGameOver()) {
276279
endGame();
277280
return;
@@ -291,9 +294,11 @@ function startGame() {
291294
dx = 1;
292295
dy = 0;
293296
changingDirection = false;
297+
isPaused = false;
294298
scoreElement.textContent = `Score: ${score}`;
295299
gameOverElement.classList.add("hidden");
296300
startMessageElement.classList.add("hidden");
301+
pausedMessageElement.classList.add("hidden");
297302

298303
const startX = Math.floor(GRID_SIZE / 2);
299304
const startY = Math.floor(GRID_SIZE / 2);
@@ -385,6 +390,34 @@ function triggerEasterEgg() {
385390
};
386391
}
387392

393+
// Function to pause the game
394+
function pauseGame() {
395+
isPaused = true;
396+
pausedMessageElement.classList.remove("hidden");
397+
}
398+
399+
// Function to resume the game
400+
function resumeGame() {
401+
isPaused = false;
402+
pausedMessageElement.classList.add("hidden");
403+
}
404+
405+
// Function to handle space key press
406+
document.addEventListener('keydown', (event) => {
407+
if (event.code === 'Space') {
408+
// is game over or not started, play the game
409+
if (!gameActive &&
410+
(!gameOverElement.classList.contains("hidden") || !startMessageElement.classList.contains("hidden"))
411+
) {
412+
startGame();
413+
} else if (isPaused) {
414+
resumeGame();
415+
} else {
416+
pauseGame();
417+
}
418+
}
419+
});
420+
388421
// --- Event Listeners ---
389422
document.addEventListener("keydown", handleKeyDown);
390423
restartButton.addEventListener("click", startGame);

0 commit comments

Comments
 (0)