Skip to content
Snippets Groups Projects
Commit f1670a06 authored by jjd358's avatar jjd358
Browse files

Merge branch 'test-of-collision&growing' into 'main'

Update game.js for collision & growing features

See merge request !10
parents 01940a3d 194bafa7
Branches
No related tags found
1 merge request!10Update game.js for collision & growing features
......@@ -80,6 +80,16 @@
);
ctx.fillStyle = "white";
}
if (board[i][j].type === 4) {
ctx.fillStyle = "yellow";
ctx.fillRect(
j * widthStep + 1,
i * heightStep + 1,
widthStep - 2,
heightStep - 2,
);
ctx.fillStyle = "white";
}
}
}
};
......
......@@ -3,6 +3,7 @@ const cellTypes = {
PLAYERHEAD: 1,
PLAYERBODY: 2,
FOOD: 3,
BANANA: 4
};
function createCell(x, y, type) {
......@@ -31,12 +32,19 @@ function addFood(gameBoard) {
}
}
let bananaExists = gameBoard.some(row => row.some(cell => cell.type === cellTypes.BANANA));
//Create food cells randomly
if (emptySpaces.length > 0) {
let randomCell = emptySpaces[getRandomInt(emptySpaces.length)];
randomCell.type = cellTypes.FOOD;
}
if (!bananaExists && emptySpaces.length > 0) {
let randomCell = emptySpaces[getRandomInt(emptySpaces.length)];
randomCell.type = cellTypes.BANANA;
}
return gameBoard;
}
......@@ -90,6 +98,9 @@ module.exports = {
},
moveOneStep: (gameBoard) => {
// Save board state to allow multiple snake movements
let updatedBoard = gameBoard.map(row => row.map(cell => ({ ...cell })));
// Loop through board until we find a PLAYERHEAD
for (var i = 0; i < gameBoard.length; i++) {
for (var j = 0; j < gameBoard[i].length; j++) {
......@@ -117,26 +128,56 @@ module.exports = {
newX -= 1;
}
//Continue if within bounds
// Check for collisions with walls
if (
newX >= 0 &&
newX < gameBoard[0].length &&
newY >= 0 &&
newY < gameBoard.length
newX < 0 ||
newX >= gameBoard[0].length ||
newY < 0 ||
newY >= gameBoard.length
) {
console.log(`Player ${cell.pid} has collided with the wall!`);
// Remove the player from the game (indicating death)
updatedBoard = updatedBoard.map(row =>
row.map(c => (c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c))
);
continue;
}
// Handle collision with food
if (gameBoard[newY][newX].type === cellTypes.FOOD) {
console.log(`Player ${cell.pid} has eaten the food!`);
// Add new body segment at the current head's position
let newBodySegment = createCell(oldX, oldY, cellTypes.PLAYERBODY);
newBodySegment.pid = cell.pid;
newBodySegment.next = cell.next;
cell.next = newBodySegment;
// Place new food somewhere else
updatedBoard = addFood(updatedBoard);
}
// Handle collision with banana
if (gameBoard[newY][newX].type == cellTypes.BANANA) {
console.log('Slip');
updatedBoard[newY][newX] = createCell(newX, newY, cellTypes.EMPTY);
updatedBoard = updatedBoard.map(row =>
row.map(c => (c.pid === cell.pid ? createCell(c.x, c.y, cellTypes.EMPTY) : c))
);
updatedBoard = addFood(updatedBoard);
continue;
}
// Snake head to new position by updating cell object
let newHeadCell = createCell(newX, newY, cellTypes.PLAYERHEAD);
newHeadCell.pid = cell.pid;
gameBoard[newY][newX] = newHeadCell;
newHeadCell.direction = tempVar;
gameBoard[newY][newX] = newHeadCell;
newHeadCell.next = cell.next;
updatedBoard[newY][newX] = newHeadCell;
// Remove previous head position
gameBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY);
updatedBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY);
//Snake body to previous locations
let currentBody = gameBoard[newY][newX].next;
// Move the body segments
let currentBody = newHeadCell.next;
while (currentBody) {
let bodyOldX = currentBody.x;
let bodyOldY = currentBody.y;
......@@ -144,7 +185,7 @@ module.exports = {
currentBody.y = oldY;
// Update cell body position
gameBoard[oldY][oldX] = {
updatedBoard[oldY][oldX] = {
...currentBody,
type: cellTypes.PLAYERBODY,
};
......@@ -153,16 +194,14 @@ module.exports = {
currentBody = currentBody.next;
}
//Remove previous body position
gameBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY);
console.log(gameBoard);
return gameBoard;
// Remove previous body position if it's no longer part of the snake
if (oldX !== newHeadCell.x || oldY !== newHeadCell.y) {
updatedBoard[oldY][oldX] = createCell(oldX, oldY, cellTypes.EMPTY);
}
}
}
}
return gameBoard;
return updatedBoard;
},
checkCollisions: (gameBoard) => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment