let snake = [];
let direction = null;
let apple = null;
let gameInterval = null;
let isGameOver = false;
let isGameStarted = false;
const messageElement = document.getElementById('message');
const faceAdjacency = {
0: { // Front
up: {face:4, x:'x', y: gridSize - 1 - 'x'},
down: {face:5, x:'x', y:'x'},
left: {face:3, x: gridSize - 1, y:'y'},
right: {face:2, x: 0, y:'y'}
},
1: { // Back
up: {face:4, x: gridSize - 1 - 'x', y: 0},
down: {face:5, x: gridSize - 1 - 'x', y: gridSize - 1},
left: {face:2, x: gridSize - 1, y: 'y'},
right: {face:3, x: 0, y: 'y'}
},
2: { // Right
up: {face:4, x: 'y', y: 'x'},
down: {face:5, x: gridSize - 1 - 'y', y: 'x'},
left: {face:0, x: gridSize - 1, y: 'y'},
right: {face:1, x: 0, y: 'y'}
},
3: { // Left
up: {face:4, x: gridSize - 1 - 'y', y: gridSize - 1 - 'x'},
down: {face:5, x: 'y', y: gridSize - 1 - 'x'},
left: {face:1, x: gridSize - 1, y: 'y'},
right: {face:0, x: 0, y: 'y'}
},
4: { // Top
up: {face:1, x: 'x', y: 0},
down: {face:0, x: 'x', y: 0},
left: {face:3, x:'x', y:0},
right: {face:2, x: 'x', y: 0}
},
5: { // Bottom
up: {face:0, x: 'x', y: gridSize - 1},
down: {face:1, x: 'x', y: gridSize -1},
left: {face:3, x:'x', y: gridSize - 1},
right: {face:2, x: 'x', y: gridSize -1}
}
};
function initGame() {
// Clear previous game
snake.forEach(segment => {
removeCell(segment);
});
if (apple) removeCell(apple);
snake = [];
direction = null;
isGameOver = false;
isGameStarted = false;
messageElement.textContent = "Press SPACE to start the game";
// Initialize snake starting position
const startingFace = Math.floor(Math.random() * 6);
const startingX = Math.floor(gridSize / 2);
const startingY = Math.floor(gridSize / 2);
direction = getRandomDirection();
snake.push({face: startingFace, x: startingX, y: startingY});
drawCell(snake[0], 'snake');
spawnApple();
}
function getRandomDirection() {
const dirs = ['up', 'down', 'left', 'right'];
return dirs[Math.floor(Math.random() * dirs.length)];
}
function spawnApple() {
let validCell = false;
while (!validCell) {
const face = Math.floor(Math.random() * 6);
const x = Math.floor(Math.random() * gridSize);
const y = Math.floor(Math.random() * gridSize);
// Check if apple is on snake
if (!snake.some(segment => segment.face === face && segment.x === x && segment.y === y)) {
apple = {face, x, y};
validCell = true;
}
}
drawCell(apple, 'apple');
}
function drawCell(cell, className) {
const cellDiv = document.createElement('div');
cellDiv.classList.add('cell', className);
cellDiv.style.left = (cell.x * cellSize) + 'px';
cellDiv.style.top = (cell.y * cellSize) + 'px';
faceElements[cell.face].appendChild(cellDiv);
cell.element = cellDiv;
}
function removeCell(cell) {
if (cell.element && cell.element.parentNode) {
cell.element.parentNode.removeChild(cell.element);
cell.element = null;
}
}
function gameLoop() {
if (isGameOver) return;
// Calculate new head position
const head = snake[0];
let newHead = {face: head.face, x: head.x, y: head.y};
switch (direction) {
case 'up':
newHead.y -=1;
break;
case 'down':
newHead.y +=1;
break;
case 'left':
newHead.x -=1;
break;
case 'right':
newHead.x +=1;
break;
default:
return; // No movement if direction is null
}
if (newHead.x < 0 || newHead.x >= gridSize || newHead.y < 0 || newHead.y >= gridSize) {
// Need to move to adjacent face
const adjacency = faceAdjacency[newHead.face][direction];
const adjFace = adjacency.face;
let adjX = adjacency.x;
let adjY = adjacency.y;
adjX = (typeof adjX === 'number') ? adjX : eval(adjX.replace(/'x'/g, head.x).replace(/'y'/g, head.y));
adjY = (typeof adjY === 'number') ? adjY : eval(adjY.replace(/'x'/g, head.x).replace(/'y'/g, head.y));
newHead.face = adjFace;
newHead.x = adjX;
newHead.y = adjY;
}
// Check collision with self
if (snake.some(segment => segment.face === newHead.face && segment.x === newHead.x && segment.y === newHead.y)) {
gameOver();
return;
}
// Insert new head
snake.unshift(newHead);
drawCell(newHead, 'snake');
// Check if apple is eaten
if (apple.face === newHead.face && apple.x === newHead.x && apple.y === newHead.y) {
removeCell(apple);
spawnApple();
} else {
// Remove tail
const tail = snake.pop();
removeCell(tail);
}
// Rotate cube to show the face with snake's head
rotateCubeToFace(newHead.face);
}
function rotateCubeToFace(face){
// Reset transform
cube.style.transform = `translate(-50%, -50%)`;
// Apply rotation
let rotationX = 0;
let rotationY = 0;
switch(face){
case 0: // Front
rotationY = 0;
break;
case 1: // Back
rotationY = 180;
break;
case 2: // Right
rotationY = -90;
break;
case 3: // Left
rotationY = 90;
break;
case 4: // Top
rotationX = -90;
break;
case 5: // Bottom
rotationX = 90;
break;
}
cube.style.transform += ` rotateX(${rotationX}deg) rotateY(${rotationY}deg)`;
}
function gameOver() {
isGameOver = true;
clearInterval(gameInterval);
messageElement.textContent = "Game Over! Press SPACE to restart";
}
// Handle key input
document.addEventListener('keydown', function(e) {
if (isGameOver && e.code === 'Space') {
initGame();
isGameStarted = true;
messageElement.textContent = "";
gameInterval = setInterval(gameLoop, 300);
}
if (!isGameStarted && e.code === 'Space') {
isGameStarted = true;
messageElement.textContent = "";
gameInterval = setInterval(gameLoop, 300);
}
if (e.key === 'w' || e.key === 'ArrowUp') {
if (direction !== 'down') direction = 'up';
} else if (e.key === 's' || e.key === 'ArrowDown') {
if (direction !== 'up') direction = 'down';
} else if (e.key === 'a' || e.key === 'ArrowLeft') {
if (direction !== 'right') direction = 'left';
} else if (e.key === 'd' || e.key === 'ArrowRight') {
if (direction !== 'left') direction = 'right';
}
});
// Start the game
initGame();
})();
</script>
To enable the snake to seamlessly traverse across all six faces of the cube, we need to ensure that transitions between faces are handled correctly. This involves accurately mapping the snake's position (x, y) and face (face) when it crosses the boundaries of the current face.
The current implementation uses faceAdjacency with string-based expressions and eval, which can be error-prone and difficult to maintain. Instead, we'll refactor the adjacency mapping to use explicit functions that calculate the new coordinates based on the current position and direction of movement.
Here's a step-by-step guide to implementing this functionality:
Define a Robust Face Adjacency Mapping:
We'll create a faceAdjacency object where each face maps to its adjacent faces in all four directions (up, down, left, right). For each direction, we'll specify:
The adjacent face.
A function to calculate the new x and y coordinates on the adjacent face based on the current position.
Update the Game Loop:
Modify the gameLoop function to use the new faceAdjacency mappings. When the snake's head crosses a boundary, determine the new face and calculate the corresponding x and y coordinates using the provided functions.
Ensure Consistent Rotation:
Adjust the rotateCubeToFace function to rotate the cube smoothly to the current face where the snake's head is located.
Enhance Code Readability and Maintainability:
By using functions within faceAdjacency, the code becomes cleaner and easier to manage, especially when dealing with coordinate transformations across different faces.
Below is the complete updated code with the necessary modifications:
html
<!DOCTYPE html>
<html>
<head>
<title>3D Snake Game on Cube Surface</title>
<style>
body {
margin: 0;
overflow: hidden;
background: radial-gradient(circle at center, #000428, #004e92);
}
#game-container {
perspective: 1000px;
width: 100vw;
height: 100vh;
position: relative;
}
#cube {
position: absolute;
top: 50%;
left: 50%;
width: 300px;
height: 300px;
transform-style: preserve-3d;
transform: translate(-50%, -50%) rotateX(-20deg) rotateY(45deg);
transition: transform 0.5s ease-out;
}
.face {
position: absolute;
width: 300px;
height: 300px;
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
box-sizing: border-box;
}
/* Position each face */
#face-0 { /* Front */
transform: rotateY(0deg) translateZ(150px);
}
#face-1 { /* Back */
transform: rotateY(180deg) translateZ(150px);
}
#face-2 { /* Right */
transform: rotateY(90deg) translateZ(150px);
}
#face-3 { /* Left */
transform: rotateY(-90deg) translateZ(150px);
}
#face-4 { /* Top */
transform: rotateX(90deg) translateZ(150px);
}
#face-5 { /* Bottom */
transform: rotateX(-90deg) translateZ(150px);
}
/* Snake and apple cells */
.cell {
position: absolute;
width: 30px; /* Assuming grid size 10x10 */
height: 30px;
box-sizing: border-box;
}
.snake {
background: limegreen;
}
.apple {
background: red;
}
/* Additional styles */
#message {
position: absolute;
top: 20px;
width: 100%;
text-align: center;
color: white;
font-family: Arial, sans-serif;
font-size: 24px;
z-index: 10;
}
</style>
</head>
<body>
<div id="game-container">
<div id="cube">
<div class="face" id="face-0"></div>
<div class="face" id="face-1"></div>
<div class="face" id="face-2"></div>
<div class="face" id="face-3"></div>
<div class="face" id="face-4"></div>
<div class="face" id="face-5"></div>
</div>
<div id="message">Press SPACE to start the game</div>
</div>
<script>
(function() {
const gridSize = 10; // 10x10 grid on each face
const cellSize = 30; // Must match CSS
const faces = [0,1,2,3,4,5];
const cube = document.getElementById('cube');
const faceElements = faces.map(faceIndex => document.getElementById('face-' + faceIndex));
let snake = [];
let direction = null;
let apple = null;
let gameInterval = null;
let isGameOver = false;
let isGameStarted = false;
const messageElement = document.getElementById('message');
// Updated face adjacency with coordinate transformation functions
const faceAdjacency = {
0: { // Front
up: {
face: 4, // Top
transform: (x, y) => ({ x: x, y: gridSize -1 })
},
down: {
face: 5, // Bottom
transform: (x, y) => ({ x: x, y: 0 })
},
left: {
face: 3, // Left
transform: (x, y) => ({ x: gridSize -1, y: y })
},
right: {
face: 2, // Right
transform: (x, y) => ({ x: 0, y: y })
}
},
1: { // Back
up: {
face: 4, // Top
transform: (x, y) => ({ x: gridSize -1 - x, y: gridSize -1 })
},
down: {
face: 5, // Bottom
transform: (x, y) => ({ x: gridSize -1 - x, y: 0 })
},
left: {
face: 2, // Right (Back's left is Right)
transform: (x, y) => ({ x: gridSize -1, y: gridSize -1 - y })
},
right: {
face: 3, // Left (Back's right is Left)
transform: (x, y) => ({ x: 0, y: gridSize -1 - y })
}
},
2: { // Right
up: {
face: 4, // Top
transform: (x, y) => ({ x: y, y: gridSize -1 })
},
down: {
face: 5, // Bottom
transform: (x, y) => ({ x: gridSize -1 - y, y: 0 })
},
left: {
face: 0, // Front
transform: (x, y) => ({ x: gridSize -1, y: y })
},
right: {
face: 1, // Back
transform: (x, y) => ({ x: 0, y: gridSize -1 - y })
}
},
3: { // Left
up: {
face: 4, // Top
transform: (x, y) => ({ x: gridSize -1 - y, y: gridSize -1 })
},
down: {
face: 5, // Bottom
transform: (x, y) => ({ x: y, y: 0 })
},
left: {
face: 1, // Back
transform: (x, y) => ({ x: gridSize -1, y: gridSize -1 - y })
},
right: {
face: 0, // Front
transform: (x, y) => ({ x: 0, y: y })
}
},
4: { // Top
up: {
face: 1, // Back
transform: (x, y) => ({ x: gridSize -1 - x, y: gridSize -1 })
},
down: {
face: 0, // Front
transform: (x, y) => ({ x: x, y: 0 })
},
left: {
face: 3, // Left
transform: (x, y) => ({ x: gridSize -1 - y, y: 0 })
},
right: {
face: 2, // Right
transform: (x, y) => ({ x: y, y: 0 })
}
},
5: { // Bottom
up: {
face: 0, // Front
transform: (x, y) => ({ x: x, y: gridSize -1 })
},
down: {
face: 1, // Back
transform: (x, y) => ({ x: gridSize -1 - x, y: 0 })
},
left: {
face: 3, // Left
transform: (x, y) => ({ x: y, y: gridSize -1 })
},
right: {
face: 2, // Right
transform: (x, y) => ({ x: gridSize -1 - y, y: gridSize -1 })
}
},
};
function initGame() {
// Clear previous game
snake.forEach(segment => {
removeCell(segment);
});
if (apple) removeCell(apple);
snake = [];
direction = null;
isGameOver = false;
isGameStarted = false;
messageElement.textContent = "Press SPACE to start the game";
// Initialize snake starting position
const startingFace = Math.floor(Math.random() * 6);
const startingX = Math.floor(gridSize / 2);
const startingY = Math.floor(gridSize / 2);
direction = getRandomDirection();
snake.push({face: startingFace, x: startingX, y: startingY});
drawCell(snake[0], 'snake');
spawnApple();
}
function getRandomDirection() {
const dirs = ['up', 'down', 'left', 'right'];
return dirs[Math.floor(Math.random() * dirs.length)];
}
function spawnApple() {
let validCell = false;
while (!validCell) {
const face = Math.floor(Math.random() * 6);
const x = Math.floor(Math.random() * gridSize);
const y = Math.floor(Math.random() * gridSize);
// Check if apple is on snake
if (!snake.some(segment => segment.face === face && segment.x === x && segment.y === y)) {
apple = {face, x, y};
validCell = true;
}
}
drawCell(apple, 'apple');
}
function drawCell(cell, className) {
const cellDiv = document.createElement('div');
cellDiv.classList.add('cell', className);
cellDiv.style.left = (cell.x * cellSize) + 'px';
cellDiv.style.top = (cell.y * cellSize) + 'px';
faceElements[cell.face].appendChild(cellDiv);
cell.element = cellDiv;
}
function removeCell(cell) {
if (cell.element && cell.element.parentNode) {
cell.element.parentNode.removeChild(cell.element);
cell.element = null;
}
}
function gameLoop() {
if (isGameOver) return;
// Calculate new head position
const head = snake[0];
let newHead = {face: head.face, x: head.x, y: head.y};
switch (direction) {
case 'up':
newHead.y -=1;
break;
case 'down':
newHead.y +=1;
break;
case 'left':
newHead.x -=1;
break;
case 'right':
newHead.x +=1;
break;
default:
return; // No movement if direction is null
}
// Check if newHead is out of bounds
if (newHead.x < 0 || newHead.x >= gridSize || newHead.y < 0 || newHead.y >= gridSize) {
// Determine the direction of crossing
let crossingDirection;
if (newHead.y < 0) {
crossingDirection = 'up';
} else if (newHead.y >= gridSize) {
crossingDirection = 'down';
} else if (newHead.x < 0) {
crossingDirection = 'left';
} else if (newHead.x >= gridSize) {
crossingDirection = 'right';
}
const currentFace = head.face;
const adjacency = faceAdjacency[currentFace][crossingDirection];
const adjFace = adjacency.face;
const { x: adjX, y: adjY } = adjacency.transform(head.x, head.y);
newHead.face = adjFace;
newHead.x = adjX;
newHead.y = adjY;
}
// Check collision with self
if (snake.some(segment => segment.face === newHead.face && segment.x === newHead.x && segment.y === newHead.y)) {
gameOver();
return;
}
// Insert new head
snake.unshift(newHead);
drawCell(newHead, 'snake');
// Check if apple is eaten
if (apple.face === newHead.face && apple.x === newHead.x && apple.y === newHead.y) {
removeCell(apple);
spawnApple();
} else {
// Remove tail
const tail = snake.pop();
removeCell(tail);
}
// Rotate cube to show the face with snake's head
rotateCubeToFace(newHead.face);
}
function rotateCubeToFace(face){
// Apply rotation based on the face
let rotationX = -20; // Base rotation
let rotationY = 45; // Base rotation
switch(face){
case 0: // Front
rotationX = -20;
rotationY = 45;
break;
case 1: // Back
rotationX = -20;
rotationY = 45 + 180;
break;
case 2: // Right
rotationX = -20;
rotationY = 45 - 90;
break;
case 3: // Left
rotationX = -20;
rotationY = 45 + 90;
break;
case 4: // Top
rotationX = -20 - 90;
rotationY = 45;
break;
case 5: // Bottom
rotationX = -20 + 90;
rotationY = 45;
break;
}
cube.style.transform = `translate(-50%, -50%) rotateX(${rotationX}deg) rotateY(${rotationY}deg)`;
}
function gameOver() {
isGameOver = true;
clearInterval(gameInterval);
messageElement.textContent = "Game Over! Press SPACE to restart";
}
// Handle key input
document.addEventListener('keydown', function(e) {
if (isGameOver && e.code === 'Space') {
initGame();
isGameStarted = true;
messageElement.textContent = "";
gameInterval = setInterval(gameLoop, 300);
return;
}
if (!isGameStarted && e.code === 'Space') {
isGameStarted = true;
messageElement.textContent = "";
gameInterval = setInterval(gameLoop, 300);
return;
}
if (isGameOver || !isGameStarted) return;
if (e.key === 'w' || e.key === 'ArrowUp') {
if (direction !== 'down') direction = 'up';
} else if (e.key === 's' || e.key === 'ArrowDown') {
if (direction !== 'up') direction = 'down';
} else if (e.key === 'a' || e.key === 'ArrowLeft') {
if (direction !== 'right') direction = 'left';
} else if (e.key === 'd' || e.key === 'ArrowRight') {
if (direction !== 'left') direction = 'right';
}
});
// Start the game
initGame();
})();
</script>
</body>
</html>
Explanation of Changes
Enhanced faceAdjacency:
Structure: For each face, each direction (up, down, left, right) now contains an object with:
face: The adjacent face index.
transform: A function that takes the current x and y coordinates and returns the new x and y on the adjacent face.
Benefits: This approach eliminates the need for string-based expressions and eval, ensuring that coordinate transformations are explicit and less error-prone.
Updated gameLoop Function:
Boundary Checking: Determines if the new head position crosses the boundary. If it does, it identifies the crossingDirection (up, down, left, right).
Face Transition: Uses the faceAdjacency mapping to determine the new face and calculates the corresponding x and y using the transform function.
Collision Detection: Checks if the new head position collides with the snake's body.
Improved rotateCubeToFace Function:
Rotation Logic: Adjusted to rotate the cube smoothly to the face where the snake's head currently resides. This enhances the visual feedback when the snake moves across different faces.
Base Rotation: Maintains a base rotation (rotateX(-20deg) rotateY(45deg)) and adjusts from there based on the target face.
UI Enhancements:
Z-Index for Message: Ensures that the message text appears above other elements.
Code Cleanup: Organized and commented the code for better readability.
Testing the Implementation
To ensure that the snake correctly traverses all faces of the cube:
Start the Game:
Press the SPACE bar to initiate the game.
Navigate Across Faces:
Use the W, A, S, D keys or the arrow keys to control the snake's direction.
Move the snake towards the edges of the current face to transition it to adjacent faces.
Observe the cube rotating to bring the current face with the snake's head into view.
Collision Detection:
Ensure that the game correctly detects collisions when the snake runs into itself.
Apple Consumption:
Verify that when the snake eats an apple, its length increases, and a new apple spawns on a different face.
Game Over Scenario:
Allow the snake to collide with itself and confirm that the game ends appropriately.
Conclusion
By refactoring the faceAdjacency mapping and updating the gameLoop and rotateCubeToFace functions, the snake can now traverse seamlessly across all six faces of the cube. This implementation ensures accurate position mapping and smooth visual transitions, enhancing the overall gameplay experience.
Feel free to further customize the transformations and visual aspects to fit your specific requirements or preferences.