diff --git a/src/game/game.js b/src/game/game.js index 801a4c216fdf738705a605b9a2ef8de6487f2f95..b04c7bca263e4dcad11444a199ddad4ffa4b93c7 100644 --- a/src/game/game.js +++ b/src/game/game.js @@ -76,6 +76,10 @@ function checkCollisions(newY, newX, gameBoard) { console.log(`Player has eaten the food!`); return COLLISIONTYPES.FOOD; } + if (gameBoard[newY][newX].type === cellTypes.PLAYERHEAD) { + console.log(`Player has collided with another player!`); + return COLLISIONTYPES.SNAKE; + } //Handle collision with banana if (gameBoard[newY][newX].type == cellTypes.BANANA) { console.log("Slip"); @@ -179,6 +183,23 @@ module.exports = { ) { updatedBoard = addFood(updatedBoard); } + if (collision === COLLISIONTYPES.SNAKE) { + updatedBoard = updatedBoard.map((row) => + row.map((c) => + c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c, + ), + ); + deadPlayers.push(cell.pid); + updatedBoard = updatedBoard.map((row) => + row.map((c) => + c.pid === gameBoard[newY][newX].pid + ? createCell(c.x, c.y, cellTypes.EMPTY) + : c, + ), + ); + deadPlayers.push(gameBoard[newY][newX].pid); + continue; + } if (collision > COLLISIONTYPES.FOOD) { updatedBoard = updatedBoard.map((row) => row.map((c) =>