Skip to content

Commit

Permalink
Implement Game Over on Snake Biting Itself iiitl#4
Browse files Browse the repository at this point in the history
  • Loading branch information
akshita-86 committed Mar 19, 2024
1 parent 4dc72d0 commit 5c01520
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,26 @@ function main(ctime) {
gameEngine();
// console.log(ctime);
}
// Function to check if snake collides with itself
function isCollide(snake) {
// return false;
//if you into yourself

// Check collision with walls
if (snake[0].x > 18 || snake[0].x < 0 || snake[0].y > 18 || snake[0].y < 0) {
return true;
}

// Check collision with itself
for (let i = 1; i < snake.length; i++) {
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) {
return true;
}
}

return false;
}




function gameEngine() {
//part1: updating the snake array and food
if (isCollide(snakeArr)) {
Expand All @@ -47,6 +59,7 @@ function gameEngine() {
snakeArr = [{ x: 13, y: 15 }];
// musicSound.play();
}


//IF you have eaten the food, increment the score and regenerate the food
if (snakeArr[0].y === food.y && snakeArr[0].x === food.x) {
Expand Down

0 comments on commit 5c01520

Please sign in to comment.