Skip to content

Commit f38388d

Browse files
Merge pull request #584 from Mudita-Singh/fix/snake-restart-experience
fix: resolve snake game restart and overlay issues
2 parents 4c1414b + ff4a4b4 commit f38388d

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

web-app/js/projects/snake.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ const CONFIG_DIFFICULTY = {
192192
};
193193

194194
function main(ctime) {
195+
const canvas = document.getElementById('snakeCanvas');
196+
if (!canvas) return; // Exit loop if the game modal has been closed
197+
195198
window.requestAnimationFrame(main);
196199
if ((ctime - lastPaintTime) / 1000 < 1 / speed) {
197200
return;
@@ -286,6 +289,18 @@ function gameEngine() {
286289
ctx.fillRect(food.x * 20, food.y * 20, 18, 18);
287290
}
288291

292+
function restartGame() {
293+
const selector = document.getElementById('difficultySelect');
294+
applyDifficultySettings();
295+
if (selector) selector.disabled = true;
296+
document.getElementById('game-over-overlay').classList.add('hidden');
297+
direction = { x: 1, y: 0 };
298+
snakeArr = [{ x: 13, y: 10 }];
299+
score = 0;
300+
document.getElementById('score').innerHTML = score;
301+
food = { x: 6, y: 7 };
302+
}
303+
289304
// Initialize execution listeners
290305
function initSnakeGame() {
291306
window.requestAnimationFrame(main);
@@ -333,6 +348,7 @@ function initSnakeGame() {
333348
applyDifficultySettings();
334349
// Prevent changing parameter matrices on an active engine run
335350
if (selector) selector.disabled = true;
351+
document.getElementById('game-over-overlay').classList.add('hidden');
336352
direction = { x: 1, y: 0 }; // Start moving right
337353
});
338354

0 commit comments

Comments
 (0)