Implement 3D Snake with HTML + JS + CSS etc. The entire code should be written in a…
- User
Implement 3D Snake with HTML + JS + CSS etc. The entire code should be written in a single HTML block with embedded JS and CSS etc. Don't use any remote assets. After opening the html, user will need to hit space to start / restart the game, the snake will randomly go in one direction at the start and use "wasd" to control the direction of the snake. The game will take place on the surface of a cube the snake will automatically curl around the sides of the cube when it reaches the edge and the cube will always be showing the face with the head of the snake on it. the cube will be see through / transparent so that we can always see what side the apples are on no matter where they spawn. Make it pretty and the background large.
- Assistant
Sure! Below is a complete HTML file that implements a 3D Snake game as per your specifications. The entire code is enclosed within a single HTML file with embedded JavaScript and CSS, and it doesn't use any external assets. Simply copy the code into a
.htmlfile and open it in your browser to play.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>3D Snake Game on a Cube</title> <style> html, body { margin: 0; padding: 0; overflow: hidden; background: radial-gradient(#1e1e1e, #111); height: 100%; width: 100%; } #game-container { width: 800px; height: 800px; margin: auto; perspective: 1000px; position: relative; top: 50%; transform: translateY(-50%); } #cube { width: 400px; height: 400px; position: relative; transform-style: preserve-3d; transform: rotateX(0deg) rotateY(0deg); margin: auto; } .face { position: absolute; width: 400px; height: 400px; background: rgba(255, 255, 255, 0.05); border: 1px solid rgba(255, 255, 255, 0.1); box-sizing: border-box; } .face-grid { display: grid; grid-template-columns: repeat(10, 1fr); grid-template-rows: repeat(10, 1fr); width: 100%; height: 100%; } .cell { width: 100%; height: 100%; } .snake { background: limegreen; } .apple { background: red; } /* Cube Faces */ #front { transform: translateZ(200px); } #back { transform: rotateY(180deg) translateZ(200px); } #right { transform: rotateY(90deg) translateZ(200px); } #left { transform: rotateY(-90deg) translateZ(200px); } #top { transform: rotateX(90deg) translateZ(200px); } #bottom { transform: rotateX(-90deg) translateZ(200px); } /* Instructions */ #instructions { position: absolute; top: 20px; left: 50%; transform: translateX(-50%); color: #fff; font-family: Arial, sans-serif; text-align: center; z-index: 10; } #scoreboard { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); color: #fff; font-family: Arial, sans-serif; text-align: center; z-index: 10; } </style> </head> <body> <div id="game-container"> <div id="instructions"> <h1>3D Snake Game on a Cube</h1> <p>Press <strong>Space</strong> to Start/Restart</p> <p>Use <strong>W A S D</strong> keys to control the snake</p> </div> <div id="cube"> <div id="front" class="face"></div> <div id="back" class="face"></div> <div id="right" class="face"></div> <div id="left" class="face"></div> <div id="top" class="face"></div> <div id="bottom" class="face"></div> </div> <div id="scoreboard"> <p>Score: <span id="score">0</span></p> </div> </div> <script> // Game Variables const cube = document.getElementById('cube'); const faces = ['front', 'right', 'back', 'left', 'top', 'bottom']; const faceElements = {}; let snake = []; let direction = [0, -1]; // dx, dy let currentFace = 'front'; let gameInterval; let apple = null; let score = 0; let gameStarted = false; let cubeRotation = { x: 0, y: 0 }; // Initialize Faces faces.forEach(faceName => { const face = document.getElementById(faceName); face.classList.add('face-grid'); faceElements[faceName] = face; for (let i = 0; i < 100; i++) { const cell = document.createElement('div'); cell.classList.add('cell'); face.appendChild(cell); } }); // Start Game function startGame() { // Reset Variables snake = [{ face: 'front', x: 5, y: 5 }]; direction = getRandomDirection(); currentFace = 'front'; score = 0; document.getElementById('score').textContent = score; gameStarted = true; apple = null; // Clear Board clearBoard(); // Place Snake updateSnake(); // Place Apple placeApple(); // Start Game Loop clearInterval(gameInterval); gameInterval = setInterval(gameLoop, 200); } // Game Loop function gameLoop() { moveSnake(); checkCollision(); updateSnake(); } // Move Snake function moveSnake() { const head = snake[0]; let newHead = { ...head }; newHead.x += direction[0]; newHead.y += direction[1]; // Handle Edge Crossing if (newHead.x < 0) { newHead = crossEdge(head.face, 'left'); } else if (newHead.x > 9) { newHead = crossEdge(head.face, 'right'); } else if (newHead.y < 0) { newHead = crossEdge(head.face, 'up'); } else if (newHead.y > 9) { newHead = crossEdge(head.face, 'down'); } snake.unshift(newHead); // Check Apple Collision if (apple && newHead.face === apple.face && newHead.x === apple.x && newHead.y === apple.y) { score += 1; document.getElementById('score').textContent = score; placeApple(); } else { snake.pop(); } // Rotate Cube to keep head on front rotateCubeToFace(newHead.face); } // Cross Edge Logic function crossEdge(face, direction) { let newFace = ''; let x = 0; let y = 0; switch (face) { case 'front': if (direction === 'left') { newFace = 'left'; x = 9; y = snake[0].y; } if (direction === 'right') { newFace = 'right'; x = 0; y = snake[0].y; } if (direction === 'up') { newFace = 'top'; x = snake[0].x; y = 9; } if (direction === 'down') { newFace = 'bottom'; x = snake[0].x; y = 0; } break; case 'back': if (direction === 'left') { newFace = 'right'; x = 9; y = 9 - snake[0].y; } if (direction === 'right') { newFace = 'left'; x = 0; y = 9 - snake[0].y; } if (direction === 'up') { newFace = 'top'; x = 9 - snake[0].x; y = 0; } if (direction === 'down') { newFace = 'bottom'; x = 9 - snake[0].x; y = 9; } break; case 'left': if (direction === 'left') { newFace = 'back'; x = 9; y = snake[0].y; } if (direction === 'right') { newFace = 'front'; x = 0; y = snake[0].y; } if (direction === 'up') { newFace = 'top'; x = 0; y = 9 - snake[0].x; } if (direction === 'down') { newFace = 'bottom'; x = 0; y = snake[0].x; } break; case 'right': if (direction === 'left') { newFace = 'front'; x = 9; y = snake[0].y; } if (direction === 'right') { newFace = 'back'; x = 0; y = snake[0].y; } if (direction === 'up') { newFace = 'top'; x = 9; y = snake[0].x; } if (direction === 'down') { newFace = 'bottom'; x = 9; y = 9 - snake[0].x; } break; case 'top': if (direction === 'left') { newFace = 'left'; x = snake[0].y; y = 0; } if (direction === 'right') { newFace = 'right'; x = 9 - snake[0].y; y = 0; } if (direction === 'up') { newFace = 'back'; x = snake[0].x; y = 0; } if (direction === 'down') { newFace = 'front'; x = snake[0].x; y = 0; } break; case 'bottom': if (direction === 'left') { newFace = 'left'; x = 9 - snake[0].y; y = 9; } if (direction === 'right') { newFace = 'right'; x = snake[0].y; y = 9; } if (direction === 'up') { newFace = 'front'; x = snake[0].x; y = 9; } if (direction === 'down') { newFace = 'back'; x = snake[0].x; y = 9; } break; } return { face: newFace, x: x, y: y }; } // Update Snake on Board function updateSnake() { clearBoard(); snake.forEach((segment, index) => { const faceGrid = faceElements[segment.face]; const cellIndex = segment.y * 10 + segment.x; const cell = faceGrid.children[cellIndex]; cell.classList.add('snake'); }); // Draw Apple if (apple) { const faceGrid = faceElements[apple.face]; const cellIndex = apple.y * 10 + apple.x; const cell = faceGrid.children[cellIndex]; cell.classList.add('apple'); } } // Clear Board function clearBoard() { faces.forEach(faceName => { const faceGrid = faceElements[faceName]; Array.from(faceGrid.children).forEach(cell => { cell.classList.remove('snake', 'apple'); }); }); } // Place Apple function placeApple() { const availableCells = []; faces.forEach(faceName => { for (let y = 0; y < 10; y++) { for (let x = 0; x < 10; x++) { if (!snake.some(segment => segment.face === faceName && segment.x === x && segment.y === y)) { availableCells.push({ face: faceName, x: x, y: y }); } } } }); if (availableCells.length === 0) { alert('You win!'); clearInterval(gameInterval); gameStarted = false; return; } apple = availableCells[Math.floor(Math.random() * availableCells.length)]; } // Check Collision function checkCollision() { const head = snake[0]; // Check Self Collision for (let i = 1; i < snake.length; i++) { if (head.face === snake[i].face && head.x === snake[i].x && head.y === snake[i].y) { alert('Game Over!'); clearInterval(gameInterval); gameStarted = false; return; } } } // Rotate Cube to Face function rotateCubeToFace(face) { let xRotation = 0; let yRotation = 0; switch (face) { case 'front': xRotation = 0; yRotation = 0; break; case 'back': xRotation = 0; yRotation = 180; break; case 'left': xRotation = 0; yRotation = -90; break; case 'right': xRotation = 0; yRotation = 90; break; case 'top': xRotation = -90; yRotation = 0; break; case 'bottom': xRotation = 90; yRotation = 0; break; } cube.style.transform = `rotateX(${xRotation}deg) rotateY(${yRotation}deg)`; cubeRotation.x = xRotation; cubeRotation.y = yRotation; } // Handle Key Presses document.addEventListener('keydown', (e) => { if (!gameStarted && e.code === 'Space') { startGame(); return; } if (!gameStarted) return; switch (e.key.toLowerCase()) { case 'w': if (direction[1] === 0) { direction = [0, -1]; } break; case 's': if (direction[1] === 0) { direction = [0, 1]; } break; case 'a': if (direction[0] === 0) { direction = [-1, 0]; } break; case 'd': if (direction[0] === 0) { direction = [1, 0]; } break; } }); // Get Random Direction function getRandomDirection() { const directions = [[0, -1], [0, 1], [-1, 0], [1, 0]]; return directions[Math.floor(Math.random() * directions.length)]; } </script> </body> </html>Instructions:
- Copy the above code into a text editor and save it with a
.htmlextension (e.g.,3d_snake_cube.html). - Open the saved HTML file in your web browser.
- Press Spacebar to start or restart the game.
- Use the W, A, S, D keys to control the snake's direction.
- The snake moves on the surface of a cube and wraps around the edges.
- The cube rotates to always show the face where the snake's head is located.
- The cube is transparent, allowing you to see apples (red squares) on other faces.
- Try to collect as many apples as possible without running into yourself!
Notes:
- The game uses CSS 3D transforms to create and rotate the cube.
- JavaScript handles the game logic, including snake movement, apple placement, and collision detection.
- The background features a radial gradient for a pleasing visual effect.
- The cube and its faces are styled to be semi-transparent so you can see through to the other sides.
- The scoreboard at the bottom displays your current score.
Enjoy the game!
- Copy the above code into a text editor and save it with a
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- o1-mini-2024-09-12Compare side by side →
- o1-mini-2024-09-12Compare side by side →
- o1-preview-2024-09-12Compare side by side →
- o1-mini-2024-09-12Compare side by side →
- o1-mini-2024-09-12Compare side by side →
- o1-preview-2024-09-12Compare side by side →
- o1-preview-2024-09-12Compare side by side →
- o1-preview-2024-09-12Compare side by side →