Sure! Below is a complete HTML file that implements a 3D-like Snake game using HTML, CSS, and JavaScript. The game starts or restarts when the user presses the Spacebar and allows direction control using the W , A , S , and D keys. The snake starts moving in a random direction, and the game features a visually appealing 3D-inspired design with a large background.
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Snake Game</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
height: 100%;
width: 100%;
background: linear-gradient(135deg, #1e3c72, #2a5298);
font-family: Arial, sans-serif;
color: white;
}
#gameCanvas {
display: block;
background: rgba(0, 0, 0, 0.2);
}
#overlay {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
z-index: 10;
user-select: none;
}
#overlay.hidden {
display: none;
}
#overlay h1 {
font-size: 48px;
margin-bottom: 20px;
text-shadow: 2px 2px 4px #000;
}
#overlay p {
font-size: 24px;
text-shadow: 1px 1px 2px #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<div id="overlay">
<h1>3D Snake Game</h1>
<p>Press <strong>Space</strong> to Start / Restart</p>
<p>Use <strong>WASD</strong> keys to move</p>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const overlay = document.getElementById('overlay');
// Set canvas size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Grid settings
const gridSize = 20;
const tileSize = 30;
const rows = Math.floor(canvas.height / tileSize);
const cols = Math.floor(canvas.width / tileSize);
// Directions
const directions = {
'ArrowUp': {x: 0, y: -1},
'ArrowDown': {x: 0, y: 1},
'ArrowLeft': {x: -1, y: 0},
'ArrowRight': {x: 1, y: 0},
'w': {x: 0, y: -1},
's': {x: 0, y: 1},
'a': {x: -1, y: 0},
'd': {x: 1, y: 0}
};
let snake = [];
let direction = {x: 0, y: 0};
let nextDirection = {x: 0, y: 0};
let food = {};
let gameInterval;
let gameSpeed = 100; // milliseconds
// Initialize snake
function initSnake() {
snake = [
{x: Math.floor(cols / 2), y: Math.floor(rows / 2)},
{x: Math.floor(cols / 2) -1, y: Math.floor(rows / 2)},
{x: Math.floor(cols / 2) -2, y: Math.floor(rows / 2)}
};
// Random initial direction
const rand = Math.floor(Math.random() * 4);
switch(rand){
case 0: direction = {x: 1, y: 0}; break; // Right
case 1: direction = {x: -1, y: 0}; break; // Left
case 2: direction = {x: 0, y: 1}; break; // Down
case 3: direction = {x: 0, y: -1}; break; // Up
}
nextDirection = {x: direction.x, y: direction.y};
}
// Generate food
function generateFood() {
food = {
x: Math.floor(Math.random() * cols),
y: Math.floor(Math.random() * rows)
};
// Ensure food is not on the snake
for(let segment of snake){
if(segment.x === food.x && segment.y === food.y){
generateFood();
break;
}
}
}
// Draw grid (optional for 3D effect)
function drawGrid() {
ctx.strokeStyle = 'rgba(255,255,255,0.1)';
ctx.lineWidth = 1;
for(let x = 0; x <= cols; x++){
ctx.beginPath();
ctx.moveTo(x * tileSize, 0);
ctx.lineTo(x * tileSize, rows * tileSize);
ctx.stroke();
}
for(let y = 0; y <= rows; y++){
ctx.beginPath();
ctx.moveTo(0, y * tileSize);
ctx.lineTo(cols * tileSize, y * tileSize);
ctx.stroke();
}
}
// Draw snake
function drawSnake() {
for(let i = 0; i < snake.length; i++){
let segment = snake[i];
// Create gradient for 3D effect
let grd = ctx.createLinearGradient(segment.x * tileSize, segment.y * tileSize, segment.x * tileSize + tileSize, segment.y * tileSize + tileSize);
grd.addColorStop(0, '#4CAF50');
grd.addColorStop(1, '#2E7D32');
ctx.fillStyle = grd;
// Draw shadow for depth
ctx.shadowColor = 'rgba(0,0,0,0.5)';
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 5;
ctx.shadowOffsetY = 5;
ctx.fillRect(segment.x * tileSize, segment.y * tileSize, tileSize, tileSize);
ctx.shadowBlur = 0;
}
}
// Draw food
function drawFood() {
// 3D effect
let grd = ctx.createRadialGradient(food.x * tileSize + tileSize/2, food.y * tileSize + tileSize/2, tileSize/4, food.x * tileSize + tileSize/2, food.y * tileSize + tileSize/2, tileSize/2);
grd.addColorStop(0, '#FF5722');
grd.addColorStop(1, '#E64A19');
ctx.fillStyle = grd;
// Draw shadow
ctx.shadowColor = 'rgba(0,0,0,0.5)';
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 5;
ctx.shadowOffsetY = 5;
ctx.beginPath();
ctx.arc(food.x * tileSize + tileSize/2, food.y * tileSize + tileSize/2, tileSize/2 - 5, 0, Math.PI * 2);
ctx.fill();
ctx.shadowBlur = 0;
}
// Update game state
function update() {
direction = {x: nextDirection.x, y: nextDirection.y};
let head = {x: snake[0].x + direction.x, y: snake[0].y + direction.y};
// Wrap around the edges
if(head.x < 0) head.x = cols -1;
if(head.x >= cols) head.x = 0;
if(head.y < 0) head.y = rows -1;
if(head.y >= rows) head.y = 0;
// Check collision with self
for(let segment of snake){
if(segment.x === head.x && segment.y === head.y){
endGame();
return;
}
}
snake.unshift(head);
// Check if food is eaten
if(head.x === food.x && head.y === food.y){
generateFood();
} else {
snake.pop();
}
}
// Render game
function render() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw background
let bgGrd = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
bgGrd.addColorStop(0, '#1e3c72');
bgGrd.addColorStop(1, '#2a5298');
ctx.fillStyle = bgGrd;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Optionally draw grid
// drawGrid();
drawFood();
drawSnake();
}
// Game loop
function gameLoop() {
update();
render();
}
// Start game
function startGame() {
initSnake();
generateFood();
if(gameInterval) clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, gameSpeed);
overlay.classList.add('hidden');
}
// End game
function endGame() {
clearInterval(gameInterval);
overlay.innerHTML = `<h1>Game Over</h1><p>Press <strong>Space</strong> to Restart</p>`;
overlay.classList.remove('hidden');
}
// Handle key presses
document.addEventListener('keydown', function(e){
if(['w','a','s','d','ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(e.key.toLowerCase()) ){
let newDir = directions[e.key];
// Prevent the snake from reversing
if(newDir && (newDir.x !== -direction.x || newDir.y !== -direction.y)){
nextDirection = newDir;
}
}
if(e.key === ' '){
startGame();
}
});
// Adjust canvas on window resize
window.addEventListener('resize', function(){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// Initial render
render();
</script>
</body>
</html>
Feel free to customize the colors, speeds, and other parameters to enhance the game further. Have fun playing the 3D Snake game!