collision function... 🐍
This commit is contained in:
parent
3f0bee34ce
commit
1908a36f82
30
src/index.js
30
src/index.js
@ -145,9 +145,24 @@ const makeFood = () => {
|
|||||||
const x = _.random(50);
|
const x = _.random(50);
|
||||||
const y = _.random(50);
|
const y = _.random(50);
|
||||||
|
|
||||||
|
if (_.find(snakeBody, { x, y })) {
|
||||||
|
console.log('food placed under the snake...');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Food was placed under the snake
|
||||||
|
*/
|
||||||
|
return makeFood();
|
||||||
|
/**
|
||||||
|
* So we made food again... yolo
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
food.x = x;
|
food.x = x;
|
||||||
food.y = y;
|
food.y = y;
|
||||||
|
|
||||||
|
const snakeLength = snake.log.length - 1;
|
||||||
|
const snakeBody = snake.log.slice((snakeLength - snake.body), snakeLength);
|
||||||
|
|
||||||
setBrickColor('blue', x, y);
|
setBrickColor('blue', x, y);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -163,11 +178,26 @@ const checkIfSnakeIsEating = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check collision
|
||||||
|
*/
|
||||||
|
|
||||||
|
const checkCollision = () => {
|
||||||
|
const snakeLength = snake.log.length - 2;
|
||||||
|
const snakeBody = snake.log.slice((snakeLength - snake.body), snakeLength);
|
||||||
|
const snakeHead = { x: snake.x, y: snake.y };
|
||||||
|
|
||||||
|
if (_.find(snakeBody, snakeHead)) {
|
||||||
|
alert('Oh noooo! Collision.... Start again?');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run game
|
* Run game
|
||||||
*/
|
*/
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
|
checkCollision();
|
||||||
moveSnake(snake.direction);
|
moveSnake(snake.direction);
|
||||||
checkIfSnakeIsEating();
|
checkIfSnakeIsEating();
|
||||||
}, snake.speed);
|
}, snake.speed);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user