Skip to content
Snippets Groups Projects
Commit 7c534ab1 authored by ys678's avatar ys678
Browse files

added energy (green color) to let snake move faster

parent d69aedb5
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ const cellTypes = {
FOOD: 3,
BANANA: 4,
BORDER: 5,
ENERGY: 6,
};
const COLLISIONTYPES = {
......@@ -13,6 +14,7 @@ const COLLISIONTYPES = {
BANANA: 2,
BORDER: 3,
SNAKE: 4,
ENERGY: 5,
};
function createCell(x, y, type) {
......@@ -56,6 +58,15 @@ function addFood(gameBoard) {
randomCell.type = cellTypes.BANANA;
}
// Add ENERGY if it doesn't already exist
let energyExists = gameBoard.some((row) =>
row.some((cell) => cell.type === cellTypes.ENERGY),
);
if (!energyExists && emptySpaces.length > 0) {
let randomCell = emptySpaces[getRandomInt(emptySpaces.length)];
randomCell.type = cellTypes.ENERGY;
}
return gameBoard;
}
......@@ -85,6 +96,12 @@ function checkCollisions(newY, newX, gameBoard) {
console.log("Slip");
return COLLISIONTYPES.BANANA;
}
//Handle collision with energy
if (gameBoard[newY][newX].type == cellTypes.ENERGY) {
console.log("Player has collected ENERGY!");
return COLLISIONTYPES.ENERGY;
}
//Handle collision with border
if (gameBoard[newY][newX].type == cellTypes.BORDER) {
console.log("Void");
......@@ -177,6 +194,20 @@ module.exports = {
let collision = checkCollisions(newY, newX, gameBoard);
if (collision === COLLISIONTYPES.ENERGY) {
let playerId = cell.pid;
console.log(`Player ${playerId} gained speed boost!`);
// Double speed for 5 seconds
if (!speedBoostTimers[playerId]) {
speedBoostTimers[playerId] = setTimeout(() => {
clearTimeout(speedBoostTimers[playerId]);
speedBoostTimers[playerId] = null;
console.log(`Player ${playerId}'s speed boost ended.`);
}, 5000); // Boost lasts 5 seconds
}
}
if (
collision === COLLISIONTYPES.BANANA ||
collision === COLLISIONTYPES.FOOD
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment