<!DOCTYPE html>
<html>
<head>
<title>3D Snake Game</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #000;
}
#gameCanvas {
display: block;
width: 100vw;
height: 100vh;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
// Get canvas and context
let canvas = document.getElementById('gameCanvas');
let ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Handle window resize
window.addEventListener('resize', function(){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// Game variables
let snake = [];
let snakeLength = 5;
let direction = {x: 0, y: 0, z: 0};
let gameStarted = false;
let gameOver = false;
let gridSize = 20;
let gridCount = 20;
let halfGrid = (gridSize * gridCount) / 2;
// Input handling
let inputQueue = [];
// Key handling
document.addEventListener('keydown', function(e) {
if (e.key === ' ') {
// Start/restart the game
startGame();
}
if(gameStarted && !gameOver) {
if(['w', 'a', 's', 'd'].includes(e.key)) {
inputQueue.push(e.key);
}
}
});
function startGame() {
// Reset game state
snake = [];
snakeLength = 5;
gameOver = false;
gameStarted = true;
inputQueue = [];
// Initialize snake in middle of space
let middle = {x: 0, y: 0, z: 0};
for(let i = 0; i < snakeLength; i++) {
snake.push({x: middle.x, y: middle.y, z: middle.z - i * gridSize});
}
// Initialize direction randomly
let directions = [{x:gridSize,y:0,z:0}, {x:-gridSize,y:0,z:0}, {x:0,y:gridSize,z:0}, {x:0,y:-gridSize,z:0}, {x:0,y:0,z:gridSize}, {x:0,y:0,z:-gridSize}];
direction = directions[Math.floor(Math.random() * directions.length)];
// Start the game loop
requestAnimationFrame(gameLoop);
}
// Game loop
function gameLoop() {
update();
render();
if(!gameOver) {
setTimeout(function() {
requestAnimationFrame(gameLoop);
}, 100); // Control the speed of the snake
} else {
ctx.fillStyle = '#fff';
ctx.font = '24px Arial';
ctx.textAlign = 'center';
ctx.fillText('Game Over! Press SPACE to Restart', canvas.width / 2, canvas.height / 2);
}
}
// Update game state
function update() {
// Handle input
if(inputQueue.length > 0) {
let key = inputQueue.shift();
// Change direction based on input
let newDirection = {};
switch(key) {
case 'w':
newDirection = {x: 0, y: -gridSize, z: 0};
break;
case 's':
newDirection = {x: 0, y: gridSize, z: 0};
break;
case 'a':
newDirection = {x: -gridSize, y: 0, z: 0};
break;
case 'd':
newDirection = {x: gridSize, y: 0, z: 0};
break;
}
// Prevent the snake from reversing
if(!(newDirection.x === -direction.x && newDirection.y === -direction.y && newDirection.z === -direction.z)) {
direction = newDirection;
}
}
// Move snake
let head = {x: snake[0].x + direction.x, y: snake[0].y + direction.y, z: snake[0].z + direction.z};
// Check for collisions with self
for(let segment of snake) {
if(segment.x === head.x && segment.y === head.y && segment.z === head.z) {
gameOver = true;
return;
}
}
// Add new head to snake
snake.unshift(head);
// Keep snake length
if(snake.length > snakeLength) {
snake.pop();
}
}
// Render function
function render() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw 3D grid
ctx.strokeStyle = '#333';
ctx.lineWidth = 1;
for(let i = 0; i <= gridCount; i++) {
// Lines parallel to X axis
let z = -halfGrid + i * gridSize;
let start = project({x: -halfGrid, y: 0, z: z});
let end = project({x: halfGrid, y: 0, z: z});
if(start && end) {
ctx.beginPath();
ctx.moveTo(start.x, start.y);
ctx.lineTo(end.x, end.y);
ctx.stroke();
}
// Lines parallel to Z axis
let x = -halfGrid + i * gridSize;
start = project({x: x, y: 0, z: -halfGrid});
end = project({x: x, y: 0, z: halfGrid});
if(start && end) {
ctx.beginPath();
ctx.moveTo(start.x, start.y);
ctx.lineTo(end.x, end.y);
ctx.stroke();
}
}
// Draw snake
for(let i = 0; i < snake.length; i++) {
let segment = snake[i];
let point = project(segment);
if(point) {
// Adjust size based on depth
let scale = (cameraDistance / (cameraDistance + segment.z + halfGrid));
let radius = 10 * scale;
// Adjust color based on depth
let brightness = Math.max(50, 255 - (segment.z + halfGrid) * 255 / (gridSize * gridCount));
ctx.fillStyle = 'rgb(0,' + brightness + ',0)';
ctx.beginPath();
ctx.arc(point.x, point.y, radius, 0, 2 * Math.PI);
ctx.fill();
}
}
}
let cameraDistance = 500;
// Projection function
function project(point3D) {
// Simple perspective projection
let x = point3D.x;
let y = point3D.y;
let z = point3D.z + cameraDistance;
// If the point is behind the camera, don't render it
if(z <= 0) {
return null;
}
let scale = cameraDistance / (cameraDistance + z);
let x2D = x * scale + canvas.width / 2;
let y2D = y * scale + canvas.height / 2;
return {x: x2D, y: y2D};
}
// Initial message
ctx.fillStyle = '#fff';
ctx.font = '24px Arial';
ctx.textAlign = 'center';
ctx.fillText('Press SPACE to Start', canvas.width / 2, canvas.height / 2);
</script>
</body>
</html>