Below is a complete design for a web page that implements a playable version of the classic Snake game, but with a twist: This is "Wrap-Around Snake" (inspired by Pac-Man-style toroidal boards). Instead of dying when you hit the walls, the snake wraps around to the opposite side of the screen (e.g., going off the left edge reappears on the right). This adds a strategic layer, as you can loop around to ambush food or escape your own tail, but self-collision still ends the game.
The design is self-contained: a single HTML file with embedded CSS and JavaScript. You can copy-paste it into a file (e.g., snake.html) and open it in a modern browser to play. It uses HTML5 Canvas for rendering, keyboard controls (arrow keys or WASD), and a simple game loop.
Key Features:
- Game Board: 20x20 grid (400x400 pixels).
- Controls: Arrow keys or WASD to change direction.
- Twist Mechanics: Screen wraps around (no wall deaths), but colliding with your own body ends the game.
- Other Mechanics: Snake grows when eating food (red apple). Score increases by 1 per food. Game speeds up slightly as you grow.
- UI: Score display, game over alert with restart prompt.
- Responsive: Centered on the page, works on desktop (mobile touch controls could be added as an extension).
- Style: Clean, retro-inspired with a green snake and red food on a black grid.
snake.html (Complete Code)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake with a Twist: Wrap-Around Edition</title>
<style>
/* Global Styles */
body {
background-color: #1a1a1a;
color: #fff;
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
overflow: hidden;
}
/* Game Container */
#game-container {
position: relative;
border: 2px solid #00ff00;
box-shadow: 0 0 20px rgba(0, 255, 0, 0.5);
background-color: #000;
}
/* Canvas */
#gameCanvas {
display: block;
background-color: #000;
}
/* Score and Instructions */
#score {
position: absolute;
top: 10px;
left: 10px;
font-size: 24px;
color: #00ff00;
text-shadow: 0 0 5px #00ff00;
}
#instructions {
position: absolute;
bottom: 10px;
left: 10px;
font-size: 16px;
color: #fff;
text-align: left;
max-width: 380px;
}
#instructions h2 {
margin: 0;
color: #00ff00;
}
#instructions ul {
margin: 5px 0;
padding-left: 20px;
}
</style>
</head>
<body>
<div id="game-container">
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div id="score">Score: 0</div>
<div id="instructions">
<h2>Snake with a Twist: Wrap-Around</h2>
<ul>
<li>Use arrow keys or WASD to move.</li>
<li>Eat red food to grow and score.</li>
<li>Twist: Screen wraps around (no wall deaths)!</li>
<li>Avoid hitting your own body.</li>
<li>Game over? Refresh to restart.</li>
</ul>
</div>
</div>
<script>
// Game Setup
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const blockSize = 20; // Each grid block is 20x20 pixels
const cols = canvas.width / blockSize; // 20 columns
const rows = canvas.height / blockSize; // 20 rows
let snake = [{ x: 10 * blockSize, y: 10 * blockSize }]; // Initial snake position
let direction = { x: 0, y: 0 }; // Initial direction (stopped)
let food = getRandomFoodPosition();
let score = 0;
let speed = 100; // Initial speed (ms per frame)
// Game Loop
let game = setInterval(draw, speed);
// Keyboard Controls
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowUp' || e.key === 'w') {
if (direction.y !== blockSize) direction = { x: 0, y: -blockSize };
} else if (e.key === 'ArrowDown' || e.key === 's') {
if (direction.y !== -blockSize) direction = { x: 0, y: blockSize };
} else if (e.key === 'ArrowLeft' || e.key === 'a') {
if (direction.x !== blockSize) direction = { x: -blockSize, y: 0 };
} else if (e.key === 'ArrowRight' || e.key === 'd') {
if (direction.x !== -blockSize) direction = { x: blockSize, y: 0 };
}
});
// Draw the Game
function draw() {
// Clear canvas
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw Food
ctx.fillStyle = 'red';
ctx.fillRect(food.x, food.y, blockSize, blockSize);
// Move Snake
let head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
// Wrap-Around Twist: Teleport to opposite side if out of bounds
if (head.x >= canvas.width) head.x = 0;
if (head.x < 0) head.x = canvas.width - blockSize;
if (head.y >= canvas.height) head.y = 0;
if (head.y < 0) head.y = canvas.height - blockSize;
// Check for food collision
if (head.x === food.x && head.y === food.y) {
score++;
scoreElement.textContent = `Score: ${score}`;
food = getRandomFoodPosition();
speed = Math.max(50, speed - 2); // Speed up slightly
clearInterval(game);
game = setInterval(draw, speed);
// Grow snake by not removing tail
} else {
snake.pop(); // Remove tail if no food eaten
}
// Add new head
snake.unshift(head);
// Check for self-collision
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
clearInterval(game);
alert(`Game Over! Score: ${score}\nRefresh to play again.`);
return;
}
}
// Draw Snake
ctx.fillStyle = '#00ff00';
snake.forEach(segment => {
ctx.fillRect(segment.x, segment.y, blockSize, blockSize);
});
}
// Generate random food position (not on snake)
function getRandomFoodPosition() {
let position;
while (true) {
position = {
x: Math.floor(Math.random() * cols) * blockSize,
y: Math.floor(Math.random() * rows) * blockSize
};
if (!snake.some(segment => segment.x === position.x && segment.y === position.y)) {
return position;
}
}
}
</script>
</body>
</html>
How to Play
- Save the code as
snake.html and open it in a browser (e.g., Chrome, Firefox).
- Use arrow keys or WASD to control the snake.
- Eat red squares to grow and score points.
- The screen wraps around, so experiment with looping strategies!
- If you collide with yourself, the game ends with an alert. Refresh the page to restart.
Design Rationale
- HTML Structure: A container div wraps the canvas and overlays (score, instructions) for easy positioning.
- CSS Styling: Dark theme for a retro arcade feel, with glowing green accents. Everything is centered and responsive.
- JavaScript Logic: Simple game loop using
setInterval. The wrap-around is handled by modulo-like checks on the head position. No external libraries needed.
- Twist Integration: The wrap-around mechanic is seamless and changes gameplay without overcomplicating the code.
- Extensibility: You could add power-ups (e.g., temporary speed boosts) or sounds by extending the JS. For mobile, add touch event listeners.
If this twist isn't what you had in mind (e.g., you want a different one like "shrinking food" or "multiplayer"), let me know for adjustments!