Here's a fully functional web page for a Twisted Snake Game with a power-up twist. The game includes a speed-boosting power-up that temporarily increases the snake's speed. The code is structured with HTML, CSS, and JavaScript, and is designed to be easy to extend or modify.
✅ Features:
- Classic Snake mechanics with a twist: power-ups that increase speed.
- Responsive design with a clean layout.
- Keyboard controls (arrow keys).
- Game Over detection and restart functionality.
📁 File Structure
snake-game/
│
├── index.html
├── styles.css
└── script.js
📄 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Twisted Snake Game</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="game-container">
<h1>Twisted Snake Game</h1>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div class="controls">
<button id="startBtn">Start Game</button>
<button id="pauseBtn">Pause Game</button>
<button id="resetBtn">Reset Game</button>
</div>
<div id="score">Score: 0</div>
<div id="status"></div>
</div>
<script src="script.js"></script>
</body>
</html>
🎨 styles.css
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}
.game-container {
margin: 20px auto;
padding: 20px;
background: #fff;
border: 2px solid #333;
width: 450px;
}
canvas {
border: 1px solid #000;
background: #000;
}
.controls button {
margin: 5px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
#score {
margin-top: 10px;
font-size: 18px;
}
#status {
margin-top: 10px;
font-weight: bold;
}
🧠 script.js
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [{ x: 10, y: 10 }];
let dx = 1;
let dy = 0;
let food = {};
let powerUp = null;
let score = 0;
let gameInterval;
let speed = 100; // milliseconds
let powerUpActive = false;
let powerUpTimer = 0;
function getRandomPosition() {
return Math.floor(Math.random() * tileCount);
}
function createFood() {
let newFood;
do {
newFood = { x: getRandomPosition(), y: getRandomPosition(), type: "regular" };
} while (
snake.some((segment) => segment.x === newFood.x && segment.y === newFood.y)
);
return newFood;
}
function createPowerUp() {
let newPowerUp;
do {
newPowerUp = { x: getRandomPosition(), y: getRandomPosition(), type: "power-up" };
} while (
snake.some((segment) => segment.x === newPowerUp.x && segment.y === newPowerUp.y) ||
(food.x === newPowerUp.x && food.y === newPowerUp.y)
);
return newPowerUp;
}
function draw() {
// Clear canvas
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw snake
ctx.fillStyle = "lime";
snake.forEach((segment) => {
ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 1, gridSize - 1);
});
// Draw food
ctx.fillStyle = "red";
ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 1, gridSize - 1);
// Draw power-up
if (powerUp) {
ctx.fillStyle = "yellow";
ctx.fillRect(powerUp.x * gridSize, powerUp.y * gridSize, gridSize - 1, gridSize - 1);
}
// Draw score
document.getElementById("score").innerText = "Score: " + score;
}
function update() {
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
// Check wall collision
if (
head.x < 0 ||
head.x >= tileCount ||
head.y < 0 ||
head.y >= tileCount
) {
gameOver();
return;
}
// Check self collision
if (snake.some((segment) => segment.x === head.x && segment.y === head.y)) {
gameOver();
return;
}
snake.unshift(head);
let ateFood = false;
let atePowerUp = false;
// Check food
if (head.x === food.x && head.y === food.y) {
score += 10;
food = createFood();
if (Math.random() < 0.2) {
powerUp = createPowerUp();
}
ateFood = true;
}
// Check power-up
if (powerUp && head.x === powerUp.x && head.y === powerUp.y) {
score += 50;
powerUpActive = true;
powerUpTimer = 100; // 100 ticks
powerUp = null;
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, speed * 0.8); // 20% faster
atePowerUp = true;
}
if (!ateFood && !atePowerUp) {
snake.pop();
}
// Handle power-up timer
if (powerUpActive) {
powerUpTimer--;
if (powerUpTimer <= 0) {
powerUpActive = false;
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, speed);
}
}
}
function gameLoop() {
update();
draw();
}
function startGame() {
snake = [{ x: 10, y: 10 }];
dx = 1;
dy = 0;
score = 0;
food = createFood();
powerUp = null;
powerUpActive = false;
document.getElementById("status").innerText = "";
gameInterval = setInterval(gameLoop, speed);
}
function gameOver() {
clearInterval(gameInterval);
document.getElementById("status").innerText = "Game Over! Refresh to play again.";
}
document.getElementById("startBtn").addEventListener("click", startGame);
document.getElementById("pauseBtn").addEventListener("click", () => {
clearInterval(gameInterval);
});
document.getElementById("resetBtn").addEventListener("click", () => {
startGame();
});
document.addEventListener("keydown", (e) => {
switch (e.key) {
case "ArrowLeft":
if (dx === 0) {
dx = -1;
dy = 0;
}
break;
case "ArrowUp":
if (dy === 0) {
dx = 0;
dy = -1;
}
break;
case "ArrowRight":
if (dx === 0) {
dx = 1;
dy = 0;
}
break;
case "ArrowDown":
if (dy === 0) {
dx = 0;
dy = 1;
}
break;
}
});
// Initialize game
startGame();
🧪 How to Play:
- Open
index.html in a browser.
- Use arrow keys to control the snake.
- Eat red food to grow and score points.
- Eat yellow power-ups to temporarily increase speed.
- Avoid walls and your own body.
- Click Start Game to begin, Pause Game to pause, and Reset Game to restart.
🧩 Ideas for Future Enhancements:
- Add multiple power-up types (e.g., shrink snake, reverse controls).
- Introduce obstacles or enemies.
- Add levels with increasing difficulty.
- Include sound effects and music.
Let me know if you'd like to expand the game further! 🎮