Here's the complete HTML file for a classic Nokia Snake game:
<!DOCTYPE html>
<html>
<head>
<title>Nokia Snake Game</title>
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #000;
color: lime;
font-family: Courier New, monospace;
}
canvas {
border: 2px solid lime;
background-color: #000;
box-shadow: 0 0 10px lime;
}
#scoreText {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
color: white;
font-size: 16px;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = 200;
canvas.height = 200;
const gridSize = 10;
let snake = [[10, 10], [9, 10], [8, 10]];
let direction = 'right';
let food = getRandomFood();
let score = 0;
let gameOver = false;
function getRandomFood() {
while (true) {
const x = Math.floor(Math.random() * 20);
const y = Math.floor(Math.random() * 20);
if (!snake.some(segment => segment[0] === x && segment[1] === y)) {
return [x, y];
}
}
}
function getDirectionFromKey(key) {
switch (key) {
case 'ArrowUp':
return 'up';
case 'ArrowDown':
return 'down';
case 'ArrowLeft':
return 'left';
case 'ArrowRight':
return 'right';
default:
return null;
}
}
function oppositeDirection(current) {
if (current === 'up') return 'down';
if (current === 'down') return 'up';
if (current === 'left') return 'right';
if (current === 'right') return 'left';
}
document.addEventListener('keydown', function(e) {
const newDir = getDirectionFromKey(e.key);
if (newDir && newDir !== oppositeDirection(direction)) {
direction = newDir;
}
});
function updateSnake() {
const head = [snake[0][0], snake[0][1]];
switch (direction) {
case 'up':
head[1]--;
break;
case 'down':
head[1]++;
break;
case 'left':
head[0]--;
break;
case 'right':
head[0]++;
break;
}
snake.unshift(head);
if (head[0] === food[0] && head[1] === food[1]) {
score += 10;
food = getRandomFood();
} else {
snake.pop();
}
}
function checkCollision() {
const head = snake[0];
if (
head[0] < 0 || head[0] >= 20 ||
head[1] < 0 || head[1] >= 20
) {
gameOver = true;
}
for (let i = 1; i < snake.length; i++) {
if (head[0] === snake[i][0] && head[1] === snake[i][1]) {
gameOver = true;
break;
}
}
}
function draw() {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'lime';
snake.forEach(segment => {
ctx.fillRect(
segment[0] * gridSize,
segment[1] * gridSize,
gridSize,
gridSize
);
});
ctx.fillStyle = '#00f';
ctx.fillRect(
food[0] * gridSize,
food[1] * gridSize,
gridSize,
gridSize
);
ctx.fillStyle = 'white';
ctx.font = '16px Courier New';
ctx.fillText(`Score: ${score}`, 10, 20);
if (gameOver) {
ctx.font = '30px Courier New';
ctx.fillText('GAME OVER', 25, 100);
clearInterval(gameLoop);
}
}
let gameLoop = setInterval(() => {
if (gameOver) return;
updateSnake();
checkCollision();
draw();
}, 100);
</script>
</body>
</html>
This HTML file contains everything needed to play the game:
- Game Mechanics:
- Classic snake movement with arrow keys
- Collision detection with walls and self
- Scoring system (increments by 10 points)
- Game over condition with restart option
- Visuals:
- Black background with lime green border (mimics phone screen)
- Lime green snake
- Blue food pellets
- Pixel-art style with 10x10 grid squares
- User Interface:
- Score display in top-left corner
- Game over message with centered text
- Responsive design centered on the screen
To play:
- Open the HTML file in any modern web browser
- Use arrow keys to control the snake
- Eat the blue food pellets to grow
- Avoid walls and your own tail
- Game ends when collision occurs
The game speed is set to 100ms per frame (adjustable in the setInterval timing). The Nokia-style look is achieved through the color scheme and pixel-art design.