Skip to content

Commit

Permalink
added check for food spawning on snake, issue iiitl#9
Browse files Browse the repository at this point in the history
  • Loading branch information
Karanagarwal12 committed Mar 14, 2024
1 parent e9e6781 commit 9beaeb3
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function main(ctime) {
function isCollide(snake) {
// return false;
//if you into yourself

if (snake[0].x > 18 || snake[0].x < 0 || snake[0].y > 18 || snake[0].y < 0) {
return true;
}
Expand Down Expand Up @@ -62,9 +62,23 @@ function gameEngine() {
// console.log(snakeArr)
let a = 2;
let b = 16;
food = { x: 2 + Math.round(a + (b - a) * Math.random()), y: Math.round(a + (b - a) * Math.random()) }
let xx = 2 + Math.round(a + (b - a) * Math.random());
let yy = Math.round(a + (b - a) * Math.random());
while (checkFoodOnSnake(xx, yy)) {
xx = 2 + Math.round(a + (b - a) * Math.random());
yy = Math.round(a + (b - a) * Math.random());
}
food = { x: xx, y: yy }
}

function checkFoodOnSnake(x, y) {
for (let i = 0; i < snakeArr.length; i++) {
if (snakeArr[i].x === x && snakeArr[i].y === y) {
return true;
}
}
return false;
}

//Moving the snake
// console.log("-----")
// console.log(snakeArr.l)
Expand Down

0 comments on commit 9beaeb3

Please sign in to comment.