From 36a43ce31931f754d58fcb1eaa30dd3f05a472b8 Mon Sep 17 00:00:00 2001 From: Jake Dreher <jdreherschool@gmail.com> Date: Wed, 4 Sep 2024 17:08:38 -0400 Subject: [PATCH] Implement snake collision --- src/game/game.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/game/game.js b/src/game/game.js index 801a4c2..b04c7bc 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) => -- GitLab