<script>
window.onload = function() {
const faces = ['front', 'back', 'right', 'left', 'top', 'bottom'];
const cubeFaces = {};
faces.forEach(faceName => {
cubeFaces[faceName] = document.querySelector(`.${faceName}`);
createGrid(cubeFaces[faceName]);
});
function createGrid(faceElement) {
faceElement.style.display = 'grid';
faceElement.style.gridTemplateRows = 'repeat(10, 1fr)';
faceElement.style.gridTemplateColumns = 'repeat(10, 1fr)';
for (let row = 0; row < 10; row++) {
for (let col = 0; col < 10; col++) {
let cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.row = row;
cell.dataset.col = col;
faceElement.appendChild(cell);
}
}
}
let game = {
snake: [],
direction: null,
apple: null,
isRunning: false,
cubeFaces: cubeFaces
};
let gameLoop;
document.addEventListener('keydown', function(e) {
if (!game.isRunning && e.code === 'Space') {
startGame();
} else if (game.isRunning) {
switch(e.key) {
case 'w':
setDirection('up');
break;
case 's':
setDirection('down');
break;
case 'a':
setDirection('left');
break;
case 'd':
setDirection('right');
break;
}
}
});
function startGame() {
game.isRunning = true;
game.snake = [];
game.direction = getRandomDirection();
game.snake.push({face: 'front', row: 5, col: 5}); // Start at center of front face
placeApple();
updateView();
gameLoop = setInterval(updateGame, 300); // Update every 300ms
}
function getRandomDirection() {
const directions = ['up', 'down', 'left', 'right'];
return directions[Math.floor(Math.random() * directions.length)];
}
function setDirection(dir) {
const oppositeDirections = {
'up': 'down',
'down': 'up',
'left': 'right',
'right': 'left'
};
// Prevent snake from going back into itself
if (game.direction !== oppositeDirections[dir]) {
game.direction = dir;
}
}
function placeApple() {
let faceNames = ['front', 'back', 'left', 'right', 'top', 'bottom'];
let validCell = false;
while (!validCell) {
let randomFace = faceNames[Math.floor(Math.random() * faceNames.length)];
let row = Math.floor(Math.random() * 10);
let col = Math.floor(Math.random() * 10);
let occupied = game.snake.some(segment => segment.face === randomFace && segment.row === row && segment.col === col);
if (!occupied) {
game.apple = {face: randomFace, row: row, col: col};
validCell = true;
}
}
}
function updateGame() {
// Move snake
let head = game.snake[0];
let newHead = {...head};
switch(game.direction) {
case 'up':
newHead.row -= 1;
break;
case 'down':
newHead.row += 1;
break;
case 'left':
newHead.col -= 1;
break;
case 'right':
newHead.col += 1;
break;
}
// Handle wrapping around the cube faces
newHead = wrapPosition(newHead, game.direction);
// Check for collisions
if (isCollision(newHead)) {
gameOver();
return;
}
game.snake.unshift(newHead); // Add new head
// Check for apple
if (newHead.face === game.apple.face && newHead.row === game.apple.row && newHead.col === game.apple.col) {
placeApple(); // Eat apple and place a new one
} else {
game.snake.pop(); // Remove tail
}
updateView();
}
function wrapPosition(pos, direction) {
let face = pos.face;
let row = pos.row;
let col = pos.col;
// Handle wrapping to adjacent faces
if (row < 0) {
// Move to adjacent face
switch(face) {
case 'front':
face = 'top';
row = 9;
break;
case 'back':
face = 'top';
row = 9;
break;
case 'top':
face = 'back';
row = 9;
break;
case 'bottom':
face = 'front';
row = 0;
break;
case 'left':
face = 'top';
row = 9;
break;
case 'right':
face = 'top';
row = 9;
break;
}
} else if (row > 9) {
switch(face) {
case 'front':
face = 'bottom';
row = 0;
break;
case 'back':
face = 'bottom';
row = 0;
break;
case 'top':
face = 'front';
row = 0;
break;
case 'bottom':
face = 'back';
row = 0;
break;
case 'left':
face = 'bottom';
row = 0;
break;
case 'right':
face = 'bottom';
row = 0;
break;
}
} else if (col < 0) {
switch(face) {
case 'front':
face = 'left';
col = 9;
break;
case 'back':
face = 'right';
col = 9;
break;
case 'left':
face = 'back';
col = 9;
break;
case 'right':
face = 'front';
col = 9;
break;
case 'top':
face = 'left';
col = row;
row = 0;
break;
case 'bottom':
face = 'left';
col = 9 - row;
row = 9;
break;
}
} else if (col > 9) {
switch(face) {
case 'front':
face = 'right';
col = 0;
break;
case 'back':
face = 'left';
col = 0;
break;
case 'left':
face = 'front';
col = 0;
break;
case 'right':
face = 'back';
col = 0;
break;
case 'top':
face = 'right';
col = 9 - row;
row = 0;
break;
case 'bottom':
face = 'right';
col = row;
row = 9;
break;
}
}
return {face: face, row: row, col: col};
}
function isCollision(pos) {
// Check if collides with self
for (let i = 0; i < game.snake.length; i++) {
let segment = game.snake[i];
if (pos.face === segment.face && pos.row === segment.row && pos.col === segment.col) {
return true;
}
}
return false;
}
function gameOver() {
clearInterval(gameLoop);
game.isRunning = false;
alert('Game Over! Press Space to Restart.');
}
function updateView() {
// Clear all cells
document.querySelectorAll('.cell').forEach(cell => {
cell.classList.remove('snake', 'apple');
});
// Draw snake
game.snake.forEach(segment => {
let faceElement = game.cubeFaces[segment.face];
let cell = faceElement.querySelector(`.cell[data-row='${segment.row}'][data-col='${segment.col}']`);
if (cell) {
cell.classList.add('snake');
}
});
// Draw apple
let appleFace = game.cubeFaces[game.apple.face];
let appleCell = appleFace.querySelector(`.cell[data-row='${game.apple.row}'][data-col='${game.apple.col}']`);
if (appleCell) {
appleCell.classList.add('apple');
}
// Rotate cube to show the face with the snake's head
rotateCubeToFace(game.snake[0].face);
}
function rotateCubeToFace(faceName) {
let rotations = {
'front': 'rotateX(0deg) rotateY(0deg)',
'back': 'rotateX(0deg) rotateY(180deg)',
'right': 'rotateX(0deg) rotateY(-90deg)',
'left': 'rotateX(0deg) rotateY(90deg)',
'top': 'rotateX(90deg) rotateY(0deg)',
'bottom': 'rotateX(-90deg) rotateY(0deg)'
};
let cube = document.getElementById('cube');
cube.style.transform = rotations[faceName];
}
}
</script>
Sure! Here's your 3D Snake Game on a Cube transformed with a vibrant Neon Tron-like aesthetic. This makeover includes neon colors, glowing effects, and a sleek dark background to emulate the iconic Tron style.
Updated HTML, CSS, and JavaScript Code
html
<!DOCTYPE html>
<html>
<head>
<title>Neon Tron 3D Snake Game</title>
<style>
/* Importing a sleek Tron-like font */
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&display=swap');
body {
margin: 0;
overflow: hidden;
background: #0d0d0d; /* Dark background for better neon contrast */
font-family: 'Orbitron', sans-serif;
}
#cube-container {
position: absolute;
width: 80vmin;
height: 80vmin;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transform-style: preserve-3d;
perspective: 1000px;
}
#cube {
width: 100%;
height: 100%;
position: relative;
transform-style: preserve-3d;
transition: transform 1s cubic-bezier(0.4, 0.0, 0.2, 1);
}
.face {
position: absolute;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.85);
border: 2px solid #00ffff; /* Neon cyan borders */
box-sizing: border-box;
box-shadow: inset 0 0 20px rgba(0, 255, 255, 0.2);
}
.front { transform: translateZ(40vmin); }
.back { transform: rotateY(180deg) translateZ(40vmin); }
.right { transform: rotateY(90deg) translateZ(40vmin); }
.left { transform: rotateY(-90deg) translateZ(40vmin); }
.top { transform: rotateX(90deg) translateZ(40vmin); }
.bottom { transform: rotateX(-90deg) translateZ(40vmin); }
.cell {
border: 1px solid rgba(0, 255, 255, 0.3); /* Neon grid lines */
box-sizing: border-box;
position: relative;
transition: background-color 0.3s, box-shadow 0.3s;
}
.cell::after {
content: '';
display: block;
padding-top: 100%; /* Makes cells square */
}
/* Styles for snake and apple with glowing effects */
.snake {
background-color: #00ffff; /* Neon cyan snake */
box-shadow: 0 0 10px #00ffff, 0 0 20px #00ffff, 0 0 30px #00ffff;
}
.apple {
background-color: #ff00ff; /* Neon magenta apple */
box-shadow: 0 0 10px #ff00ff, 0 0 20px #ff00ff, 0 0 30px #ff00ff;
}
/* Optional: Add a subtle animation to the cube for extra flair */
@keyframes rotateCube {
0% { transform: rotateX(0deg) rotateY(0deg); }
100% { transform: rotateX(360deg) rotateY(360deg); }
}
/* Uncomment the following lines to enable continuous rotation */
/*
#cube {
animation: rotateCube 60s linear infinite;
}
*/
</style>
</head>
<body>
<div id="cube-container">
<div id="cube">
<div class="face front"></div>
<div class="face back"></div>
<div class="face right"></div>
<div class="face left"></div>
<div class="face top"></div>
<div class="face bottom"></div>
</div>
</div>
<script>
window.onload = function() {
const faces = ['front', 'back', 'right', 'left', 'top', 'bottom'];
const cubeFaces = {};
faces.forEach(faceName => {
cubeFaces[faceName] = document.querySelector(`.${faceName}`);
createGrid(cubeFaces[faceName]);
});
function createGrid(faceElement) {
faceElement.style.display = 'grid';
faceElement.style.gridTemplateRows = 'repeat(10, 1fr)';
faceElement.style.gridTemplateColumns = 'repeat(10, 1fr)';
for (let row = 0; row < 10; row++) {
for (let col = 0; col < 10; col++) {
let cell = document.createElement('div');
cell.classList.add('cell');
cell.dataset.row = row;
cell.dataset.col = col;
faceElement.appendChild(cell);
}
}
}
let game = {
snake: [],
direction: null,
apple: null,
isRunning: false,
cubeFaces: cubeFaces
};
let gameLoop;
document.addEventListener('keydown', function(e) {
if (!game.isRunning && e.code === 'Space') {
startGame();
} else if (game.isRunning) {
switch(e.key.toLowerCase()) {
case 'w':
setDirection('up');
break;
case 's':
setDirection('down');
break;
case 'a':
setDirection('left');
break;
case 'd':
setDirection('right');
break;
}
}
});
function startGame() {
game.isRunning = true;
game.snake = [];
game.direction = getRandomDirection();
game.snake.push({face: 'front', row: 5, col: 5}); // Start at center of front face
placeApple();
updateView();
gameLoop = setInterval(updateGame, 300); // Update every 300ms
}
function getRandomDirection() {
const directions = ['up', 'down', 'left', 'right'];
return directions[Math.floor(Math.random() * directions.length)];
}
function setDirection(dir) {
const oppositeDirections = {
'up': 'down',
'down': 'up',
'left': 'right',
'right': 'left'
};
// Prevent snake from going back into itself
if (game.direction !== oppositeDirections[dir]) {
game.direction = dir;
}
}
function placeApple() {
let faceNames = ['front', 'back', 'left', 'right', 'top', 'bottom'];
let validCell = false;
while (!validCell) {
let randomFace = faceNames[Math.floor(Math.random() * faceNames.length)];
let row = Math.floor(Math.random() * 10);
let col = Math.floor(Math.random() * 10);
let occupied = game.snake.some(segment => segment.face === randomFace && segment.row === row && segment.col === col);
if (!occupied) {
game.apple = {face: randomFace, row: row, col: col};
validCell = true;
}
}
}
function updateGame() {
// Move snake
let head = game.snake[0];
let newHead = {...head};
switch(game.direction) {
case 'up':
newHead.row -= 1;
break;
case 'down':
newHead.row += 1;
break;
case 'left':
newHead.col -= 1;
break;
case 'right':
newHead.col += 1;
break;
}
// Handle wrapping around the cube faces
newHead = wrapPosition(newHead, game.direction);
// Check for collisions
if (isCollision(newHead)) {
gameOver();
return;
}
game.snake.unshift(newHead); // Add new head
// Check for apple
if (newHead.face === game.apple.face && newHead.row === game.apple.row && newHead.col === game.apple.col) {
placeApple(); // Eat apple and place a new one
} else {
game.snake.pop(); // Remove tail
}
updateView();
}
function wrapPosition(pos, direction) {
let face = pos.face;
let row = pos.row;
let col = pos.col;
// Handle wrapping to adjacent faces
if (row < 0) {
// Move to adjacent face
switch(face) {
case 'front':
face = 'top';
row = 9;
break;
case 'back':
face = 'top';
row = 9;
break;
case 'top':
face = 'back';
row = 9;
break;
case 'bottom':
face = 'front';
row = 0;
break;
case 'left':
face = 'top';
row = 9;
break;
case 'right':
face = 'top';
row = 9;
break;
}
} else if (row > 9) {
switch(face) {
case 'front':
face = 'bottom';
row = 0;
break;
case 'back':
face = 'bottom';
row = 0;
break;
case 'top':
face = 'front';
row = 0;
break;
case 'bottom':
face = 'back';
row = 0;
break;
case 'left':
face = 'bottom';
row = 0;
break;
case 'right':
face = 'bottom';
row = 0;
break;
}
} else if (col < 0) {
switch(face) {
case 'front':
face = 'left';
col = 9;
break;
case 'back':
face = 'right';
col = 9;
break;
case 'left':
face = 'back';
col = 9;
break;
case 'right':
face = 'front';
col = 9;
break;
case 'top':
face = 'left';
col = row;
row = 0;
break;
case 'bottom':
face = 'left';
col = 9 - row;
row = 9;
break;
}
} else if (col > 9) {
switch(face) {
case 'front':
face = 'right';
col = 0;
break;
case 'back':
face = 'left';
col = 0;
break;
case 'left':
face = 'front';
col = 0;
break;
case 'right':
face = 'back';
col = 0;
break;
case 'top':
face = 'right';
col = 9 - row;
row = 0;
break;
case 'bottom':
face = 'right';
col = row;
row = 9;
break;
}
}
return {face: face, row: row, col: col};
}
function isCollision(pos) {
// Check if collides with self
for (let i = 0; i < game.snake.length; i++) {
let segment = game.snake[i];
if (pos.face === segment.face && pos.row === segment.row && pos.col === segment.col) {
return true;
}
}
return false;
}
function gameOver() {
clearInterval(gameLoop);
game.isRunning = false;
alert('Game Over! Press Space to Restart.');
}
function updateView() {
// Clear all cells
document.querySelectorAll('.cell').forEach(cell => {
cell.classList.remove('snake', 'apple');
});
// Draw snake
game.snake.forEach(segment => {
let faceElement = game.cubeFaces[segment.face];
let cell = faceElement.querySelector(`.cell[data-row='${segment.row}'][data-col='${segment.col}']`);
if (cell) {
cell.classList.add('snake');
}
});
// Draw apple
let appleFace = game.cubeFaces[game.apple.face];
let appleCell = appleFace.querySelector(`.cell[data-row='${game.apple.row}'][data-col='${game.apple.col}']`);
if (appleCell) {
appleCell.classList.add('apple');
}
// Rotate cube to show the face with the snake's head
rotateCubeToFace(game.snake[0].face);
}
function rotateCubeToFace(faceName) {
let rotations = {
'front': 'rotateX(0deg) rotateY(0deg)',
'back': 'rotateX(0deg) rotateY(180deg)',
'right': 'rotateX(0deg) rotateY(-90deg)',
'left': 'rotateX(0deg) rotateY(90deg)',
'top': 'rotateX(90deg) rotateY(0deg)',
'bottom': 'rotateX(-90deg) rotateY(0deg)'
};
let cube = document.getElementById('cube');
cube.style.transform = rotations[faceName];
}
}
</script>
</body>
</html>
Key Enhancements for Neon Tron Aesthetic
Color Scheme:
Background: Changed to a deep black (#0d0d0d) to make neon elements pop.
Borders and Grid Lines: Utilized bright cyan (#00ffff) for borders and grid lines to emulate the Tron look.
Snake and Apple:
Snake: Bright cyan (#00ffff) with multiple layers of glow.
Apple: Vibrant magenta (#ff00ff) to contrast the snake and stand out against the dark background.
Glowing Effects:
Applied box-shadow to .snake and .apple classes to create a neon glow around these elements.
Added box-shadow to the .face class for an inner neon glow effect.
Typography:
Incorporated the Orbitron font from Google Fonts to match the futuristic feel of Tron.
Animations:
Included an optional continuous rotation animation for the cube (rotateCube). It's currently commented out, but you can enable it by uncommenting the relevant CSS rules to add dynamic motion.
Transitions and Effects:
Enhanced transitions for smoother cube rotations and cell color changes.
Utilized a cubic-bezier function for the cube's transform transition to create a sleek motion.
Interactivity:
The game controls remain the same, with W, A, S, D for movement and Space to start/restart the game.
The alert message upon game over uses the default browser alert, which maintains focus on the game's neon visuals without adding additional UI elements.
How to Use
Start the Game:
Press the Space key to initiate the snake game.
Control the Snake:
Use the W (up), A (left), S (down), and D (right) keys to navigate the snake around the cube.
Objective:
Guide the snake to consume the magenta apples (#ff00ff) without colliding with itself. Each apple consumed grows the snake longer.
Game Over:
If the snake collides with itself, the game ends, and an alert prompts you to press Space to restart.
Customization Tips
Change Glow Intensity:
Adjust the box-shadow values in the .snake and .apple classes to increase or decrease the neon glow intensity.
Modify Grid Density:
Alter the gridTemplateRows and gridTemplateColumns in the .face class to make the grid finer or coarser.
Enable Continuous Rotation:
For a more dynamic visual, uncomment the #cube animation in the CSS to have the cube rotate continuously.
Adjust Game Speed:
Modify the interval duration in the setInterval function (updateGame call) to make the game faster or slower.