Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
update ISSUE makes snake only be able to do one input at a time
  • Loading branch information
nikhilsanjeev29 committed Jan 15, 2026
commit 94d3560db3f891d62dab1a00b9c71501cc783cfd
43 changes: 28 additions & 15 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -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;
}
}

Expand All @@ -42,7 +47,7 @@ const gameOver = () => {
}

// Function to restart the game
const restartGame = () => {
const restartGame = () => {
// Reset game state
snakeX = 5;
snakeY = 10;
Expand Down Expand Up @@ -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;
Expand All @@ -100,13 +100,25 @@ 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);
}


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
Expand All @@ -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;
Expand Down Expand Up @@ -192,16 +203,18 @@ const initGame = () => {
htmlMarkup += `<div class="body" style='grid-area: ${snakeBody[i][1]} / ${snakeBody[i][0]}'></div>`;
}

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);
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down