From fb695427e1b5243a96382e9cabfd3b80b1c9e7ca Mon Sep 17 00:00:00 2001 From: Himanshu Bhardwaj Date: Mon, 10 Jun 2024 22:11:26 +0530 Subject: [PATCH] gameOver() added, but boundry conditions work incorrectly. --- jsSnake/snake.js | 69 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/jsSnake/snake.js b/jsSnake/snake.js index 3359c96..26000aa 100644 --- a/jsSnake/snake.js +++ b/jsSnake/snake.js @@ -19,6 +19,7 @@ class Snake { constructor(positionArray, color) { this.positionArray = [...positionArray]; this.color = color; + this.currentInterval = null; } // Index/ Array value reducers for array as well @@ -50,6 +51,38 @@ class Snake { add(index) { this.positionArray.push(index); } + + // a move() function using which snake moves in the other direction + move(increment) { + if (this.currentInterval) { + clearInterval(this.currentInterval); + } + this.currentInterval = setInterval(() => { + this.undraw(); + this.reducer(increment); + this.draw(); + + if (this.gameOver(increment)) { + clearInterval(this.currentInterval); + console.log(`Game Over due to ${increment}`); + } + }, 1000); + } + + // game-Over function() which checks if game is over due to collision, currently only with walls + gameOver(increment) { + let head = this.positionArray[0]; + if ( + head <= 9 || + head >= 90 || + (increment == -1 && head % 10 == 0) || + (increment == 1 && head % 10 == 9) + ) { + return true; + } else { + return false; + } + } } class Apple { @@ -82,46 +115,36 @@ class Apple { } } -let snake = new Snake([50], 'green'); -let apple = new Apple(0, 'red'); - -snake.draw(); -apple.draw(); - document.addEventListener('keydown', (e) => { switch (e.code) { case 'ArrowUp': - snake.undraw(); - // Figure out the logic for collision up if (snake.positionArray[0] > 9) { - snake.reducer(-10); + snake.move(-10); } - snake.draw(); break; case 'ArrowDown': - snake.undraw(); - // Figure out the logic for collision down - // includes() can only check for one item if (snake.positionArray[0] < 90) { - snake.reducer(10); + snake.move(10); } - snake.draw(); break; case 'ArrowLeft': - snake.undraw(); - if (snake.positionArray[0] % 10 != 0) { - snake.reducer(-1); + if (snake.positionArray[0] % 10 !== 0) { + snake.move(-1); } - snake.draw(); break; case 'ArrowRight': - snake.undraw(); - if (snake.positionArray[0] % 10 != 9) { - snake.reducer(1); + if (snake.positionArray[0] % 10 !== 9) { + snake.move(1); } - snake.draw(); break; default: break; } }); + +let snake = new Snake([50], 'green'); +let apple = new Apple(0, 'red'); + +snake.draw(); +// snake.move(1); +apple.draw();