From 085a4f4873faff56e8705b82c7bf729ebcad929f Mon Sep 17 00:00:00 2001 From: nikhilsanjeev29 Date: Tue, 13 Jan 2026 17:52:36 +0000 Subject: [PATCH 1/2] update wrapped and walls mode --- index.html | 10 +++++++++ script.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++++----- style.css | 3 ++- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index 2216810..927ac30 100644 --- a/index.html +++ b/index.html @@ -34,8 +34,18 @@

Select Difficulty

+ + +
+
+

Select Mode

+ + +
+
+ diff --git a/script.js b/script.js index f54a3a4..195f93c 100644 --- a/script.js +++ b/script.js @@ -67,19 +67,26 @@ const restartGame = () => { const startGame = (difficulty) => { switch (difficulty) { case 'easy': - gameSpeed = 200; // Slower speed for easy + gameSpeed = 125; // Slower speed for easy break; case 'medium': - gameSpeed = 125; // Medium speed + gameSpeed = 100; // Medium speed break; case 'hard': gameSpeed = 75; // Faster speed for hard break; + case 'impossible': + gameSpeed = 30; // Faster speed for impossible + break; } // Hide the difficulty modal document.getElementById("difficultyModal").style.display = "none"; + // Show the mode modal + + document.getElementById("wallsWrapModal").style.display = "block"; + // Initialize the food position changeFoodPosition(); @@ -87,6 +94,21 @@ const startGame = (difficulty) => { gameInterval = setInterval(initGame, gameSpeed); } +let gameWalls = false; + +function changeModeWall() { + gameWalls = true; + console.log('wall'); + document.getElementById("wallsWrapModal").style.display = "none"; +} + + +function changeModeWrap() { + gameWalls = false; + console.log('wrap'); + document.getElementById("wallsWrapModal").style.display = "none"; +} + // Main function to update the game state const initGame = () => { // Save the current tail position @@ -98,11 +120,38 @@ const initGame = () => { snakeY += velocityY; // Check for border collision (game over condition) - if (snakeX < 1 || snakeX > 30 || snakeY < 1 || snakeY > 30) { - gameOver(); - return; + + if (gameWalls) { + console.log('aaaa') + if (snakeX < 1 || snakeX > 30 || snakeY < 1 || snakeY > 30) { + gameOver(); + return; + + } + } else { + if (snakeX < 1) { + snakeX = 30 + velocityX = -1; + velocityY = 0; + } + if (snakeX > 30) { + snakeX = 1 + velocityX = 1; + velocityY = 0; + } + if (snakeY < 1) { + snakeY = 30 + velocityX = 0; + velocityY = -1; + } + if (snakeY > 30) { + snakeY = 1 + velocityX = 0; + velocityY = 1; + } } + // Check for self-collision (game over condition) for (let i = 0; i < snakeBody.length; i++) { if (snakeX === snakeBody[i][0] && snakeY === snakeBody[i][1]) { @@ -150,5 +199,9 @@ const initGame = () => { // Show the difficulty selection modal on page load document.getElementById("difficultyModal").style.display = "flex"; +// Hideh the mode modal on page load + +document.getElementById("wallsWrapModal").style.display = "none"; + // Add event listener for keypress to change the snake's direction document.addEventListener('keydown', changeDirection); diff --git a/style.css b/style.css index fce24b5..4dc32ae 100644 --- a/style.css +++ b/style.css @@ -39,7 +39,7 @@ body { display: grid; grid-template-rows: repeat(30, 1fr); grid-template-columns: repeat(30, 1fr); - background-color: #212837; + background-color: #074201; } .play-board .food { @@ -121,6 +121,7 @@ body { justify-content: center; } + .difficulty-content { background-color: #fefefe; margin: auto; From 94d3560db3f891d62dab1a00b9c71501cc783cfd Mon Sep 17 00:00:00 2001 From: nikhilsanjeev29 Date: Thu, 15 Jan 2026 17:31:15 +0000 Subject: [PATCH 2/2] update ISSUE makes snake only be able to do one input at a time --- script.js | 43 ++++++++++++++++++++++++++++--------------- style.css | 2 +- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/script.js b/script.js index 195f93c..b3a3f41 100644 --- a/script.js +++ b/script.js @@ -8,6 +8,7 @@ let highScore = localStorage.getItem("highScore") || 0; let gameInterval; let GameOver = false; let gameSpeed = 125; // Default game speed +let changingDirection = false; // Function to change the food position to a random location const changeFoodPosition = () => { @@ -18,18 +19,22 @@ const changeFoodPosition = () => { // Function to change the direction of the snake based on arrow keys const changeDirection = (e) => { // Prevent the snake from reversing direction - if (e.key === "ArrowUp" && velocityY !== 1) { + if (e.key === "ArrowUp" && velocityY !== 1 && changingDirection == false) { velocityX = 0; velocityY = -1; - } else if (e.key === "ArrowDown" && velocityY !== -1) { + changingDirection = true; + } else if (e.key === "ArrowDown" && velocityY !== -1 && changingDirection == false) { velocityX = 0; velocityY = 1; - } else if (e.key === "ArrowRight" && velocityX !== -1) { + changingDirection = true; + } else if (e.key === "ArrowRight" && velocityX !== -1 && changingDirection == false) { velocityX = 1; velocityY = 0; - } else if (e.key === "ArrowLeft" && velocityX !== 1) { + changingDirection = true; + } else if (e.key === "ArrowLeft" && velocityX !== 1 && changingDirection == false) { velocityX = -1; velocityY = 0; + changingDirection = true; } } @@ -42,7 +47,7 @@ const gameOver = () => { } // Function to restart the game -const restartGame = () => { +const restartGame = () => { // Reset game state snakeX = 5; snakeY = 10; @@ -87,11 +92,6 @@ const startGame = (difficulty) => { document.getElementById("wallsWrapModal").style.display = "block"; - // Initialize the food position - changeFoodPosition(); - - // Start the game with the selected speed - gameInterval = setInterval(initGame, gameSpeed); } let gameWalls = false; @@ -100,6 +100,12 @@ function changeModeWall() { gameWalls = true; console.log('wall'); document.getElementById("wallsWrapModal").style.display = "none"; + + // Initialize the food position + changeFoodPosition(); + + // Start the game with the selected speed + gameInterval = setInterval(initGame, gameSpeed); } @@ -107,6 +113,12 @@ function changeModeWrap() { gameWalls = false; console.log('wrap'); document.getElementById("wallsWrapModal").style.display = "none"; + + // Initialize the food position + changeFoodPosition(); + + // Start the game with the selected speed + gameInterval = setInterval(initGame, gameSpeed); } // Main function to update the game state @@ -122,7 +134,6 @@ const initGame = () => { // Check for border collision (game over condition) if (gameWalls) { - console.log('aaaa') if (snakeX < 1 || snakeX > 30 || snakeY < 1 || snakeY > 30) { gameOver(); return; @@ -192,16 +203,18 @@ const initGame = () => { htmlMarkup += `
`; } + changingDirection = false; + // Update the play board with the new positions playBoard.innerHTML = htmlMarkup; } -// Show the difficulty selection modal on page load -document.getElementById("difficultyModal").style.display = "flex"; - -// Hideh the mode modal on page load +// Hide the mode modal on page load document.getElementById("wallsWrapModal").style.display = "none"; +// Show the difficulty selection modal on page load +document.getElementById("difficultyModal").style.display = "flex"; + // Add event listener for keypress to change the snake's direction document.addEventListener('keydown', changeDirection); diff --git a/style.css b/style.css index 4dc32ae..7a5e732 100644 --- a/style.css +++ b/style.css @@ -39,7 +39,7 @@ body { display: grid; grid-template-rows: repeat(30, 1fr); grid-template-columns: repeat(30, 1fr); - background-color: #074201; + background-color: #129204; } .play-board .food {